draw / tester-bundle
Requires
- ext-dom: *
- draw/core: ^0.10.47
- draw/tester: ^0.10.47
- symfony/browser-kit: ^6.4.0
- symfony/deprecation-contracts: ^3.5.0
- symfony/framework-bundle: ^6.4.0
- symfony/http-foundation: ^6.4.0
- symfony/validator: ^6.4.0
Requires (Dev)
- draw/profiling: ^0.10.47
- phpunit/phpunit: ^11.3
- symfony/messenger: ^6.4.0
- symfony/yaml: ^6.4.0
- dev-master / 0.11.x-dev
- 0.10.47
- 0.10.46
- 0.10.45
- 0.10.44
- 0.10.43
- 0.10.42
- 0.10.41
- 0.10.40
- 0.10.39
- 0.10.38
- 0.10.37
- 0.10.36
- 0.10.35
- 0.10.34
- 0.10.33
- 0.10.32
- 0.10.31
- 0.10.30
- 0.10.29
- 0.10.28
- 0.10.27
- 0.10.26
- 0.10.25
- 0.10.24
- 0.10.23
- 0.10.22
- 0.10.21
- 0.10.20
- 0.10.18
- 0.10.17
- 0.10.16
- 0.10.15
- 0.10.14
- 0.10.13
- 0.10.11
- 0.10.10
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.92
- 0.9.91
- 0.9.90
- 0.9.89
- 0.9.88
- 0.9.87
- 0.9.86
- 0.9.85
- 0.9.84
- 0.9.83
- 0.9.82
- 0.9.81
- 0.9.80
- 0.9.79
- 0.9.78
- 0.9.77
- 0.9.76
- 0.9.75
- 0.9.74
- 0.9.73
- 0.9.72
- 0.9.71
- 0.9.70
- 0.9.69
- 0.9.68
- 0.9.67
- 0.9.66
- 0.9.65
- 0.9.64
- 0.9.63
- 0.9.62
- 0.9.61
- 0.9.60
- 0.9.59
- 0.9.58
- 0.9.57
- 0.9.56
- 0.9.55
- 0.9.54
- 0.9.53
- 0.9.52
- 0.9.51
- 0.9.50
- 0.9.49
- 0.9.48
- 0.9.47
- 0.9.46
- 0.9.45
- 0.9.44
- 0.9.43
- 0.9.42
- 0.9.41
- 0.9.40
- 0.9.39
- 0.9.38
- 0.9.37
- 0.9.36
- 0.9.35
- 0.9.34
- 0.9.33
- 0.9.32
- 0.9.31
- 0.9.30
- 0.9.29
- 0.9.28
- 0.9.27
- 0.9.26
- 0.9.25
- 0.9.24
- 0.9.23
- 0.9.22
- 0.9.21
- 0.9.20
- 0.9.19
- 0.9.18
- 0.9.17
- 0.9.16
- 0.9.15
- 0.9.14
- 0.9.13
- 0.9.12
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.59
- 0.7.58
- 0.7.57
- 0.7.56
- 0.7.54
- 0.7.53
- v0.7.52
- v0.7.51
- v0.7.50
- v0.7.49
- v0.7.48
- v0.7.47
- v0.7.46
- v0.7.45
- v0.7.44
- v0.7.43
- v0.7.42
- v0.7.41
- v0.7.40
- v0.7.39
- v0.7.38
- v0.7.37
- v0.7.36
- v0.7.35
- v0.7.34
- v0.7.33
- v0.7.32
- v0.7.31
- v0.7.30
- v0.7.29
- v0.7.28
- v0.7.27
- v0.7.26
- v0.7.25
- v0.7.24
- v0.7.23
- v0.7.22
- 0.7.21
- v0.7.20
- v0.7.19
- v0.7.18
- v0.7.17
- v0.7.16
- v0.7.15
- v0.7.14
- v0.7.13
- v0.7.12
- v0.7.11
- v0.7.10
- v0.7.9
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.23
- 0.5.22
- v0.5.21
- v0.5.20
- v0.5.19
- v0.5.18
- v0.5.17
- v0.5.16
- v0.5.15
- v0.5.14
- v0.5.13
- v0.5.12
- v0.5.11
- v0.5.10
- v0.5.9
- v0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- dev-packages-git-actions
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
属性来获取客户端。
通过结合使用 AutowireClient
和 AutowireService
,您确保客户端在创建其他服务之前创建,从而防止异常。
在调用 "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
属性有 options
和 server
参数,就像您在调用 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 { /*...*/ } }