beotie / lib_test
此软件包提供了一组类和特质,用于在测试用例中使用
dev-master / 1.0.x-dev
2018-02-01 15:37 UTC
Requires
- phpunit/phpunit: ^6.3@stable
Requires (Dev)
- phpmd/phpmd: ^2.6@stable
- phpmetrics/phpmetrics: ^2.2@stable
- sebastian/phpcpd: ^3.0@stable
- squizlabs/php_codesniffer: ^3.0@stable
This package is not auto-updated.
Last update: 2024-09-18 05:30:43 UTC
README
此库被Beotie项目用作某些功能的基础包装器。首要目标是能够在特质中创建测试,而不是直接在测试用例中。
实现
此库包含两个特质,因此只需将这些特质用于测试即可。
class MyTestCase extends TestCase { use Beotie\LibTest\Traits\TestCaseTrait, MyFeatureTest; protected function getTestedInstance() : string { return MyAwesomeClass::class; } } trait MyFeatureTest { use Beotie\LibTest\Traits\TestTrait; public function myTest() { $this->getTestCase()->assertTrue(true); } }
Beotie\LibTest\Traits\TestCaseTrait
实现了一个 getTestCase()
方法,该方法返回当前的 TestCase
实例。需要实现 getTestedInstance()
方法,以便能够使用 Beotie\LibTest\Traits\TestTrait
,因为它定义了一个抽象。
TestTrait
方法
createEmptyInstance
为了进行完全抽象的单元测试,TestTrait
提供了一个 createEmptyInstance()
方法,该方法返回一个新实例的已定义测试实例,而不调用构造函数。
trait MyFeatureTest { use Beotie\LibTest\Traits\TestTrait; public function myTest() { $instanceToTest = $this->createEmptyInstance(); $this->getTestCase()->assertTrue($instanceToTest->isEmpty()); } }
属性获取器和设置器
Beotie\LibTest\Traits\TestTrait
允许您自动提取和注入实例属性中的值。如果该属性不是直接在测试类中,它将遵循继承树以找到属性位置,并在受保护或私有时使其可访问。
trait MyFeatureTest { use Beotie\LibTest\Traits\TestTrait; public function myTest() { $instanceToTest = $this->createEmptyInstance(); $this->setValue($instance, 'propertyName', 'valueToInject'); $this->getTestCase()->assertFalse($instanceToTest->isEmpty()); $instanceToTest->setPropertyName('otherValue'); $this->getTestCase()->assertEquals('otherValue', $this->getValue($instance, 'propertyName')); } }
TestCase 包装
Beotie\LibTest\Traits\TestTrait
将方法调用转发到存储的 TestCase,因此在 TestTrait
内可以直接调用 TestCase
方法。
trait MyFeatureTest { use Beotie\LibTest\Traits\TestTrait; public function myTest() { $instanceToTest = $this->createEmptyInstance(); $this->setValue($instance, 'propertyName', 'valueToInject'); $this->assertFalse($instanceToTest->isEmpty()); $instanceToTest->setPropertyName('otherValue'); $this->assertEquals('otherValue', $this->getValue($instance, 'propertyName')); } }