-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Fulvio Notarstefano <[email protected]>
- Loading branch information
1 parent
b647631
commit d058417
Showing
11 changed files
with
228 additions
and
4 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
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
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
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,71 @@ | ||
<?php | ||
// Match on class type. | ||
|
||
namespace WP_Mock\Matcher; | ||
|
||
use Mockery\Exception; | ||
use Mockery\Matcher\MatcherAbstract; | ||
|
||
class AnyInstance extends FuzzyObject { | ||
|
||
/** | ||
* @param string|object $expected A classname or instance of a class whose type should match. | ||
* | ||
* @throws \Mockery\Exception | ||
*/ | ||
public function __construct( $expected = null ) { | ||
if( is_string( $expected ) && class_exists( $expected ) ) { | ||
$reflectedExpected = new \ReflectionClass( $expected ); | ||
$expectedInstance = $reflectedExpected->newInstanceWithoutConstructor(); | ||
} elseif ( is_object( $expected ) ) { | ||
$expectedInstance = $expected; | ||
} else { | ||
throw new Exception( 'AnyInstance matcher can only match objects!' ); | ||
} | ||
|
||
parent::__construct($expectedInstance); | ||
} | ||
|
||
/** | ||
* Check if the actual value matches the expected. | ||
* Actual passed by reference to preserve reference trail (where applicable) | ||
* back to the original method parameter. | ||
* | ||
* @param mixed $actual | ||
* | ||
* @return bool | ||
*/ | ||
public function match( &$actual ) { | ||
if ( ! is_object( $actual ) ) { | ||
return false; | ||
} | ||
|
||
if( $actual instanceof \Closure ) { | ||
return false; | ||
} | ||
|
||
if( get_class( $actual ) === get_class( $this->_expected ) ) { | ||
return true; | ||
} | ||
|
||
// parent::haveCommonAncestor() expects two objects. | ||
$reflectedExpected = new \ReflectionClass( $this->_expected ); | ||
$expectedInstance = $reflectedExpected->newInstanceWithoutConstructor(); | ||
if ( ! $this->haveCommonAncestor( $actual, $expectedInstance ) ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Return a string representation of this Matcher | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() { | ||
$classname = get_class($this->_expected); | ||
return "<AnyInstance[{$classname}]>"; | ||
} | ||
|
||
} |
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,113 @@ | ||
<?php | ||
|
||
namespace WP_Mock\Matcher; | ||
|
||
class AnyInstanceTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testExactClassInstanceMatchesTrue() | ||
{ | ||
$sut = new AnyInstance(new SampleClass()); | ||
|
||
$exactClassAction = new SampleClass(); | ||
|
||
$result = $sut->match($exactClassAction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testExactClassStringMatchesTrue() | ||
{ | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$exactClassAction = new SampleClass(); | ||
|
||
$result = $sut->match($exactClassAction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testSubClassMatchesTrue() | ||
{ | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$subClassAction = new SampleSubClass(); | ||
|
||
$result = $sut->match($subClassAction); | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testWrongClassMatchesFalse() | ||
{ | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$wrongClassAction = new \stdClass(); | ||
|
||
$result = $sut->match($wrongClassAction); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testClosureMatchesFalse() | ||
{ | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$closureAction = function () { | ||
}; | ||
|
||
$result = $sut->match($closureAction); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::match | ||
*/ | ||
public function testStringFunctionMatchesFalse() | ||
{ | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$stringFunctionAction = 'action_name'; | ||
|
||
$result = $sut->match($stringFunctionAction); | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::__toString | ||
*/ | ||
public function testToString() { | ||
$sut = new AnyInstance(SampleClass::class); | ||
|
||
$result = "$sut"; | ||
|
||
$this->assertEquals("<AnyInstance[WP_Mock\Matcher\SampleClass]>", $result); | ||
} | ||
|
||
/** | ||
* @covers \WP_Mock\Matcher\AnyInstance::__construct | ||
*/ | ||
public function testCannotConstructWithoutObject() { | ||
$this->expectException(\Exception::class); | ||
|
||
new AnyInstance('NotAClass' ); | ||
} | ||
} | ||
|
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,7 @@ | ||
<?php | ||
|
||
namespace WP_Mock\Matcher; | ||
|
||
class SampleClass { | ||
public function action(){} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace WP_Mock\Matcher; | ||
|
||
class SampleSubClass extends SampleClass { | ||
public function action(){} | ||
} |