Created
November 22, 2012 14:36
-
-
Save cjsaylor/4131481 to your computer and use it in GitHub Desktop.
Convenient PHPUnit mocking of Singleton Classes
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
<?php | |
class Sample { | |
public __construct() { | |
} | |
public function sayHello($name) { | |
return Someclass::getInstance()->hello($name); | |
} | |
} |
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
<?php | |
class SampleTest extends \PHPUnit_Framework_TestCase { | |
public function testSayHello() { | |
$sample = new Sample(); | |
$name = 'Chris'; | |
$replace = 'Goodbye, Chris!'; | |
SomeclassMock::expects($name, $replace); | |
$this->assertEquals("Goodbye, Chris!", $sample->sayHello($name)); | |
SomeclassMock::cleanup(); | |
} | |
} |
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
<?php | |
class Someclass { | |
protected static $self; | |
protected __construct() { | |
} | |
public static function getInstance() { | |
if (empty(static::$self)) { | |
static::$self = new Someclass(); | |
} | |
return static::$self; | |
} | |
public function hello($name) { | |
return "hello, $name!"; | |
} | |
} |
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
<?php | |
class SomeclassMock { | |
/** | |
* Generates a mock object on the singleton Someclass util object. | |
* | |
* @param array $name | |
* @return void | |
*/ | |
public static function expects($name, $replace) { | |
// Mock the object | |
$mock = \PHPUnit_Framework_MockObject_Generator::getMock( | |
'Someclass', | |
array('hello'), | |
array(), | |
'', | |
false | |
); | |
// Replace protected self reference with mock object | |
$ref = new \ReflectionProperty('Someclass', 'self'); | |
$ref->setAccessible(true); | |
$ref->setValue(null, $mock); | |
// Set expectations and return values | |
$mock | |
->expects(new \PHPUnit_Framework_MockObject_Matcher_InvokedCount(1)) | |
->method('hello') | |
->with( | |
\PHPUnit_Framework_Assert::equalTo($name) | |
) | |
->will(new \PHPUnit_Framework_MockObject_Stub_Return($replace)); | |
} | |
public static function cleanup() { | |
$ref = new \ReflectionProperty('Someclass', 'self'); | |
$ref->setAccessible(true); | |
$ref->setValue(null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment