Skip to content

A null value can be matched. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions match.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\ExpressionMatcher;
use Coduo\PHPMatcher\Matcher\JsonMatcher;
use Coduo\PHPMatcher\Matcher\NullMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
Expand Down Expand Up @@ -36,6 +37,7 @@ function match($value, $pattern)
new CallbackMatcher(),
new ExpressionMatcher(),
new TypeMatcher(),
new NullMatcher(),
new ScalarMatcher(),
new WildcardMatcher()
));
Expand Down
2 changes: 1 addition & 1 deletion src/Coduo/PHPMatcher/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class JsonMatcher extends Matcher
{
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean)@([^"])/';
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean|null)@([^"])/';
const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3';

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/NullMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use Coduo\ToString\String;

class NullMatcher extends Matcher
{
const MATCH_PATTERN = "/^@null@$/";

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (null !== $value) {
$this->error = sprintf("%s \"%s\" does not match null.", gettype($value), new String($value));
return false;
}

return true;
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern);
}
}
6 changes: 6 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\JsonMatcher;
use Coduo\PHPMatcher\Matcher\NullMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
Expand All @@ -20,6 +21,7 @@ public function setUp()
$scalarMatchers = new ChainMatcher(array(
new TypeMatcher(),
new ScalarMatcher(),
new NullMatcher(),
new WildcardMatcher()
));
$this->matcher = new JsonMatcher(new ChainMatcher(array(
Expand Down Expand Up @@ -131,6 +133,10 @@ public static function positiveMatches()
'{"foobar":[1.22, 2, "hello"]}',
'{"foobar":[@double@, @integer@, @string@]}'
),
array(
'{"null":[null]}',
'{"null":[@null@]}'
),
array(
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":["ROLE_USER", "ROLE_DEVELOPER"]}]}',
'{"users":[{"firstName":"Norbert","lastName":"Orzechowicz","roles":"@wildcard@"}]}'
Expand Down
95 changes: 95 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/NullMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Matcher\NullMatcher;

class NullMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider positiveCanMatchData
*/
public function test_positive_can_matches($pattern)
{
$matcher = new NullMatcher();
$this->assertTrue($matcher->canMatch($pattern));
}

/**
* @dataProvider negativeCanMatchData
*/
public function test_negative_can_matches($pattern)
{
$matcher = new NullMatcher();
$this->assertFalse($matcher->canMatch($pattern));
}

/**
* @dataProvider positiveMatchData
*/
public function test_positive_match($value, $pattern)
{
$matcher = new NullMatcher();
$this->assertTrue($matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchData
*/
public function test_negative_match($value, $pattern)
{
$matcher = new NullMatcher();
$this->assertFalse($matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchDescription
*/
public function test_negative_match_description($value, $pattern, $error)
{
$matcher = new NullMatcher();
$matcher->match($value, $pattern);
$this->assertEquals($error, $matcher->getError());
}

public static function positiveCanMatchData()
{
return array(
array("@null@")
);
}

public static function positiveMatchData()
{
return array(
array(null, "@null@"),
);
}

public static function negativeCanMatchData()
{
return array(
array("@null"),
array("null"),
array(0)
);
}

public static function negativeMatchData()
{
return array(
array("null", "@null@"),
array(0, "@null@")
);
}

public static function negativeMatchDescription()
{
return array(
array("test", "@boolean@", "string \"test\" does not match null."),
array(new \stdClass, "@string@", "object \"\\stdClass\" does not match null."),
array(1.1, "@integer@", "double \"1.1\" does not match null."),
array(false, "@double@", "boolean \"false\" does not match null."),
array(1, "@array@", "integer \"1\" does not match null.")
);
}
}