#PHP Matcher
PHP Matcher lets You assert like a gangster in Your test cases, where response can be something you cannot predict
##Installation
Add to your composer.json
require: {
"coduo/php-matcher": "1.1.*"
}
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$match = $matcher->match("lorem ipsum dolor", "@string@")
// $match === true
@string@
@integer@
@number@
@double@
@boolean@
@array@
@...@
- unbounded array@double@
@null@
@*@
||@wildcard@
expr(expression)
startsWith($stringBeginning, $ignoreCase = false)
endsWith($stringEnding, $ignoreCase = false)
contains($string, $ignoreCase = false)
isDateTime()
isEmail()
notEmpty()
lowerThan($boundry)
greaterThan($boundry)
inArray($value)
oneOf(...$expanders)
- example usage"@string@.oneOf(contains('foo'), contains('bar'), contains('baz'))"
##Example usage
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(1, 1);
$matcher->match('string', 'string')
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match('Norbert', '@string@');
$matcher->match("lorem ipsum dolor", "@string@.startsWith('lorem').contains('ipsum').endsWith('dolor')")
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(100, '@integer@');
$matcher->match(100, '@integer@.lowerThan(200).greaterThan(10)');
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(100, '@number@');
$matcher->match('200', '@number@');
$matcher->match(1.25, '@number@');
$matcher->match('1.25', '@number@');
$matcher->match(0b10100111001, '@number@');
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(10.1, "@double@");
$matcher->match(10.1, "@double@.lowerThan(50.12).greaterThan(10)");
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(true, "@boolean@");
$matcher->match(false, "@boolean@");
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match("@integer@", "@*@"),
$matcher->match("foobar", "@*@"),
$matcher->match(true, "@*@"),
$matcher->match(6.66, "@*@"),
$matcher->match(array("bar"), "@wildcard@"),
$matcher->match(new \stdClass, "@wildcard@"),
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01'");
$matcher->match("Norbert", "expr(value === 'Norbert')");
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->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@.greaterThan(0)',
'firstName' => '@string@',
'lastName' => 'Orzechowicz',
'roles' => '@array@'
),
array(
'id' => '@integer@'
'firstName' => '@string@',
'lastName' => 'Dąbrowski',
'roles' => '@array@'
),
'@...@'
),
'@boolean@',
'@double@'
)
)
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(
'{
"users":[
{
"firstName": "Norbert",
"lastName": "Orzechowicz",
"created": "2014-01-01",
"roles":["ROLE_USER", "ROLE_DEVELOPER"]}
]
}',
'{
"users":[
{
"firstName": @string@,
"lastName": @string@,
"created": "@string@.isDateTime()",
"roles": @array@
}
]
}'
)
<?php
use Coduo\PHPMatcher\Factory\SimpleFactory;
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
$matcher->match(<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
<m:StockValue>Any Value</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML
,
<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="@string@"
soap:encodingStyle="@string@">
<soap:Body xmlns:m="@string@">
<m:GetStockPrice>
<m:StockName>@string@</m:StockName>
<m:StockValue>@string@</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML
);
@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": "@*@"
}
]
"""
This library is distributed under the MIT license. Please see the LICENSE file.
This lib was inspired by JSON Expressions gem && Behat RestExtension