Skip to content

Commit

Permalink
Merge pull request #543 from SaveYa/add-contraint-matcher
Browse files Browse the repository at this point in the history
added constraint matcher
  • Loading branch information
davedevelopment committed Feb 9, 2016
2 parents cf51134 + cf50a4b commit 36b9bfb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
43 changes: 43 additions & 0 deletions library/Mockery/Matcher/PHPUnitConstraint.php
@@ -0,0 +1,43 @@
<?php
namespace Mockery\Matcher;

class PHPUnitConstraint extends MatcherAbstract
{
protected $constraint;
protected $rethrow;

/**
* @param \PHPUnit_Framework_Constraint $constraint
* @param bool $rethrow
*/
public function __construct(\PHPUnit_Framework_Constraint $constraint, $rethrow = false)
{
$this->constraint = $constraint;
$this->rethrow = $rethrow;
}

/**
* @param mixed $actual
* @return bool
*/
public function match(&$actual)
{
try {
$this->constraint->evaluate($actual);
return true;
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
if ($this->rethrow) {
throw $e;
}
return false;
}
}

/**
*
*/
public function __toString()
{
return '<Constraint>';
}
}
1 change: 1 addition & 0 deletions package.xml
Expand Up @@ -92,6 +92,7 @@
<file name="Any.php" role="php"/>
<file name="AnyOf.php" role="php"/>
<file name="Closure.php" role="php"/>
<file name="Constraint.php" role="php"/>
<file name="Contains.php" role="php"/>
<file name="Ducktype.php" role="php"/>
<file name="HasKey.php" role="php"/>
Expand Down
72 changes: 72 additions & 0 deletions tests/Mockery/Matcher/PHPUnitConstraintTest.php
@@ -0,0 +1,72 @@
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 2/1/16
* Time: 12:36 PM
*/

use Mockery\MockInterface;
use Mockery\Matcher\PHPUnitConstraint;

class PHPUnitConstraintTest extends \PHPUnit_Framework_TestCase
{
/** @var PHPUnitConstraint */
protected $matcher;
/** @var PHPUnitConstraint */
protected $rethrowingMatcher;
/** @var MockInterface */
protected $constraint;

public function setUp()
{
$this->constraint = \Mockery::mock('PHPUnit_Framework_Constraint');
$this->matcher = new PHPUnitConstraint($this->constraint);
$this->rethrowingMatcher = new PHPUnitConstraint($this->constraint, true);
}

public function testMatches()
{
$value1 = 'value1';
$value2 = 'value1';
$value3 = 'value1';
$this->constraint
->shouldReceive('evaluate')
->once()
->with($value1)
->getMock()
->shouldReceive('evaluate')
->once()
->with($value2)
->andThrow('PHPUnit_Framework_AssertionFailedError')
->getMock()
->shouldReceive('evaluate')
->once()
->with($value3)
->getMock()
;
$this->assertTrue($this->matcher->match($value1));
$this->assertFalse($this->matcher->match($value2));
$this->assertTrue($this->rethrowingMatcher->match($value3));
}

/**
* @expectedException \PHPUnit_Framework_AssertionFailedError
*/
public function testMatchesWhereNotMatchAndRethrowing()
{
$value = 'value';
$this->constraint
->shouldReceive('evaluate')
->once()
->with($value)
->andThrow('PHPUnit_Framework_AssertionFailedError')
;
$this->rethrowingMatcher->match($value);
}

public function test__toString()
{
$this->assertEquals('<Constraint>', $this->matcher);
}
}

0 comments on commit 36b9bfb

Please sign in to comment.