PHP Tool for asserting array structures using the fluent interface.
Using Composer, run composer require --dev star/structure-assertion.
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 3StructureAssertion::fromArray($array);: Using the given array.StructureAssertion::fromJsonResponse($response);: Build with a Response object that is assumed to have a JSON content.
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
});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.
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.
Any contribution is welcome, just propose a Pull request, and we'll look into it.