Skip to content

PHPUnit integration #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,37 @@ Feature: Listing user toys
"""
```

## PHPUnit integration

The `assertMatchesPattern()` is a handy assertion that matches values in PHPUnit tests.
To use it either include the `Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions` trait,
or extend the `Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase`:

```php
namespace Coduo\PHPMatcher\Tests\PHPUnit;

use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;

class PHPMatcherAssertionsTest extends \PHPUnit_Framework_TestCase
{
use PHPMatcherAssertions;

public function test_it_asserts_if_a_value_matches_the_pattern()
{
$this->assertMatchesPattern('@string@', 'foo');
}
}
```

The `matchesPattern()` method can be used in PHPUnit stubs or mocks:

```php
$mock = $this->getMock(Foo::class);
$mock->method('bar')
->with($this->matchesPattern('@string@'))
->willReturn('foo');
```

## License

This library is distributed under the MIT license. Please see the LICENSE file.
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
<testsuites>
<testsuite name="PHP Matcher Test Suite">
<directory>./tests/</directory>
<exclude>tests/PHPUnit/PHPMatcherAssertionsTest.php</exclude>
<file phpVersion="5.4.0" phpVersionOperator=">=">tests/PHPUnit/PHPMatcherAssertionsTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/Coduo/PHPMatcher/</directory>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
26 changes: 26 additions & 0 deletions src/PHPUnit/PHPMatcherAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Coduo\PHPMatcher\PHPUnit;

trait PHPMatcherAssertions
{
/**
* @param string $pattern
* @param mixed $value
* @param string $message
*/
protected function assertMatchesPattern($pattern, $value, $message = '')
{
\PHPUnit_Framework_TestCase::assertThat($value, self::matchesPattern($pattern), $message);
}

/**
* @param string $pattern
*
* @return PHPMatcherConstraint
*/
protected static function matchesPattern($pattern)
{
return new PHPMatcherConstraint($pattern);
}
}
68 changes: 68 additions & 0 deletions src/PHPUnit/PHPMatcherConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Coduo\PHPMatcher\PHPUnit;

use Coduo\PHPMatcher\Factory\SimpleFactory;
use Coduo\PHPMatcher\Matcher;

final class PHPMatcherConstraint extends \PHPUnit_Framework_Constraint
{
/**
* @var string
*/
private $pattern;

/**
* @var Matcher
*/
private $matcher;

/**
* @param string $pattern
*/
public function __construct($pattern)
{
parent::__construct();

$this->pattern = $pattern;
$this->matcher = $this->createMatcher();
}

/**
* @return string
*/
public function toString()
{
return 'matches the pattern';
}

/**
* @param mixed $other
*
* @return null|string
*/
protected function additionalFailureDescription($other)
{
return $this->matcher->getError();
}

/**
* @param mixed $value
*
* @return bool
*/
protected function matches($value)
{
return $this->matcher->match($value, $this->pattern);
}

/**
* @return Matcher
*/
private function createMatcher()
{
$factory = new SimpleFactory();

return $factory->createMatcher();
}
}
26 changes: 26 additions & 0 deletions src/PHPUnit/PHPMatcherTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Coduo\PHPMatcher\PHPUnit;

abstract class PHPMatcherTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @param string $pattern
* @param mixed $value
* @param string $message
*/
protected function assertMatchesPattern($pattern, $value, $message = '')
{
$this->assertThat($value, self::matchesPattern($pattern), $message);
}

/**
* @param string $pattern
*
* @return PHPMatcherConstraint
*/
protected static function matchesPattern($pattern)
{
return new PHPMatcherConstraint($pattern);
}
}
41 changes: 41 additions & 0 deletions tests/PHPUnit/PHPMatcherAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Coduo\PHPMatcher\Tests\PHPUnit;

use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;

class PHPMatcherAssertionsTest extends \PHPUnit_Framework_TestCase
{
use PHPMatcherAssertions;

public function test_it_asserts_if_a_value_matches_the_pattern()
{
$this->assertMatchesPattern('@string@', 'foo');
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Failed asserting that '{"foo":"bar"}' matches the pattern
*/
public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern()
{
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(array('foo' => 'bar')));
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Failed asserting that 42 matches the pattern.
*/
public function test_it_creates_a_constraint_for_stubs()
{
$mock = $this->getMockBuilder('stdClass')
->setMethods(array('getTitle'))
->getMock();

$mock->method('getTitle')
->with($this->matchesPattern('@string@'))
->willReturn('foo');

$mock->getTitle(42);
}
}
56 changes: 56 additions & 0 deletions tests/PHPUnit/PHPMatcherConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Coduo\PHPMatcher\Tests\PHPUnit;

use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;

class PHPMatcherConstraintTest extends \PHPUnit_Framework_TestCase
{
public function test_it_is_a_phpunit_constraint()
{
$this->assertInstanceOf('PHPUnit_Framework_Constraint', new PHPMatcherConstraint('@string@'));
}

public function test_it_returns_true_if_a_value_matches_the_pattern()
{
$constraint = new PHPMatcherConstraint('@string@');

$this->assertTrue($constraint->evaluate('foo', '', true));
}

public function test_it_returns_false_if_a_value_does_not_match_the_pattern()
{
$constraint = new PHPMatcherConstraint('@string@');

$this->assertFalse($constraint->evaluate(42, '', true));
}

public function test_it_returns_false_if_a_pattern_is_not_a_string()
{
$constraint = new PHPMatcherConstraint(new \stdClass());

$this->assertFalse($constraint->evaluate('foo', '', true));
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Failed asserting that 42 matches the pattern
*/
public function test_it_sets_a_failure_description_if_not_given()
{
$constraint = new PHPMatcherConstraint('@string@');

$this->assertFalse($constraint->evaluate(42));
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage integer "42" is not a valid string
*/
public function test_it_sets_additional_failure_description()
{
$constraint = new PHPMatcherConstraint('@string@');

$this->assertFalse($constraint->evaluate(42));
}
}
39 changes: 39 additions & 0 deletions tests/PHPUnit/PHPMatcherTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Coduo\PHPMatcher\Tests\PHPUnit;

use Coduo\PHPMatcher\PHPUnit\PHPMatcherTestCase;

class PHPMatcherTestCaseTest extends PHPMatcherTestCase
{
public function test_it_asserts_if_a_value_matches_the_pattern()
{
$this->assertMatchesPattern('@string@', 'foo');
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Failed asserting that '{"foo":"bar"}' matches the pattern
*/
public function test_it_throws_an_expectation_failed_exception_if_a_value_does_not_match_the_pattern()
{
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(array('foo' => 'bar')));
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Failed asserting that 42 matches the pattern.
*/
public function test_it_creates_a_constraint_for_stubs()
{
$mock = $this->getMockBuilder('stdClass')
->setMethods(array('getTitle'))
->getMock();

$mock->method('getTitle')
->with($this->matchesPattern('@string@'))
->willReturn('foo');

$mock->getTitle(42);
}
}