Download the PHP package zlob/any-json-tester without Composer
On this page you can find all versions of the php package zlob/any-json-tester. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package any-json-tester
AnyJsonTester

Extension for PhpUnit, that helps you to test JSON or Array with changeable values, like timestamps, count etc. It can be very helpful if you decide to test your API. All you need - is to define data structure with set of helper classes.
Installation
via composer
Example
Lets test JSON like
Test code:
Usage
After install, use AnyJsonTester trait in your test class. It gives you two methods: seeJsonLike to test JSON, and seeArrayLike to test arrays.
Some sugar for Laravel 5.1
In my favorite framework, you can use AnyJsonTesterLaravel trait instead of AnyJsonTester trait, that will automatically retrieves data from response, so you can chain seeJsonLike method with request method, like
Interface
Trait has only two public methods: seeJsonLike and seeArrayLike, that takes as argument AnyObject or AnyArray object (and tested JSON string in case of non-Laravel trait). AnyObject is used to test JSON object like '{"name" : "Zlob"}' and can use additional types described bellow. AnyArray is used to test JSON array of similar objects, such as AnyObject, AnyInteger and etc. or even another AnyArray.
Supported types
AnyObject - help to test JSON objects
Arguments:
- options - array, avaible options are:
- hasFields - array of fields and values, that Object should contain. Instead of explicit value, you may use one of supportd types
- hasNoFields - array of fields, that Object should not contain.
- strictMode - boolean. In strict mode, object can contain only fields, described in hasFields option
- nullable - boolean. If true, object can be null.
Example:
AnyArray - helps to test array
Arguments:
- expectedElement - AnyObject, AnyBoolean, AnyDateTime, AnyFloat, AnyInteger, AnyString or another AnyArray object, that describe content of array items
- options - array, avaible options are:
- min - int, minimum array length
- max - int, maximum array length
- nullable - boolean. If true, array can be null.
Example:
AnyString - helps to test strings
Arguments:
- options - array, avaible options are:
- min - int, minimum string length
- max - int, maximum string length
- regex - string, regex pattern
- enum - array, defines possible string values
- nullable - boolean. If true, string can be null.
Example:
AnyInteger - helps to test integers
Arguments:
- options - array, avaible options are:
- min - int, minimum integer value
- max - int, maximum integer value
- nullable - boolean. If true, integer can be null.
Example:
AnyFloat - helps to test floats
Arguments:
- options - array, avaible options are:
- min - int, minimum float value
- max - int, maximum float value
- precision - int, float precision
- nullable - boolean. If true, float can be null.
Example:
AnyDateTime - helps to test date and time
Arguments:
- options - array, avaible options are:
- min - int, minimum date/time value
- max - int, maximum date/time value
- format - string, DateTime format
- nullable - boolean. If true, value can be null.
Example:
AnyBoolean - helps to test boolean
Arguments:
- options - array, avaible options are:
- strictMode - bool, if true - only 'true' and 'false' strings are boolean. Otherwise, 'true', 'false', '0', '1', 'on', 'off', 'yes' and 'no'.
- nullable - boolean. If true, value can be null.
Example:
License
MIT - Zlob