Skip to content

Commit 8709d93

Browse files
author
Norbert Orzechowicz
committedJan 25, 2016
Merge pull request #68 from jakzal/phpunit-integration
PHPUnit integration
2 parents 1146696 + 323f70e commit 8709d93

8 files changed

+290
-1
lines changed
 

‎README.md

+31
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,37 @@ Feature: Listing user toys
387387
"""
388388
```
389389

390+
## PHPUnit integration
391+
392+
The `assertMatchesPattern()` is a handy assertion that matches values in PHPUnit tests.
393+
To use it either include the `Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions` trait,
394+
or extend the `Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase`:
395+
396+
```php
397+
namespace Coduo\PHPMatcher\Tests\PHPUnit;
398+
399+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
400+
401+
class PHPMatcherAssertionsTest extends \PHPUnit_Framework_TestCase
402+
{
403+
use PHPMatcherAssertions;
404+
405+
public function test_it_asserts_if_a_value_matches_the_pattern()
406+
{
407+
$this->assertMatchesPattern('@string@', 'foo');
408+
}
409+
}
410+
```
411+
412+
The `matchesPattern()` method can be used in PHPUnit stubs or mocks:
413+
414+
```php
415+
$mock = $this->getMock(Foo::class);
416+
$mock->method('bar')
417+
->with($this->matchesPattern('@string@'))
418+
->willReturn('foo');
419+
```
420+
390421
## License
391422

392423
This library is distributed under the MIT license. Please see the LICENSE file.

‎phpunit.xml.dist

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
<testsuites>
77
<testsuite name="PHP Matcher Test Suite">
88
<directory>./tests/</directory>
9+
<exclude>tests/PHPUnit/PHPMatcherAssertionsTest.php</exclude>
10+
<file phpVersion="5.4.0" phpVersionOperator=">=">tests/PHPUnit/PHPMatcherAssertionsTest.php</file>
911
</testsuite>
1012
</testsuites>
1113
<filter>
1214
<whitelist>
13-
<directory>./src/Coduo/PHPMatcher/</directory>
15+
<directory>./src/</directory>
1416
</whitelist>
1517
</filter>
1618
</phpunit>

‎src/PHPUnit/PHPMatcherAssertions.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\PHPUnit;
4+
5+
trait PHPMatcherAssertions
6+
{
7+
/**
8+
* @param string $pattern
9+
* @param mixed $value
10+
* @param string $message
11+
*/
12+
protected function assertMatchesPattern($pattern, $value, $message = '')
13+
{
14+
\PHPUnit_Framework_TestCase::assertThat($value, self::matchesPattern($pattern), $message);
15+
}
16+
17+
/**
18+
* @param string $pattern
19+
*
20+
* @return PHPMatcherConstraint
21+
*/
22+
protected static function matchesPattern($pattern)
23+
{
24+
return new PHPMatcherConstraint($pattern);
25+
}
26+
}

‎src/PHPUnit/PHPMatcherConstraint.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\PHPUnit;
4+
5+
use Coduo\PHPMatcher\Factory\SimpleFactory;
6+
use Coduo\PHPMatcher\Matcher;
7+
8+
final class PHPMatcherConstraint extends \PHPUnit_Framework_Constraint
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $pattern;
14+
15+
/**
16+
* @var Matcher
17+
*/
18+
private $matcher;
19+
20+
/**
21+
* @param string $pattern
22+
*/
23+
public function __construct($pattern)
24+
{
25+
parent::__construct();
26+
27+
$this->pattern = $pattern;
28+
$this->matcher = $this->createMatcher();
29+
}
30+
31+
/**
32+
* @return string
33+
*/
34+
public function toString()
35+
{
36+
return 'matches the pattern';
37+
}
38+
39+
/**
40+
* @param mixed $other
41+
*
42+
* @return null|string
43+
*/
44+
protected function additionalFailureDescription($other)
45+
{
46+
return $this->matcher->getError();
47+
}
48+
49+
/**
50+
* @param mixed $value
51+
*
52+
* @return bool
53+
*/
54+
protected function matches($value)
55+
{
56+
return $this->matcher->match($value, $this->pattern);
57+
}
58+
59+
/**
60+
* @return Matcher
61+
*/
62+
private function createMatcher()
63+
{
64+
$factory = new SimpleFactory();
65+
66+
return $factory->createMatcher();
67+
}
68+
}

‎src/PHPUnit/PHPMatcherTestCase.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\PHPUnit;
4+
5+
abstract class PHPMatcherTestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @param string $pattern
9+
* @param mixed $value
10+
* @param string $message
11+
*/
12+
protected function assertMatchesPattern($pattern, $value, $message = '')
13+
{
14+
$this->assertThat($value, self::matchesPattern($pattern), $message);
15+
}
16+
17+
/**
18+
* @param string $pattern
19+
*
20+
* @return PHPMatcherConstraint
21+
*/
22+
protected static function matchesPattern($pattern)
23+
{
24+
return new PHPMatcherConstraint($pattern);
25+
}
26+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\PHPUnit;
4+
5+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
6+
7+
class PHPMatcherAssertionsTest extends \PHPUnit_Framework_TestCase
8+
{
9+
use PHPMatcherAssertions;
10+
11+
public function test_it_asserts_if_a_value_matches_the_pattern()
12+
{
13+
$this->assertMatchesPattern('@string@', 'foo');
14+
}
15+
16+
/**
17+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
18+
* @expectedExceptionMessage Failed asserting that '{"foo":"bar"}' matches the pattern
19+
*/
20+
public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern()
21+
{
22+
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(array('foo' => 'bar')));
23+
}
24+
25+
/**
26+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
27+
* @expectedExceptionMessage Failed asserting that 42 matches the pattern.
28+
*/
29+
public function test_it_creates_a_constraint_for_stubs()
30+
{
31+
$mock = $this->getMockBuilder('stdClass')
32+
->setMethods(array('getTitle'))
33+
->getMock();
34+
35+
$mock->method('getTitle')
36+
->with($this->matchesPattern('@string@'))
37+
->willReturn('foo');
38+
39+
$mock->getTitle(42);
40+
}
41+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\PHPUnit;
4+
5+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;
6+
7+
class PHPMatcherConstraintTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function test_it_is_a_phpunit_constraint()
10+
{
11+
$this->assertInstanceOf('PHPUnit_Framework_Constraint', new PHPMatcherConstraint('@string@'));
12+
}
13+
14+
public function test_it_returns_true_if_a_value_matches_the_pattern()
15+
{
16+
$constraint = new PHPMatcherConstraint('@string@');
17+
18+
$this->assertTrue($constraint->evaluate('foo', '', true));
19+
}
20+
21+
public function test_it_returns_false_if_a_value_does_not_match_the_pattern()
22+
{
23+
$constraint = new PHPMatcherConstraint('@string@');
24+
25+
$this->assertFalse($constraint->evaluate(42, '', true));
26+
}
27+
28+
public function test_it_returns_false_if_a_pattern_is_not_a_string()
29+
{
30+
$constraint = new PHPMatcherConstraint(new \stdClass());
31+
32+
$this->assertFalse($constraint->evaluate('foo', '', true));
33+
}
34+
35+
/**
36+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
37+
* @expectedExceptionMessage Failed asserting that 42 matches the pattern
38+
*/
39+
public function test_it_sets_a_failure_description_if_not_given()
40+
{
41+
$constraint = new PHPMatcherConstraint('@string@');
42+
43+
$this->assertFalse($constraint->evaluate(42));
44+
}
45+
46+
/**
47+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
48+
* @expectedExceptionMessage integer "42" is not a valid string
49+
*/
50+
public function test_it_sets_additional_failure_description()
51+
{
52+
$constraint = new PHPMatcherConstraint('@string@');
53+
54+
$this->assertFalse($constraint->evaluate(42));
55+
}
56+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\PHPUnit;
4+
5+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase;
6+
7+
class PHPMatcherTestCaseTest extends PHPMatcherTestCase
8+
{
9+
public function test_it_asserts_if_a_value_matches_the_pattern()
10+
{
11+
$this->assertMatchesPattern('@string@', 'foo');
12+
}
13+
14+
/**
15+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
16+
* @expectedExceptionMessage Failed asserting that '{"foo":"bar"}' matches the pattern
17+
*/
18+
public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern()
19+
{
20+
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(array('foo' => 'bar')));
21+
}
22+
23+
/**
24+
* @expectedException \PHPUnit_Framework_ExpectationFailedException
25+
* @expectedExceptionMessage Failed asserting that 42 matches the pattern.
26+
*/
27+
public function test_it_creates_a_constraint_for_stubs()
28+
{
29+
$mock = $this->getMockBuilder('stdClass')
30+
->setMethods(array('getTitle'))
31+
->getMock();
32+
33+
$mock->method('getTitle')
34+
->with($this->matchesPattern('@string@'))
35+
->willReturn('foo');
36+
37+
$mock->getTitle(42);
38+
}
39+
}

0 commit comments

Comments
 (0)