dev-master
9999999-dev
The Requires
- php >=5.4
The Development Requires
1.0.0
1.0.0.0
The Requires
- php >=5.4
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Native PHP assert for testing REST API JSON response, (*2)
Include a dependency in your composer.json file:, (*3)
{
"require": {
"reenexe/compare-data-structure": "1.0.0"
}
}
You have REST API method that return response like:, (*4)
{
"id": 3,
"name": "Alex",
"location": 3,
"gender": "men",
"joined": {
"source": 1,
"at": "2011-11-11 11:11:11"
},
"friends": [
{
"id": 7,
"name": "Alice"
},
{
"id": 8,
"name": "Bob"
}
],
"interests": ["programming", "books", "sport"],
"games": null,
"books": [
{
"author": "Достоевский Фёдор Михайлович",
"title": "Преступление и наказание"
},
{
"author": "Steve McConnell",
"title": "Code Complete"
}
],
"social": ["GitHub", "LinkedIn"]
}
And we want to check these structures through the settings:, (*5)
[
'assoc' => [
'id' => 'integer',
'name' => 'string',
'location' => 'integer',
'gender' => [
'set' => ['men', 'women', null]
],
'joined' => [
'assoc' => [
'source' => 'integer|null',
'at' => 'string'
]
],
'friends' => [
'type' => 'null',
'values' => [
'id' => 'integer',
'name' => 'string'
]
],
'interests' => [
'type' => 'null',
'values' => 'string'
],
'games' => [
'type' => 'null',
'values' => [
'title' => 'string'
]
],
'books' => [
'values' => [
'author' => 'string',
'title' => 'string',
]
],
'social' => [
'set' => [
'GitHub', 'LinkedIn', 'Facebook', 'Google', 'Twitter',
]
]
]
]
In this example, tried to describe the full scope, (*6)
We can test the simple types, (*7)
1
```php 'integer', (*8)
or
```php
array_merge(
range(1, 100),
range('a', 'z'),
[true, false]
)
```php ['values' => 'integer|string|boolean'], (*9)
Use: ------------ ```php /** * @return StructureDiffInfo */ Comparator::check($data, $structure) /** * @method public bool StructureDiffInfo::isEqual * @method public string StructureDiffInfo::getMessage * @method public string StructureDiffInfo::getPath */
in PHPUnit easy:, (*10)
use ReenExe\CompareDataStructure\Comparator;
class Test extends PHPUnit_Framework_TestCase{
public function testSimple()
{
$diff = Comparator::check(1, 'integer');
$this->assertTrue($diff->isEqual(), $diff);
}
}
user types):We can set user-defined types once and use them on:, (*11)
Comparator::addCustom(array $custom)
Example:, (*12)
Comparator::addCustom( [
'profile' => [
'assoc' => [
'id' => 'integer',
'name' => 'string'
]
]
]);
...
Comparator::check($profile, 'profile');
...
Comparator::check($response, 'profile');
...
And also to establish the types of one-time inspection:, (*13)
Comparator::check(
$data = [
'value' => 1,
'next' => [
'value' => 3,
'next' => [
'value' => 5,
'next' => null
]
]
],
$structure = 'link',
$custom = [
'link' => [
'assoc' => [
'value' => 'integer',
'next' => 'link|null'
]
]
]
);
In user types - possibility of recursive checks, (*14)
@TODO or to be continued...Together with the structures of assoc,values, set - there is a desire to add structure range
Adding strict, (*15)
AssertJsonStructure: https://github.com/t4web/AssertJsonStructure
- part of it was used to create, also used idea of checking the structure, (*16)
json-schema: https://github.com/justinrainbow/json-schema, (*17)