Skip to content

Commit 70d8562

Browse files
committed
Update tests
1 parent 6becf62 commit 70d8562

File tree

8 files changed

+43
-31
lines changed

8 files changed

+43
-31
lines changed

Diff for: src/JsonMatcher/Matcher.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public function __construct(PropertyMatcher $matcher)
1515
$this->matcher = $matcher;
1616
}
1717

18-
public function match($matcher, $pattern)
18+
public function match($value, $pattern)
1919
{
20-
return $this->matcher->match($matcher, $pattern);
20+
return $this->matcher->match($value, $pattern);
2121
}
2222

2323
public function canMatch($pattern)
@@ -26,4 +26,4 @@ public function canMatch($pattern)
2626
}
2727

2828

29-
}
29+
}

Diff for: src/JsonMatcher/Matcher/ArrayMatcher.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,51 @@
22

33
namespace JsonMatcher\Matcher;
44

5-
use JsonMatcher\Matcher\PropertyMatcher;
65
use Symfony\Component\PropertyAccess\PropertyAccess;
76
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
7+
88
class ArrayMatcher implements PropertyMatcher
99
{
1010
/**
11-
* @var Matcher\PropertyMatcher
11+
* @var PropertyMatcher
1212
*/
1313
private $propertyMatcher;
1414

15+
private $paths;
16+
1517
public function __construct(PropertyMatcher $propertyMatcher)
1618
{
1719
$this->propertyMatcher = $propertyMatcher;
1820
}
1921

20-
public function match($matcher, $pattern)
22+
public function match($value, $pattern)
2123
{
2224
$accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
2325
$accessorBuilder->enableExceptionOnInvalidIndex();
2426
$accessor = $accessorBuilder->getPropertyAccessor();
2527

26-
$paths = [];
27-
foreach ($matcher as $key => $element) {
28+
$this->paths = [];
29+
foreach ($value as $key => $element) {
2830
$path = sprintf("[%s]", $key);
2931

3032
if (is_array($element)) {
3133
$this->buildPath($element, $path);
3234
continue;
3335
}
3436

35-
$paths[] = $path;
37+
$this->paths[] = $path;
3638
}
3739

38-
foreach ($paths as $path) {
39-
$value = $accessor->getValue($matcher, $path);
40+
foreach ($this->paths as $path) {
41+
$elementValue = $accessor->getValue($value, $path);
4042
try {
4143
$patternValue = $accessor->getValue($pattern, $path);
4244
} catch (NoSuchIndexException $e) {
4345
return false;
4446
}
4547

4648
if ($this->propertyMatcher->canMatch($patternValue)) {
47-
if (false === $this->propertyMatcher->match($value, $patternValue)) {
49+
if (false === $this->propertyMatcher->match($elementValue, $patternValue)) {
4850
return false;
4951
}
5052
}

Diff for: src/JsonMatcher/Matcher/ChainMatcher.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function addMatcher(PropertyMatcher $matcher)
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public function match($matcher, $pattern)
22+
public function match($value, $pattern)
2323
{
24-
foreach ($this->matchers as $m) {
25-
if (false === $m->match($matcher, $pattern)) {
24+
foreach ($this->matchers as $propertyMatcher) {
25+
if (false === $propertyMatcher->match($value, $pattern)) {
2626
return false;
2727
}
2828
}
@@ -43,4 +43,4 @@ public function getName()
4343
{
4444
return 'chain';
4545
}
46-
}
46+
}

Diff for: src/JsonMatcher/Matcher/PropertyMatcher.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
interface PropertyMatcher
66
{
7-
public function match($matcher, $pattern);
7+
public function match($value, $pattern);
88

99
public function canMatch($pattern);
1010

11-
}
11+
}

Diff for: src/JsonMatcher/Matcher/ScalarMatcher.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44

55
class ScalarMatcher implements PropertyMatcher
66
{
7-
87
/**
98
* {@inheritDoc}
109
*/
11-
public function match($matcher, $pattern)
10+
public function match($value, $pattern)
1211
{
13-
return $matcher === $pattern;
12+
return $value === $pattern;
1413
}
1514

1615
public function canMatch($pattern)
1716
{
1817
return is_scalar($pattern);
1918
}
20-
21-
}
19+
}

Diff for: src/JsonMatcher/Matcher/TypeMatcher.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class TypeMatcher implements PropertyMatcher
88
/**
99
* {@inheritDoc}
1010
*/
11-
public function match($matcher, $pattern)
11+
public function match($value, $pattern)
1212
{
13-
return gettype($matcher) === $this->extractType($pattern);
13+
return gettype($value) === $this->extractType($pattern);
1414
}
1515

1616
public function canMatch($pattern)
@@ -23,4 +23,4 @@ private function extractType($pattern)
2323
return str_replace("@", "", $pattern);
2424
}
2525

26-
}
26+
}

Diff for: tests/JsonMatcher/ArrayMatcherTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
99
{
10-
1110
private $simpleArray;
1211

1312
public function setUp()
@@ -36,8 +35,6 @@ public function test_match_arrays()
3635
$chain->addMatcher(new ScalarMatcher());
3736
$matcher = new ArrayMatcher($chain);
3837

39-
40-
4138
$this->assertTrue($matcher->match($this->simpleArray, $this->simpleArray));
4239
$this->assertTrue($matcher->match([], []));
4340
$this->assertFalse($matcher->match($this->simpleArray, []));
@@ -56,6 +53,4 @@ public function test_match_arrays()
5653
]
5754
]));
5855
}
59-
60-
6156
}

Diff for: tests/JsonMatcher/ScalarMatcherTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace JsonMatcher\Tests;
3+
4+
use JsonMatcher\Matcher;
5+
6+
class ScalarMatcherTest extends \PHPUnit_Framework_TestCase
7+
{
8+
public function test_match_scalars()
9+
{
10+
$matcher = new Matcher\ScalarMatcher();
11+
12+
$this->assertTrue($matcher->match(1, 1));
13+
$this->assertTrue($matcher->match("michal", "michal"));
14+
$this->assertFalse($matcher->match(false, "false"));
15+
$this->assertFalse($matcher->match(false, 0));
16+
}
17+
}

0 commit comments

Comments
 (0)