Skip to content

Commit 07e9509

Browse files
committed
Restored TypeMatcher to keep BC and marked as deprecated
1 parent 41d01f6 commit 07e9509

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
/**
8+
* @deprecated since 1.1, to be removed in 2.0. Use StringMatcher, NumberMatcher, IntegerMatcher,
9+
* BooleanMatcher, ArrayMatcher instead.
10+
*/
11+
class TypeMatcher extends Matcher
12+
{
13+
const MATCH_PATTERN = "/^@(string|integer|boolean|double|array)@$/";
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function match($value, $pattern)
19+
{
20+
if (gettype($value) !== $this->extractType($pattern)) {
21+
$this->error = sprintf("%s \"%s\" does not match %s pattern.", gettype($value), new String($value), $pattern);
22+
return false;
23+
}
24+
25+
return true;
26+
}
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function canMatch($pattern)
32+
{
33+
return is_string($pattern) && 0 !== preg_match(self::MATCH_PATTERN, $pattern);
34+
}
35+
36+
37+
private function extractType($pattern)
38+
{
39+
return str_replace("@", "", $pattern);
40+
}
41+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
namespace Coduo\PHPMatcher\Tests\Matcher;
3+
4+
use Coduo\PHPMatcher\Matcher\TypeMatcher;
5+
6+
class TypeMatcherTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @dataProvider positiveCanMatchData
10+
*/
11+
public function test_positive_can_matches($pattern)
12+
{
13+
$matcher = new TypeMatcher();
14+
$this->assertTrue($matcher->canMatch($pattern));
15+
}
16+
17+
/**
18+
* @dataProvider negativeCanMatchData
19+
*/
20+
public function test_negative_can_matches($pattern)
21+
{
22+
$matcher = new TypeMatcher();
23+
$this->assertFalse($matcher->canMatch($pattern));
24+
}
25+
26+
/**
27+
* @dataProvider positiveMatchData
28+
*/
29+
public function test_positive_match($value, $pattern)
30+
{
31+
$matcher = new TypeMatcher();
32+
$this->assertTrue($matcher->match($value, $pattern));
33+
}
34+
35+
/**
36+
* @dataProvider negativeMatchData
37+
*/
38+
public function test_negative_match($value, $pattern)
39+
{
40+
$matcher = new TypeMatcher();
41+
$this->assertFalse($matcher->match($value, $pattern));
42+
}
43+
44+
/**
45+
* @dataProvider negativeMatchDescription
46+
*/
47+
public function test_negative_match_description($value, $pattern, $error)
48+
{
49+
$matcher = new TypeMatcher();
50+
$matcher->match($value, $pattern);
51+
$this->assertEquals($error, $matcher->getError());
52+
}
53+
54+
public static function positiveCanMatchData()
55+
{
56+
return array(
57+
array("@integer@"),
58+
array("@string@"),
59+
array("@boolean@"),
60+
array("@double@"),
61+
array("@array@")
62+
);
63+
}
64+
65+
public static function positiveMatchData()
66+
{
67+
return array(
68+
array(false, "@boolean@"),
69+
array("Norbert", "@string@"),
70+
array(1, "@integer@"),
71+
array(6.66, "@double@"),
72+
array(array('test'), '@array@')
73+
);
74+
}
75+
76+
public static function negativeCanMatchData()
77+
{
78+
return array(
79+
array("@integer"),
80+
array("qweqwe"),
81+
array(1),
82+
array("@string"),
83+
array(new \stdClass),
84+
array(array("foobar"))
85+
);
86+
}
87+
88+
public static function negativeMatchData()
89+
{
90+
return array(
91+
array("test", "@boolean@"),
92+
array(new \stdClass, "@string@"),
93+
array(1.1, "@integer@"),
94+
array(false, "@double@"),
95+
array(1, "@array@")
96+
);
97+
}
98+
99+
public static function negativeMatchDescription()
100+
{
101+
return array(
102+
array("test", "@boolean@", "string \"test\" does not match @boolean@ pattern."),
103+
array(new \stdClass, "@string@", "object \"\\stdClass\" does not match @string@ pattern."),
104+
array(1.1, "@integer@", "double \"1.1\" does not match @integer@ pattern."),
105+
array(false, "@double@", "boolean \"false\" does not match @double@ pattern."),
106+
array(1, "@array@", "integer \"1\" does not match @array@ pattern.")
107+
);
108+
}
109+
}

0 commit comments

Comments
 (0)