draw/tester-bundle

安装数: 42,671

依赖关系: 7

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

开放性问题: 0

类型:symfony-bundle

0.10.47 2024-09-25 20:25 UTC

This package is auto-updated.

Last update: 2024-09-25 20:27:13 UTC


README

此包集成了 Draw Tester 组件。

它还提供测试助手,使测试 Symfony 应用程序更加容易。

内核测试

当配置内核时,您可能想测试一切是否连接正确。

这里有一份服务、事件调度器、命令等的列表。

有一些 TestCase/Trait 帮助您完成这项工作(正在进行中)。

事件调度器

依赖于 debug:event-dispatcher 命令,我们可以导出事件监听器列表,并与预期列表进行验证。

<?php

namespace App\Tests;

use Draw\Bundle\TesterBundle\EventDispatcher\EventDispatcherTesterTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class AppKernelTest extends KernelTestCase
{
    use EventDispatcherTesterTrait;

    public function testEventDispatcherConfiguration(): void
    {
        $this->assertEventDispatcherConfiguration(
            __DIR__.'/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml',
           'event_dispatcher' // This is the default value, same as the debug:event-dispatcher command
        );
    }
}

第一次运行此测试时,它将失败,并在 event_dispatcher.xml 文件中导出当前配置。

提交此文件,下次运行此测试时,您将能够验证配置是否仍然有效。

如果您更改了代码中的监听器或更改了依赖项,您可以再次运行测试并查看差异。

这将允许您查看是否有外部监听器同时更改。

PHPUnit 扩展

此包还提供 PHPUnit 扩展,以使测试 Symfony 应用程序更加容易。

KernelShutdown

有时您需要在 tearDownAfterClass 方法中使用 kernel/container。

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class MyTest extends KernelTestCase
{
    use KernelShutdownTrait;

    public static function tearDownAfterClass(): void
    {
        static::getContainer()->get('my_service')->doSomething();
    }
}

由于 symfony 在 tearDown 方法中关闭内核,这将启动一个新的内核导致内核处于运行状态。

添加 KernelShutdownExtension 将确保在测试后关闭内核。

<phpunit bootstrap="vendor/autoload.php">
    <extensions>
        <!-- It must be after any extension that could also boot a kernel -->
        <bootstrap class="Draw\Bundle\TesterBundle\PHPUnit\Extension\KernelShutdown\KernelShutdownExtension"/>
    </extensions>
</phpunit>

SetUpAutowire 扩展

draw/tester 组件提供了一种在测试中自动装配属性的方法。

此包提供一些自定义 Autowire 属性,可以在 Symfony 测试用例的上下文中使用。

请确保在 phpunit 配置文件中注册它,如 draw/tester 文档中所述。

<phpunit bootstrap="vendor/autoload.php">
    <extensions>
        <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/>
    </extensions>
</phpunit>

以下是在测试用例中可以使用的属性示例

namespace App\Tests;

use App\AServiceInterface;
use App\MyService;
use Draw\Bundle\TesterBundle\Messenger\TransportTester;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireTransportTester;
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class MyTest extends KernelTestCase implements AutowiredInterface
{
   // From d
   #[AutowireMock]
   private AServiceInterface&MockObject $aService
   
   // Will hook MyService from the test container. Your test need to extend KernelTestCase.
   //
   // The AutowireMockProperty will replace the aService property of $myService. 
   // By defaults, it will use the same property name in the current test case but you can specify a different one using the second parameter.
   #[AutowireService]
   #[AutowireMockProperty('aService')]
   private MyService $myService;
   
   // Will hook the parameter from the container using ParameterBagInterface::resolveValue
   #[AutowireParameter('%my_parameter%')]
   private string $parameter;
   
   // Will hook the transport tester from the container.
   #[AutowireTransportTester('async')]
   private TransportTester $transportTester;
}

如果您从 WebTestCase 继承,您还可以使用 AutowireClient 属性来获取客户端。

通过结合使用 AutowireClientAutowireService,您确保客户端在创建其他服务之前创建,从而防止异常。

在调用 "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient" 之前启动内核是不支持的,内核应该只启动一次。

namespace App\Tests;

use App\MyService;use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireClient;use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;use Symfony\Bundle\FrameworkBundle\KernelBrowser;use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyTest extends WebTestCase implements AutowiredInterface
{
    #[AutowireClient]
    private KernelBrowser $client;
   
    public function testSomething(): void
    {
        $this->client->request('GET', '/my-route');
        
        static::assertResponseIsSuccessful();
    }
}

这是与您从 WebTestCase 获得的相同客户端,您可以以相同的方式使用它。

请注意,AutowireClient 属性有 optionsserver 参数,就像您在调用 createClient 方法时一样。

DoctrineTransaction

基于 dama/doctrine-test-bundle,此扩展在每个测试类之前启动一个事务,并在测试后回滚。

通过使用测试类而不是测试方法,如原始包所做的那样,它可以简化依赖关系管理。

请确保首先在您的 phpunit 配置文件中配置此扩展。

<phpunit bootstrap="vendor/autoload.php">
    <extensions>
        <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\DoctrineTransaction\DoctrineTransactionExtension"/>
    </extensions>
</phpunit>

此外,如果您的某个测试不需要事务,您可以在测试类上使用 NoTransaction 属性。

namespace App\Tests;

use Draw\Bundle\TesterBundle\PHPUnit\Extension\DoctrineTransaction\NoTransaction;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

#[NoTransaction]
class MyTest extends KernelTestCase
{
    public function testSomething(): void
    {
        /*...*/
    }
}