Skip to content

Commit fce3198

Browse files
committed
Replaced PHPMatcher facade with implementation that makes possible to access backtrace
1 parent a93f851 commit fce3198

18 files changed

+172
-104
lines changed

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
Library created for testing all kinds of JSON/XML/TXT/Scalar values against patterns.
44

5+
API:
6+
57
```php
6-
PHPMatcher::match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}');
8+
PHPMatcher::match($value = '{"foo": "bar"}', $pattern = '{"foo": "@string@"}') : bool;
9+
PHPMatcher::backtrace() : Backtrace;
10+
PHPMatcher::error() : ?string;
711
```
812

913
It was built to simplify API's functional testing.
@@ -31,32 +35,56 @@ composer require --dev "coduo/php-matcher"
3135

3236
## Basic usage
3337

34-
### Using facade
38+
### Direct PHPMatcher usage
3539

3640
```php
3741
<?php
3842

3943
use Coduo\PHPMatcher\PHPMatcher;
4044

41-
if (!PHPMatcher::match("lorem ipsum dolor", "@string@", $error)) {
42-
echo $error; // in case of error message is set on $error variable via reference
45+
$matcher = new PHPMatcher();
46+
$match = $matcher->match("lorem ipsum dolor", "@string@");
47+
48+
if (!$match) {
49+
echo "Error: " . $matcher->error();
50+
echo "Backtrace: \n";
51+
echo (string) $matcher->backtrace();
4352
}
53+
```
54+
55+
### PHPUnit extending PHPMatcherTestCase
56+
57+
```php
58+
<?php
59+
60+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase;
4461

62+
class MatcherTest extends PHPMatcherTestCase
63+
{
64+
public function test_matcher_that_value_matches_pattern()
65+
{
66+
$this->assertMatchesPattern('{"name": "Norbert"}', '{"name": "@string@"}');
67+
}
68+
}
4569
```
4670

47-
### Using Factory
71+
### PHPUnit using PHPMatcherAssertions trait
4872

4973
```php
5074
<?php
5175

52-
use Coduo\PHPMatcher\Factory\MatcherFactory;
76+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
77+
use PHPUnit\Framework\TestCase;
5378

54-
$factory = new MatcherFactory();
55-
$matcher = $factory->createMatcher();
79+
class MatcherTest extends TestCase
80+
{
81+
use PHPMatcherAssertions;
5682

57-
$match = $matcher->match("lorem ipsum dolor", "@string@");
58-
// $match === true
59-
$matcher->getError(); // returns null or error message
83+
public function test_matcher_that_value_matches_pattern()
84+
{
85+
$this->assertMatchesPattern('{"name": "Norbert"}', '{"name": "@string@"}');
86+
}
87+
}
6088
```
6189

6290
### Available patterns

UPGRADE.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 3.x -> 4.0
2+
3+
Below you can find list of changes between 3.x and 4.0 versions of PHPMatcher.
4+
5+
### Creating Matcher
6+
7+
```diff
8+
-$factory = new MatcherFactory();
9+
-$this->matcher = $factory->createMatcher();
10+
```
11+
12+
```diff
13+
+$this->matcher = new PHPMatcher();
14+
```
15+
16+
### Simple Matching
17+
18+
```diff
19+
-PHPMatcher::match($value, $pattern, $error)
20+
```
21+
22+
```diff
23+
+$matcher = new PHPMatcher();
24+
+$matcher->match($value, $pattern);
25+
+echo $matcher->error();
26+
```

src/Backtrace.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function expanderFailed(string $name, $value, string $error) : void
104104
);
105105
}
106106

107+
public function isEmpty() : bool
108+
{
109+
return \count($this->trace) === 0;
110+
}
111+
107112
public function __toString() : string
108113
{
109114
return \implode("\n", $this->trace);

src/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Factory
88
{
9-
public function createMatcher(Backtrace $backtrace = null) : Matcher;
9+
public function createMatcher() : Matcher;
1010
}

src/Factory/MatcherFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
final class MatcherFactory implements Factory
1414
{
15-
public function createMatcher(Backtrace $backtrace = null) : Matcher
15+
public function createMatcher() : Matcher
1616
{
17-
$matcherBacktrace = $backtrace ? $backtrace : new Backtrace();
17+
$matcherBacktrace = new Backtrace();
1818

1919
return new Matcher($this->buildMatchers($this->buildParser($matcherBacktrace), $matcherBacktrace), $matcherBacktrace);
2020
}

src/Factory/SimpleFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Coduo\PHPMatcher\Factory;
66

7-
use Coduo\PHPMatcher\Backtrace;
87
use Coduo\PHPMatcher\Factory;
98
use Coduo\PHPMatcher\Matcher;
109

@@ -13,7 +12,7 @@
1312
*/
1413
class SimpleFactory implements Factory
1514
{
16-
public function createMatcher(Backtrace $backtrace = null) : Matcher
15+
public function createMatcher() : Matcher
1716
{
1817
return (new MatcherFactory())->createMatcher();
1918
}

src/PHPMatcher.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,43 @@
88

99
final class PHPMatcher
1010
{
11-
public static function match($value, $pattern, string &$error = null) : bool
11+
private $matcher;
12+
13+
public function match($value, $pattern) : bool
1214
{
13-
$matcher = (new MatcherFactory())->createMatcher();
15+
$this->matcher = null;
1416

15-
if (!$matcher->match($value, $pattern)) {
16-
$error = $matcher->getError();
17-
return false;
18-
}
17+
return $this->getMatcher()->match($value, $pattern);
18+
}
1919

20-
return true;
20+
/**
21+
* Returns backtrace from last matching.
22+
* When called before PHPMatcher::match() function it will return instance where Backtrace::isEmpty() will return true
23+
*
24+
* @return Backtrace
25+
*/
26+
public function backtrace() : Backtrace
27+
{
28+
return $this->getMatcher()->backtrace();
2129
}
2230

23-
public static function matchBacktrace($value, $pattern, Backtrace $backtrace, string &$error = null) : bool
31+
/**
32+
* Returns error from last matching.
33+
* If last matching was successful this function will return null.
34+
*
35+
* @return string|null
36+
*/
37+
public function error() : ?string
2438
{
25-
$matcher = (new MatcherFactory())->createMatcher($backtrace);
39+
return $this->getMatcher()->getError();
40+
}
2641

27-
if (!$matcher->match($value, $pattern)) {
28-
$error = $matcher->getError();
29-
return false;
42+
private function getMatcher() : Matcher
43+
{
44+
if (null === $this->matcher) {
45+
$this->matcher = (new MatcherFactory())->createMatcher();
3046
}
3147

32-
return true;
48+
return $this->matcher;
3349
}
3450
}

src/PHPUnit/PHPMatcherConstraint.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Coduo\PHPMatcher\PHPUnit;
66

7-
use Coduo\PHPMatcher\Backtrace;
8-
use Coduo\PHPMatcher\Factory\MatcherFactory;
9-
use Coduo\PHPMatcher\Matcher;
7+
use Coduo\PHPMatcher\PHPMatcher;
108
use PHPUnit\Framework\Constraint\Constraint;
119
use PHPUnit\Util\Json;
1210
use SebastianBergmann\Comparator\ComparisonFailure;
@@ -15,7 +13,6 @@ final class PHPMatcherConstraint extends Constraint
1513
{
1614
private $pattern;
1715
private $matcher;
18-
private $backtrace;
1916
private $lastValue;
2017

2118
public function __construct($pattern)
@@ -33,33 +30,30 @@ public function __construct($pattern)
3330
}
3431

3532
$this->pattern = $pattern;
36-
$this->backtrace = new Backtrace();
37-
$this->matcher = $this->createMatcher();
33+
$this->matcher = new PHPMatcher();
3834
}
3935

4036
/**
4137
* {@inheritdoc}
4238
*/
4339
public function toString() : string
4440
{
45-
return 'matches the pattern';
41+
return 'matches given pattern.';
4642
}
4743

4844
protected function failureDescription($other): string
4945
{
50-
return parent::failureDescription($other) . ".\nError: " . $this->matcher->getError();
46+
return parent::failureDescription($other)
47+
. "\nPattern: " . $this->exporter()->export($this->pattern)
48+
. "\nError: " . $this->matcher->error()
49+
. "\nBacktrace: \n" . $this->matcher->backtrace();
5150
}
5251

5352
protected function matches($value) : bool
5453
{
5554
return $this->matcher->match($this->lastValue = $value, $this->pattern);
5655
}
5756

58-
private function createMatcher() : Matcher
59-
{
60-
return (new MatcherFactory())->createMatcher($this->backtrace);
61-
}
62-
6357
/**
6458
* {@inheritdoc}
6559
*/

tests/BacktraceTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44

55
namespace Coduo\PHPMatcher\Tests;
66

7-
use Coduo\PHPMatcher\Factory\MatcherFactory;
8-
use Coduo\PHPMatcher\Matcher;
7+
use Coduo\PHPMatcher\PHPMatcher;
98
use PHPUnit\Framework\TestCase;
109

1110
final class BacktraceTest extends TestCase
1211
{
1312
/**
14-
* @var Matcher
13+
* @var PHPMatcher
1514
*/
1615
protected $matcher;
1716

1817
public function setUp() : void
1918
{
20-
$factory = new MatcherFactory();
21-
$this->matcher = $factory->createMatcher();
19+
$this->matcher = new PHPMatcher();
2220
}
2321

2422
public function test_backtrace_in_failed_simple_matching()

tests/EmptyPatternsTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@
44

55
namespace Coduo\PHPMatcher\Tests;
66

7-
use Coduo\PHPMatcher\Factory\MatcherFactory;
8-
use Coduo\PHPMatcher\Matcher;
97
use Coduo\PHPMatcher\PHPMatcher;
108
use PHPUnit\Framework\TestCase;
119

1210
final class EmptyPatternsTest extends TestCase
1311
{
1412
/**
15-
* @var Matcher
13+
* @var PHPMatcher
1614
*/
1715
protected $matcher;
1816

1917
public function setUp() : void
2018
{
21-
$factory = new MatcherFactory();
22-
$this->matcher = $factory->createMatcher();
19+
$this->matcher = new PHPMatcher();
2320
}
2421

2522
/**
@@ -29,7 +26,6 @@ public function test_empty_pattern_in_the_json($value, $pattern, $expectedResult
2926
{
3027
$match = $this->matcher->match($value, $pattern);
3128
$this->assertSame($expectedResult, $match);
32-
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
3329
}
3430

3531
public static function emptyPatternString()

tests/ExpandersTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,27 @@
44

55
namespace Coduo\PHPMatcher\Tests;
66

7-
use Coduo\PHPMatcher\Factory\MatcherFactory;
8-
use Coduo\PHPMatcher\Matcher;
97
use Coduo\PHPMatcher\PHPMatcher;
108
use PHPUnit\Framework\TestCase;
119

1210
final class ExpandersTest extends TestCase
1311
{
1412
/**
15-
* @var Matcher
13+
* @var PHPMatcher
1614
*/
1715
protected $matcher;
1816

1917
public function setUp() : void
2018
{
21-
$factory = new MatcherFactory();
22-
$this->matcher = $factory->createMatcher();
19+
$this->matcher = new PHPMatcher();
2320
}
2421

2522
/**
2623
* @dataProvider expanderExamples()
2724
*/
2825
public function test_expanders($value, $pattern, $expectedResult)
2926
{
30-
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern), (string) $this->matcher->getError());
31-
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
27+
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern), (string) $this->matcher->error());
3228
}
3329

3430
public static function expanderExamples()

tests/Factory/SimpleFactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Coduo\PHPMatcher\Tests;
66

77
use Coduo\PHPMatcher\Factory\SimpleFactory;
8+
use Coduo\PHPMatcher\Matcher;
89
use PHPUnit\Framework\TestCase;
910

1011
class SimpleFactoryTest extends TestCase
1112
{
1213
public function test_creating_matcher()
1314
{
1415
$factory = new SimpleFactory();
15-
$this->assertInstanceOf('Coduo\PHPMatcher\Matcher', $factory->createMatcher());
16+
$this->assertInstanceOf(Matcher::class, $factory->createMatcher());
1617
}
1718
}

0 commit comments

Comments
 (0)