fesor / json_matcher Goto Github PK
View Code? Open in Web Editor NEWLibrary for simplifying data verification in functional tests for your JSON-based APIs
Library for simplifying data verification in functional tests for your JSON-based APIs
Now matchers returns bool values, so user should handle assertions something like
assert($my($response)->equal($json));
Also this will allow to use matchers chaining:
$my($response)
->includes($user1, ['at' => 'friends'])
->notIncludes($user2, ['at' => 'friends'])
->haveSize(2, ['at' => 'friends'])
->equal($user1Id, ['at' => 'friends/0/id']
;
Given we have json:
{
"foo": 1,
"bar": 2,
"baz": 3
}
Matchers should allow to check that json includes subsets:
{"baz": 3, "foo": 1}
Json Schema is very powerful tool to verify JSON responses. But it not so user friendly and very verbose. Custom schema definitions and refs makes life a little bit easier but not so much.
I like the way type declarations made in RAML 1.0. It has a lot of sugar, it allows users to define user types, extending primitives. So we need to implement something like this:
// register custom type
JsonMatcher\Types::register('email', [
'pattern' => '^.+\@\S+\.\S+'
]);
// or register multiple types:
JsonMatcher\Types::register([
'integer' => [
'type' => 'number',
'multipleOf' => 1.0
],
'positiveInteger' => [
'type' => 'integer',
'minimum' => 0,
'exclusiveMinimum' => true
],
'UserRegisterRequest' => [
'properties' => [
'email' => 'email',
'password' => [
'minLength' => 4
],
'first_name?' => 'string',
'last_name?' => 'string'
]
],
]);
And then we can just use it:
$json->matches('UserRegisterRequest');
// or we can use inline type declarations
$json->matches([
'type' => 'UserRegisterRequest',
'properties' => [
'foo' => 'string'
]
]);
Type expressions are fully stolen from RAML:
Type expressions provide a powerful way of referring to, and even defining, types. Type expressions can be used wherever a type is expected. The simplest type expression is just the name of a type. Using type expressions, you can devise type unions, arrays, maps, and other things.
Expression Description Person
The simplest type expression: A single type Person[]
An array of Person objects string[]
An array of string scalars string[][]
A bi-dimensional array of string scalars `string Person` `(string Person)[]`
Instead of exceptions json matcher should use error list. This should simplify integration within testing frameworks. Related to #8
There should be ability to match JSON with file contents. For example
$jsonResponse
->equalFile('my.json')
->includesFile('partial.json', ['at' => 'collection'])
;
Also files_dir
options should be provided in matcher constructor.
For now all failed assertion just throws ugly exceptions. Some testing frameworks required special assertion mechanism to be used.
It will be cool to have some API for defining own assertion result.
Sometime it is useful to check just several properties in some large JSON. We already have excluding
and including
options for this purposes, but we can also do something like:
$jsonResponse
->equal('{
"foo": ["foo"],
"bar": "bar"
}', ['excluding' => 'all', 'except' => ['foo', 'bar'])
;
and so on
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.