vanio/vanio-testing-bundle

Symfony2 Bundle 主要帮助您进行集成测试。

安装: 6

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master / 0.1.x-dev 2016-09-25 10:35 UTC

This package is auto-updated.

Last update: 2024-09-05 01:25:20 UTC


README

Build Status Coverage Status PHP7 License

一个主要帮助您进行集成测试的 Symfony2 Bundle。

安装

可以使用 composer 像往常一样进行安装。 composer require vanio/vanio-testing-bundle

如果在测试包配置的情况下,您需要扩展已准备的 Vanio\TestingBundle\HttpKernel\TestKernel 类。它为您提供了在测试包配置时可以使用的 TestContainerBuilder。此内核还预配置以便更容易创建独立的包测试环境。

<?php
// AppKernel.php

use Vanio\TestingBundle\HttpKernel\TestKernel;

class AppKernel extends TestKernel
{}

TestKernel 类的默认实现期望您在内核根目录相对位置创建一个名为 Tests/Functional/app/{environment}/bundles.php 的文件,返回要注册的包数组。您可以在测试用例中稍后指定环境。

<?php
// Tests/Functional/app/default/bundles.php

return [
    new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle,
    new Symfony\Bundle\FrameworkBundle\FrameworkBundle,
    new Vanio\TestingBundle\Tests\Functional\Bundle\TestBundle,
];

类似地,它期望您创建一个名为 Tests/Functional/app/{environment}/config.yml 的配置文件,您可以在其中配置包。

<?php
// Tests/Functional/app/default/config.yml

return [
    new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle,
    new Symfony\Bundle\FrameworkBundle\FrameworkBundle,
    new Vanio\TestingBundle\Tests\Functional\Bundle\TestBundle,
];

否则,您可以在 AppKernel 中经典地注册此包。

// app/AppKernel.php
// ...

class AppKernel extends Kernel
{
    // ...

    public function registerBundles(): array
    {
        // ...

        if ($this->environment === 'test') {
            $bundles[] = new Vanio\TestingBundle\VanioTestingBundle;
        }

        // ...
    }
}

以下示例取自此包的实际测试。

测试包扩展的配置

// Tests/Functional/BundleConfigurationTest.php
// ...

use Vanio\TestingBundle\DependencyInjection\TestContainerBuilder;
use Vanio\TestingBundle\PhpUnit\KernelTestCase;

class ExtensionConfigurationTest extends KernelTestCase
{
    function test_extension_configuration()
    {
        self::boot();
        $this->assertInstanceOf(TestContainerBuilder::class, self::$container);
        $this->assertArraySubset(
            [
                'secret' => 'secret',
                'form' => ['enabled' => false],
                'profiler' => ['enabled' => false],
                'router' => ['enabled' => true],
            ],
            self::$container->processExtensionConfig('framework')
        );
    }
}

页面渲染测试

// Tests/Functional/PageRenderingTest.php
// ...

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;
use Vanio\TestingBundle\PhpUnit\WebTestCase;

class PageRenderingTest extends WebTestCase
{
    function test_page_renders_successfully()
    {
        self::boot();
        $response = $this->request('GET', '/test');
        $this->assertInstanceOf(Response::class, $response);
        $this->assertSame(200, $response->getStatusCode());
        $this->assertHtmlContainsText(
            '
                HEADING
                Paragraph
                Link [/]
            ',
            $response->getContent()
        );
        $this->assertHtmlNotContainsText('foo', $response->getContent());
    }

    function test_crawling_page()
    {
        self::boot();
        $this->request('GET', '/test');
        $this->assertInstanceOf(Crawler::class, $this->crawler);
        $this->assertSame(1, $this->crawler->filter('html:contains("Heading")')->count());
    }
}