draw/tester-bundle
Composer 安装命令:
composer require draw/tester-bundle
包简介
README 文档
README
This bundle integrate the Draw Tester Component.
It also provides test helpers to make it easier to test your Symfony application.
Kernel Testing
When configuring your kernel you may want to test that everything is hooked up correctly.
There is the list of service, event dispatcher, command etc.
There is some TestCase/Trait to help you do that (work in progress).
Event Dispatcher
Relying on the debug:event-dispatcher command we can dump the list of event listeners and validated it against the expected list.
<?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 ); } }
The first time you run this test it will fail and dump the current configuration in the event_dispatcher.xml file.
Commit this file, next time your rune this test you will be able to validate that the configuration is still valid.
If you change the listener in your code or change your dependencies you can run the test again and see the diff.
This will allow you to see if some external listeners changed at the same time.
PHPUnit Extension
This bundle also provide a PHPUnit extension to make it easier to test your Symfony application.
KernelShutdown
Sometimes you need to use the kernel/container in a tearDownAfterClass method.
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(); } }
Since symfony shutdown the kernel in the tearDown method, this will boot a new kernel cause a kernel to be up.
Adding the KernelShutdownExtension will make sure the kernel is shutdown after the test.
<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 addon
The draw/tester component provide a way to autowire property in your test.
This bundle provide some custom Autowire attribute that can use in the context of a Symfony test cases.
Make sure to register is in your phpunit configuration file. as explained in the draw/tester documentation.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/> </extensions> </phpunit>
Here is an example of attribute you can use in your test case:
namespace App\Tests; use App\AServiceInterface; use App\Entity\User; use App\MyService; use App\MyOtherService; use Draw\Bundle\TesterBundle\Messenger\TransportTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireEntity; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireLoggerTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireReloadedEntity;use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireServiceMock; 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 Monolog\Handler\TestHandler; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase implements AutowiredInterface { // Will fetch the entity via doctrine base on it,s primary if proper is set. // Useful if one of your test create the entity and you want to use it in another test. // Will not work if property is not static since it will always be null on next test. #[AutowireReloadedEntity] static private ?User $user = null; // Create a mock of an interface #[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; // Rely on the 'monolog.handler.testing' service to be available in the container. #[AutowireLoggerTester] private TestHandler $loggerTester; #[AutowireEntity(['email' => 'test@example.com'])] private User $user; // Will create a mock object of MyOtherService and call container->set(MyOtherService::class, $mockObject) // You can also set the service id to use in the container as the first parameter of the attribute. #[AutowireServiceMock] private MyOtherService&MockObject $myOtherService; }
If you extend from a WebTestCase you can also use the AutowireClient attribute to get a client.
By using the AutowireClient in conjunction with the AutowireService you are use that the client is
created before the other service preventing the exception:
Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient" is not supported, the kernel should only be booted once
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(); } }
This is the same client as the one you get from the WebTestCase, you can use it the same way.
Note that the AutowireClient attribute have an options and server parameters like you would do when calling the createClient method.
DoctrineTransaction
Base on dama/doctrine-test-bundle this extension will start a transaction before each test class and rollback it after the test.
By using test class instead of test method, like the original bundle does, it will ease dependencies managements.
Make sure to configure this extension first in your phpunit configuration file.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\DoctrineTransaction\DoctrineTransactionExtension"/> </extensions> </phpunit>
Also if one of your test should not have transaction you can use the NoTransaction attribute on the test class.
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 { /*...*/ } }
draw/tester-bundle 适用场景与选型建议
draw/tester-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60.36k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「unit test」 「test」 「automation test」 「integration test」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 draw/tester-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 draw/tester-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 draw/tester-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Testing Suite For Lumen like Laravel does.
Symfony bundle for unigen test generator
A cli tool which generates unit tests.
The bundle for easy using json-rpc api on your project
PHPUnit Testing extensions for HMTL and CSS insures valid HTML and CSS via v.Nu validator
统计信息
- 总下载量: 60.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-14