Skip to content

Commit d3c031a

Browse files
authored
Testsuite cleanups (#168)
1 parent af948f0 commit d3c031a

File tree

5 files changed

+331
-283
lines changed

5 files changed

+331
-283
lines changed

src/PHPUnit/PHPMatcherConstraint.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ final class PHPMatcherConstraint extends Constraint
1818

1919
public function __construct($pattern)
2020
{
21-
if (!\in_array(\gettype($pattern), ['string', 'array'])) {
22-
throw new \LogicException(\sprintf('The PHPMatcherConstraint pattern must be a string or an array, %s given.', \gettype($pattern)));
21+
if (!\in_array(\gettype($pattern), ['string', 'array', 'object'])) {
22+
throw new \LogicException(\sprintf('The PHPMatcherConstraint pattern must be a string, closure or an array, %s given.', \gettype($pattern)));
2323
}
24+
25+
if (\is_object($pattern) && !\is_callable($pattern)) {
26+
throw new \LogicException(\sprintf('The PHPMatcherConstraint pattern must be a string, closure or an array, %s given.', \gettype($pattern)));
27+
}
28+
2429
if (\method_exists(Constraint::class, '__construct')) {
2530
parent::__construct();
2631
}
@@ -61,6 +66,7 @@ protected function fail($other, $description, ComparisonFailure $comparisonFailu
6166
{
6267
if (null === $comparisonFailure
6368
&& \is_string($other)
69+
&& \is_string($this->pattern)
6470
&& \class_exists(Json::class)
6571
) {
6672
list($error) = Json::canonicalize($other);

tests/EmptyPatternsTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests;
6+
7+
use Coduo\PHPMatcher\Factory\SimpleFactory;
8+
use Coduo\PHPMatcher\Matcher;
9+
use Coduo\PHPMatcher\PHPMatcher;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class EmptyPatternsTest extends TestCase
13+
{
14+
/**
15+
* @var Matcher
16+
*/
17+
protected $matcher;
18+
19+
public function setUp() : void
20+
{
21+
$factory = new SimpleFactory();
22+
$this->matcher = $factory->createMatcher();
23+
}
24+
25+
/**
26+
* @dataProvider emptyPatternString
27+
*/
28+
public function test_empty_pattern_in_the_json($value, $pattern, $expectedResult)
29+
{
30+
$match = $this->matcher->match($value, $pattern);
31+
$this->assertSame($expectedResult, $match);
32+
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
33+
}
34+
35+
public static function emptyPatternString()
36+
{
37+
return [
38+
[
39+
'', '', true,
40+
'123', '', false,
41+
' ', '', false,
42+
null, '', false,
43+
1, '', false,
44+
0, '', false,
45+
'{"name": "123"}', '{"name": ""}', false,
46+
'{"name": ""}', '{"name": ""}', true,
47+
],
48+
];
49+
}
50+
}

tests/ExpandersTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests;
6+
7+
use Coduo\PHPMatcher\Factory\SimpleFactory;
8+
use Coduo\PHPMatcher\Matcher;
9+
use Coduo\PHPMatcher\PHPMatcher;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class ExpandersTest extends TestCase
13+
{
14+
/**
15+
* @var Matcher
16+
*/
17+
protected $matcher;
18+
19+
public function setUp() : void
20+
{
21+
$factory = new SimpleFactory();
22+
$this->matcher = $factory->createMatcher();
23+
}
24+
25+
/**
26+
* @dataProvider expanderExamples()
27+
*/
28+
public function test_expanders($value, $pattern, $expectedResult)
29+
{
30+
$this->assertSame($expectedResult, $this->matcher->match($value, $pattern));
31+
$this->assertSame($expectedResult, PHPMatcher::match($value, $pattern));
32+
}
33+
34+
public static function expanderExamples()
35+
{
36+
return [
37+
['lorem ipsum', '@[email protected]("lorem")', true],
38+
['lorem ipsum', '@[email protected]("LOREM", true)', true],
39+
['lorem ipsum', '@[email protected]("ipsum")', true],
40+
['lorem ipsum', '@[email protected]("IPSUM", true)', true],
41+
['lorem ipsum', '@[email protected]("lorem")', true],
42+
43+
['lorem ipsum', '@[email protected]()', false],
44+
['http://coduo.pl/', '@[email protected]()', true],
45+
['lorem ipsum', '@[email protected]()', false],
46+
['2014-08-19', '@[email protected]()', true],
47+
['3014-08-19', '@[email protected]("today")', false],
48+
['1014-08-19', '@[email protected]("+ 1day")', true],
49+
['3014-08-19', '@[email protected]("today")', true],
50+
['1014-08-19', '@[email protected]("+ 1day")', false],
51+
[100, '@[email protected](101).greaterThan(10)', true],
52+
['', '@[email protected]()', false],
53+
['lorem ipsum', '@[email protected]()', true],
54+
['', '@[email protected]()', true],
55+
[['foo', 'bar'], '@[email protected]("bar")', true],
56+
[[], '@[email protected]()', true],
57+
[['foo'], '@[email protected]()', false],
58+
[[1, 2, 3], '@[email protected](3)', true],
59+
[[1, 2, 3], '@[email protected](4)', false],
60+
['lorem ipsum', '@[email protected](contains("lorem"), contains("test"))', true],
61+
['lorem ipsum', '@[email protected](contains("lorem"), contains("test")).endsWith("ipsum")', true],
62+
['lorem ipsum', '@[email protected]("/^lorem \\w+$/")', true],
63+
['lorem ipsum', '@[email protected]("/^foo/")', false],
64+
[[], ['unexistent_key' => '@[email protected]()'], true],
65+
[[], ['unexistent_key' => '@[email protected]()'], true],
66+
[[], ['unexistent_key' => '@[email protected]()'], true],
67+
[[], ['unexistent_key' => '@[email protected]()'], true],
68+
[[], ['unexistent_key' => '@[email protected]()'], true],
69+
[[], ['unexistent_key' => '@[email protected]()'], true],
70+
[[], ['unexistent_key' => '@[email protected]()'], true],
71+
[[], ['unexistent_key' => '@[email protected]()'], true],
72+
[[], ['unexistent_key' => '@[email protected]()'], true],
73+
[[], ['unexistent_key' => '@[email protected]()'], true],
74+
[[], ['unexistent_key' => '@[email protected]()'], true],
75+
[['Norbert', 'Michał'], '@[email protected]("@string@")', true],
76+
['127.0.0.1', '@[email protected]()', true],
77+
['2001:0db8:0000:42a1:0000:0000:ab1c:0001', '@[email protected]()', true],
78+
['127.255.999.999', '@[email protected]()', false],
79+
['foo:bar:42:42', '@[email protected]()', false],
80+
];
81+
}
82+
}

0 commit comments

Comments
 (0)