Giter Site home page Giter Site logo

json_matcher's People

Contributors

fesor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

json_matcher's Issues

Use exceptions on matching

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']
;

Json Schema validation

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

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)[]`

files support

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.

Allow to transform assertions

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.

Exclude all keys except

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'])
;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.