star/structure-assertion
Composer 安装命令:
composer require star/structure-assertion
包简介
PHP Tool to assert array structure using the fluent interface
README 文档
README
PHP Tool for asserting array structures using the fluent interface.
Installation
Using Composer, run composer require --dev star/structure-assertion.
Usage
This library is usefull when you have PHPUnit tests, and you want to assert some nodes of an array.
StructureAssertion::fromArray( [ 'data' => [ 'id' => 11, 'array' => [ 1, 2, 3, ], ], ] ) ->enterObjectNode('data') // Assert the node 'data' exists and is an object ->assertIsSame('id', 11) // Assert the object's property 'id' exists and match the exact value ->enterArrayNode('array') // Assert the node 'data' exists and is an array ->assertCount(3); // Assert the number of item is exactly 3
Construction methods
StructureAssertion::fromArray($array);: Using the given array.StructureAssertion::fromJsonResponse($response);: Build with a Response object that is assumed to have a JSON content.
Assertions methods
By convention, all assertion methods are named assert*.
Under the hood, StructureAssertion uses PHPUnit assertions.
Many of the assertions are named after the PHPUnit's constraint.
You can use your own assertion if a specific one is not defined by using the StructureAssertion::assertCallback() method.
ie.
StructureAssertion::fromArray($data) ->assertCallback('property', function ($value): bool { // When it evaluates to false, the expectation will fail // return true | false });
Navigation methods
To navigate the nodes, you can use the methods that starts with enter*. We assume that the current node is at the correct position.
StructureAssertion::exitNode(): Move the internal pointer to the parent node.StructureAssertion::nextArrayElement(): Shortcut forexitNode()->enterArrayElement($current + 1). Enters the next element at index + 1. Works only on integer indexed element.
Debugging methods
The lib also provide debugging method, in order to know the current position of the cursor.
StructureAssertion::dump($maxDepth = 2, $dumpStrategy): Will dump the current node using the$strategyand up to$maxDepth.StructureAssertion::dumpPath($dumpStrategy): Will dump the current node path using the$strategy.StructureAssertion::dumpKeys($dumpStrategy): Will dump the current node keys using the$strategy.
Contributing
Any contribution is welcome, just propose a Pull request, and we'll look into it.
统计信息
- 总下载量: 179
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-08