Giter Club home page Giter Club logo

php-matcher's Introduction

#PHP Matcher

PHP Matcher lets You assert like a gangster in Your test cases, where response can be something you cannot predict

Build Status

##Installation

Add to your composer.json

require: {
   "coduo/php-matcher": "1.0.*"
}

Ways of testing

Common way of testing api responses with Symfony2 WebTestCase

public function testGetToys()
{
    $this->setUpFixtures();
    $this->setUpOath();
    $this->client->request('GET', '/api/toys');
    $response = $this->client->getResponse();
    $this->assertJsonResponse($response, 200);
    $content = $response->getContent();
    $decoded = json_decode($content, true);
    $this->assertTrue(isset($decoded[0]['id']));
    $this->assertTrue(isset($decoded[1]['id']));
    $this->assertTrue(isset($decoded[2]['id']));
    $this->assertEquals($decoded[0]['name'], 'Barbie'));
    $this->assertEquals($decoded[1]['name'], 'GI Joe'));
    $this->assertEquals($decoded[2]['name'], 'Optimus Prime'));
}

With php-matcher, you can make it more readable:

public function testGetToys()
{
    $this->setUpFixtures();
    $this->setUpOath();
    $this->client->request('GET', '/api/toys');
    $response = $this->client->getResponse();
    $this->assertJsonResponse($response, 200);
    $content = $response->getContent();
    $pattern = '[
        {
          "id": @string@,
          "name": "Barbie",
          "_links: @*@
        },
        {
          "id": @string@,
          "name": "GI Joe",
          "_links": @*@
        },
        {
          "id": @string@,
          "name": "Optimus Prime",
          "_links": @*@
        }
      ]
   ';
   $this->assertTrue(match($content, $pattern));
}

From now you should be able to use global function match($value, $pattern)

Available patterns

  • @string@
  • @integer@
  • @boolean@
  • @array@
  • @...@ - unbounded array
  • @double@
  • @null@
  • @*@ || @wildcard@
  • expr(expression)

##Example usage

Scalar matching

<?php

match(1, 1);
match('string', 'string')

Type matching

<?php

match(1, '@integer@');
match('Norbert', '@string@');
match(array('foo', 'bar'), '@array@');
match(12.4, '@double@');
match(true, '@boolean@');

Wildcard

<?php

match(1, '@*@');
match(new \stdClass(), '@wildcard@');

Expression matching

<?php

match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01'");
match("Norbert", "expr(value === 'Norbert')");

Array matching

<?php

match(
   array(
      'users' => array(
          array(
              'id' => 1,
              'firstName' => 'Norbert',
              'lastName' => 'Orzechowicz',
              'roles' => array('ROLE_USER')
          ),
          array(
              'id' => 2,
              'firstName' => 'Michał',
              'lastName' => 'Dąbrowski',
              'roles' => array('ROLE_USER')
          ),
          array(
              'id' => 3,
              'firstName' => 'Johnny',
              'lastName' => 'DąbrowsBravoki',
              'roles' => array('ROLE_HANDSOME_GUY')
          )
      ),
      true,
      6.66
  ),
   array(
      'users' => array(
          array(
              'id' => '@integer@',
              'firstName' => '@string@',
              'lastName' => 'Orzechowicz',
              'roles' => '@array@'
          ),
          array(
              'id' => '@integer@'
              'firstName' => '@string@',
              'lastName' => 'Dąbrowski',
              'roles' => '@array@'
          ),
          '@...@'
      ),
      '@boolean@',
      '@double@'
  )  
)

Json matching

<?php

match(
  '{
    "users":[
      {
        "firstName": "Norbert",
        "lastName": "Orzechowicz",
        "roles":["ROLE_USER", "ROLE_DEVELOPER"]}
      ]
  }',
  '{
    "users":[
      {
        "firstName": @string@,
        "lastName": @string@,
        "roles": @array@
      }
    ]
  }'
)

Example scenario for api in behat using mongo.

@profile, @user
Feature: Listing user toys

  As a user
  I want to list my toys

  Background:
    Given I send and accept JSON

  Scenario: Listing toys
    Given the following users exist:
      | firstName     | lastName     |
      | Chuck         | Norris       | 

    And the following toys user "Chuck Norris" exist:
      | name            |
      | Barbie          |
      | GI Joe          |
      | Optimus Prime   |

    When I set valid authorization code oauth header for user "Chuck Norris"
    And I send a GET request on "/api/toys"
    Then the response status code should be 200
    And the JSON response should match:
    """
      [
        {
          "id": @string@,
          "name": "Barbie",
          "_links: "@*@"
        },
        {
          "id": @string@,
          "name": "GI Joe",
          "_links": "@*@"
        },
        {
          "id": @string@,
          "name": "Optimus Prime",
          "_links": "@*@"
        }
      ]
    """

License

This library is distributed under the MIT license. Please see the LICENSE file.

Credits

This lib was inspired by JSON Expressions gem && Behat RestExtension

php-matcher's People

Contributors

defrag avatar drymek avatar k-phoen avatar leettastic avatar mmoreram avatar norberttech avatar pskt avatar robinvdvleuten avatar

Watchers

 avatar

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.