draw / tester
基于phpunit的库,以流畅的接口测试您的数据。
Requires
- ext-simplexml: *
- draw/core: ^0.10.47
- guzzlehttp/psr7: ^1.8 || ^2.0
- phpunit/phpunit: ^11.3
- psr/http-message: ^1.0
- symfony/property-access: ^6.4.0
Requires (Dev)
- doctrine/orm: ^2.11
- draw/dependency-injection: ^0.10.47
- nesbot/carbon: ^2.0
- pdeans/http: ^1.1
- symfony/console: ^6.4.0
- symfony/dependency-injection: ^6.4.0
Suggests
- pdeans/http: If you wan to use the curl http client
- symfony/cache: If you want to use the orm setup tool
- symfony/dependency-injection: If you want to test extension/configuration
- 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:16 UTC
README
此包提供了一组工具,帮助您测试您的应用程序。
数据测试器
此库是围绕 PHPUnit Assert 类的包装,以便能够在您要测试的数据上使用流畅的接口。
以下是一个如何在 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 扩展
此包还提供了一个PHPUnit扩展,使编写测试更容易。
CarbonReset
如果您在项目中使用Carbon,您可能希望在每个测试之间重置Carbon类,以确保您有一个一致的状态。
在您的PHPUnit配置文件中注册该扩展。
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\CarbonReset\CarbonResetExtension"/> </extensions> </phpunit>
这将像在 TestCass::tearDown
和 TestCass::tearDownAfterClass
中那样在每个测试和测试套件之间重置您的Carbon类。
SetUpAutowire
与通过服务容器工作的服务自动注入类似,此扩展允许您根据实现 AutowireInterface
的属性自动注入属性。
请确保在您的PHPUnit配置文件中注册它。
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/> </extensions> </phpunit>
完成后,您的测试需要实现 AutowiredInterface
接口,以便扩展可以挂钩。
namespace App\Tests; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase implements AutowiredInterface { }
仅扩展本身并没有什么作用,您需要在需要自动注入的属性上放置一些属性。
请注意,自动注入系统不适用于静态属性。
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(); } }
这可能看起来有点无意义,但在框架环境中使用服务时将更有意义。《AutowireService》从 draw/tester-bundle 是在Symfony中实现此功能的良好示例。
由于自动注入是在phpunit扩展的 setUp
插件中完成的,因此您不能在测试的设置方法中使用它们。如果您需要在 setUp
方法中访问这些属性,则可以使用 AutowiredCompletionAwareInterface
。
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'); } }
创建自己的自动注入属性
您可以创建自己的属性来自动注入自己的属性。
您只需要创建一个实现 AutowireInterface
接口的属性。
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) ); } }
现在您可以在测试用例中简单地使用它了
namespace App\Tests; use App\Test\PHPUnit\SetUpAutowire\AutowireRandomInt; class MyTest extends KernelTestCase { #[AutowireRandomInt(1, 10)] private int $randomInt; }