draw/tester
Composer 安装命令:
composer require draw/tester
包简介
Library base on phpunit to test your data with a fluent interface.
README 文档
README
This package provide a set of tools to help you test your application.
Data Tester
This library is a wrapper around PHPUnit Assert class to be able to use a fluent interface on the data you want to test.
Here is a quick example of how to use it in a PHPUnit TestCase:
namespace Your\Project\Name; use PHPUnit\Framework\TestCase; use Draw\Component\Tester\DataTester; class SimpleTest extends TestCase { public function test() { $data = [ 'key1' => 'value1', 'key2' => (object)['toto' => 'value'] ]; $dateTester = new DataTester($data); $dateTester ->assertIsArray('array') ->assertCount(2) ->path('[key1]')->assertSame('value1'); $dateTester->path('[key2].toto')->assertSame('value'); } }
PHPUnit Extension
This package also provide a PHPUnit extension to make it easier to write test.
CarbonReset
If you are using Carbon in your project, you might want to reset the Carbon class between each test to make sure you have a consistent state.
Register the extension in your phpunit configuration file.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\CarbonReset\CarbonResetExtension"/> </extensions> </phpunit>
This will reset your carbon class between each test and test suite like it would in TestCass::tearDown and TestCass::tearDownAfterClass.
SetUpAutowire
A bit like the Service auto wiring would work via a service container, this extension allow you to autowire properties
base on attribute that implement AutowireInterface.
Make sure to register is in your phpunit configuration file.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/> </extensions> </phpunit>
Once this is done, your test need to implement the AutowiredInterface interface so the extension will hook it.
namespace App\Tests; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase implements AutowiredInterface { }
Having the extension by itself doesn't do much, you need to put some attribute on the property you need to autowire.
Note that the autowired system doesn't work on static properties.
namespace App\Tests; use App\MyInterface; use App\MyObject; use App\MySecondObject; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class MyTest extends TestCase implements AutowiredInterface { // Will create a mock object of MyInterface and assigned it to property. // This can be used in conjunction with the AutowireMockProperty (see below). #[AutowireMock] private MyInterface&MockObject $aService // The AutowireMockProperty will replace the aService property of $myObject. #[AutowireMockProperty('aService')] private MyObject $myObject; // 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. #[AutowireMockProperty('aService', 'anotherProperty')] private MySecondObject $mySecondObject; public function setUp(): void { $this->myObject = new MyObject(); $this->mySecondObject = new MySecondObject(); } }
This might seem a bit useless, but in a framework context using service it will more sense. The
AutowireServicefrom draw/tester-bundle is a good example of this in Symfony.
Since the auto wiring is done in the setUp hook of phpunit extension you cannot use them in the setup method of you test.
If you need to access those property in your setUp method, you can use the AutowiredCompletionAwareInterface instead.
namespace App\Tests; use App\MyService;use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredCompletionAwareInterface;use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase implements AutowiredCompletionAwareInterface { #[AutowireMock] private MyInterface&MockObject $aService public function postAutowire(): void { $this->aService ->expects(static::any()) ->method('someMethod') ->willReturn('someValue'); } }
Creating you own Autowire attribute
You can create your own attribute to autowire your own property.
You just need to create an attribute that implement the AutowireInterface interface.
namespace App\Test\PHPUnit\SetUpAutowire; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireInterface;use PHPUnit\Framework\TestCase; #[\Attribute(\Attribute::TARGET_PROPERTY)] class AutowireRandomInt implements AutowireInterface { // This is the priority of the autowire. The higher the number the sooner it will be called. // This can be important if you need to autowire a property before another one. public static function getPriority(): int { return 0; } public function __construct( private int $min = \PHP_INT_MIN, private int $max = \PHP_INT_MAX ) {} public function autowire(TestCase $testCase, \ReflectionProperty $reflectionProperty): void { $reflectionProperty->setValue( $testCase, random_int($this->min, $this->max) ); } }
Now you can simply use it in your test case:
namespace App\Tests; use App\Test\PHPUnit\SetUpAutowire\AutowireRandomInt; class MyTest extends KernelTestCase { #[AutowireRandomInt(1, 10)] private int $randomInt; }
draw/tester 适用场景与选型建议
draw/tester 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.46k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「phpunit」 「test」 「draw」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 draw/tester 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 draw/tester 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 draw/tester 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
Specification-oriented BDD helpers for PHPUnit
PHPUnit DbUnit Native Array-based Fixtures
统计信息
- 总下载量: 62.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 19
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-14