adgoal / phpunit-entity-tester
快速测试您的实体
Requires
- php: >=7.0
- phpunit/phpunit: >=6
This package is not auto-updated.
Last update: 2024-09-22 16:30:49 UTC
README
您可以为实体的访问器、添加器和移除器快速编写单元测试。
要求
- PHP : 7.0 或更高版本
- phpunit/phpunit : 6 或更高版本
安装
- 使用Composer
composer install adgoal/phpunit-entity-tester
快速示例
这是一个单元测试文件的示例
<?php namespace Demo\Tests; use PhpUnitEntityTester\AccessorTester; use PhpUnitEntityTester\AccessorCollectionTester; class MyEntityTest extends \PHPUnit\Framework\TestCase { public function simpleTest() { $entity = new MyEntity(); // The entity that you want to test // Test 'setName' and 'getName' new AccessorTester($entity, 'name')->test('John Doe'); // Test 'addFruit', 'removeFruit' and 'getFruits' new AccessorCollectionTester($entity, 'fruits')->test('apple', 'pear'); } }
文档
测试访问器
1. 添加 'use' 语句
use PhpUnitEntityTester\AccessorTester;
2. 创建测试对象
$tester = new AccessortTester($entity, 'attribute');
其中 $entity
是要测试的实体,'attribute'
是设置器和获取器方法的基准名称。
在这种情况下,测试器将测试 'setAttribute' 和 'getAttribute' 方法。
3. 配置测试器
您可以通过这种方式更改设置器和获取器方法
$tester->setterMethod('setFoo') //fluent method ->getteMethod('getBar'); // fluent method
您可以将设置器的流畅约束移除(默认设置为 true
)
$tester->fluent(false);
4. 运行测试
只需使用 test
方法即可
$tester->test('the value');
在这种情况下,测试器
- 使用 'the value' 调用设置器
- 测试流畅约束
- 测试获取器返回 'the value'
如果获取器返回的值必须与设置器使用的值不同,则使用 test
方法的第二个参数,如下所示
$tester->test('value for setter', 'value that the getter have to return');
测试设置器
您还可以仅测试设置器方法,如下所示
$tester->testSetter('value');
此方法调用设置器方法并测试流畅约束
测试获取器
您还可以仅测试设置器方法,如下所示
$tester->testGetter('value');
此方法调用获取器方法并测试返回的值参数
测试添加器、移除器和集合获取器
1. 添加 'use' 语句
use PhpUnitEntityTester\AccessorCollectionTester;
2. 创建测试对象
$tester = new AccessorCollectionTester($entity, 'items');
其中 $entity
是要测试的实体,'items'
是添加器、移除器和获取器方法的基准名称。对于添加器和移除方法,基准名称的最后一个 's' 将被删除。
在这种情况下,测试器将测试 'addItem'、'removeItem' 和 'getItems' 方法。
3. 配置测试器
您可以通过这种方式更改添加器、移除器和获取器方法
$tester->addMethod('addCustomItem') // fluent method ->removeMethod('popItem') // fluent method ->getMethod('getAllItems'); // fluent method
您可以将添加器和移除方法的流畅约束移除(默认设置为 true
)
$tester->fluent(false);
默认情况下,测试器认为集合尊重其项的唯一性。您可以强制测试器认为集合不尊重唯一性,如下所示
$tester->unique(false);
4. 运行测试
只需使用 test
方法即可
$tester->test('first value', 'second value');
此方法需要两个参数才能工作。在这种情况下,测试器
- 调用并测试添加器方法与第一个值
- 尝试移除第二个值(尚未在集合中)
- 调用并测试添加器方法与第二个值
- 尝试再次添加第一个值(以测试唯一性约束)
- 尝试移除第一个值
- 再次添加第一个值(以获得包含第一个和第二个值的集合)
此方法使用以下三个方法,您也可以单独使用它们。
测试添加器
您还可以仅测试添加器方法,如下所示
$tester->testAdd('value');
此方法调用添加器方法并测试
- 流畅约束
- 如果值在集合中(已添加)
- 唯一性约束
测试移除器
您还可以仅测试移除器方法,如下所示
$tester->testRemove('value');
此方法调用移除器方法并测试
- 流畅约束
- 如果值不在集合中(已移除)
- 如果移除器移除了正确数量的项(所有匹配的值,但不更多)
测试获取器
您还可以仅测试获取器方法,如下所示
$tester->testGet();
此方法测试获取器方法的返回值是否
- (如果返回是对象)实现
Countable
接口 - (如果返回值是一个对象)实现
Traversable
接口 - (如果返回值不是一个对象)是一个
数组
或空值
技巧
为了更快地测试实体,请使用测试器与 dataProvider
。
- 创建测试方法
/** * @dataProvider providerTestAccessor */ public function testAccessor($attribute, $setValue, $getValue = AccessorTester::USE_SET_DATA) { $entity = new YourEntityClass(); $tester = new AccessorTester($entity, $attribute); $tester->test($setValue, $getValue); }
- 创建数据提供者
public function providerTestAccessor() { return [ ['name', 'John Doe'], ['surname', 'JD'], ['astro', 'lion'], ['age', 12], ['age', -5, 0], ['color', 'red', '#ff0000'], ['createdAt', '2015-12-01', new \Datetime('2015-12-01')], // ... // one line here, generate complete test of your accessor ]; }
您也可以通过自定义测试方法来使用 AccessorCollectionTester
的流程。
致谢
特别感谢 gerg0ire,他鼓励我创建这个库