thenlabs / snapshots-comparator
dev-main
2022-05-19 19:33 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- thenlabs/pyramidal-tests: 2.0.x-dev
This package is auto-updated.
Last update: 2024-09-20 00:22:14 UTC
README
状态数组与预期比较。
如果你喜欢这个项目,给我们一个 ⭐。
安装。
composer require thenlabs/snapshots-comparator dev-main
使用示例。
我们有两个状态变量:
$before = [ 'document1' => [ 'id' => '1', 'title' => 'The document 1', 'customData1' => 'custom data 1', ] ]; $after = [ 'document1' => [ 'id' => '1', 'title' => 'The document one', 'anotherData' => 'another data', ] ];
当我们比较两者(不声明预期)时,我们得到以下结果:
use ThenLabs\SnapshotsComparator\Comparator; $result = Comparator::compare($before, $after); false === $result->isSuccessful(); // becouse there are unexpectations. $result->getUnexpectations() === [ 'CREATED' => [ 'document1' => [ // 'anotherData' was created. 'anotherData' => 'another data', ] ], 'UPDATED' => [ 'document1' => [ // 'title' was updated 'title' => 'The document one', ] ], 'DELETED' => [ 'document1' => [ // 'customData1' was deleted 'customData1' => 'custom data 1', ] ], ];
我们可以看到,我们获得了关于前后差异的所有信息,例如,哪些数据被创建、更新或删除。
所有这些更改都是意外的,因为我们没有声明预期,这就是为什么 $result->isSuccessful()
返回 false
的原因。
在下一个示例中,我们声明所有预期,从而在不出现意外的情况下获得成功的执行结果。
use ThenLabs\SnapshotsComparator\Comparator; use ThenLabs\SnapshotsComparator\ExpectationBuilder; $expectations = new ExpectationBuilder(); $expectations->expectCreated([ 'document1' => [ 'anotherData' => 'another data', ] ]); $expectations->expectUpdated([ 'document1' => [ 'title' => function ($value) { // custom comparator return is_string($value); }, ] ]); $expectations->expectDeleted([ 'document1' => [ 'customData1' => 'custom data 1', ] ]); $result = Comparator::compare($before, $after, $expectations); true === $result->isSuccessful(); [] === $result->getUnexpectations();