zalas / phpunit-doubles
为您初始化PHPUnit测试用例中的测试替身
v1.9.2
2024-01-22 09:53 UTC
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- phpdocumentor/reflection-docblock: ^5.2
- phpunit/phpunit: ^9.6
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0.1
Conflicts
- phpdocumentor/type-resolver: 1.3.0
- dev-main / 2.0.x-dev
- 1.9.x-dev
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- 1.1.x-dev
- v1.1.0
- 1.0.x-dev
- v1.0.0
- v0.0.2
- v0.0.1
- dev-dependabot/github_actions/dot-github/workflows/actions/download-artifact-4.1.7
- dev-phpunit-10-on-1.9
- dev-phpunit-10
This package is auto-updated.
Last update: 2024-09-04 14:37:36 UTC
README
为您初始化PHPUnit测试用例中的测试替身。
安装
Composer
composer require --dev zalas/phpunit-doubles
Phar
该扩展也作为PHAR分发,可以从最新的Github发行版下载。
将扩展放置在您的PHPUnit扩展目录中。请记住在您的phpunit.xml
中指示PHPUnit加载扩展。
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" extensionsDirectory="tools/phpunit.d" > </phpunit>
使用
包含Zalas\PHPUnit\Doubles\TestCase\ProphecyTestDoubles
或Zalas\PHPUnit\Doubles\TestCase\PHPUnitTestDoubles
特性,以便在支持的测试替身框架中初始化您的测试替身。
测试替身类型和测试替身框架的类型都来自属性类型
/** * @var Vimes|ObjectProphecy */ private $vimes;
目前支持两种测试替身框架
- Prophecy -
Prophecy\Prophecy\ObjectProphecy
类型提示 - PHPUnit -
PHPUnit\Framework\MockObject\MockObject
类型提示
Prophecy
<?php use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Zalas\PHPUnit\Doubles\TestCase\ProphecyTestDoubles; class DiscworldTest extends TestCase { use ProphecyTestDoubles; /** * @var Vimes|ObjectProphecy */ private $vimes; /** * @var Nobby|Fred|ObjectProphecy */ private $nobbyAndFred; public function test_it_hires_new_recruits_for_nightwatch() { $discworld = new Discworld($this->vimes->reveal(), $this->nobbyAndFred->reveal()); $discworld->createNightWatch(); $this->vimes->recruit($this->nobbyAndFred)->shouldHaveBeenCalled(); } }
PHPUnit
<?php use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Zalas\PHPUnit\Doubles\TestCase\PHPUnitTestDoubles; class DiscworldTest extends TestCase { use PHPUnitTestDoubles; /** * @var Vimes|MockObject */ private $vimes; /** * @var Nobby|MockObject */ private $nobbyAndFred; public function test_it_hires_new_recruits_for_nightwatch() { $discworld = new Discworld($this->vimes, $this->nobby); $this->vimes->expects($this->once()) ->method('recruit') ->with($this->nobby); $discworld->createNightWatch(); } }