Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from SaveYa/add-contraint-matcher
added constraint matcher
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |