Skip to content

Instantly share code, notes, and snippets.

@RWhar
Last active July 26, 2022 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RWhar/21a47c58beaede6acef9 to your computer and use it in GitHub Desktop.
Save RWhar/21a47c58beaede6acef9 to your computer and use it in GitHub Desktop.
PHPUnit - Mock _get() on an Abstract Class
class TestMyObjectLoader extends PHPUnit_Framework_TestCase
{
protected $mockObject;
public function setUp()
{
$this->mockObject = $this->getMockForAbstractClass('Project\MyObject', [], '', true, true, true, ['initialize', '__get']);
}
public function testSomething()
{
$expectedData = [
'myProperty' => 'test',
'anotherProperty' => ['in' => true]
];
$callback = function ($name) use ($expectedData) {
switch ($name) {
case 'myProperty':
$return = $expectedData['myProperty'];
break;
case 'anotherProperty':
$return = $expectedData['anotherProperty'];
break;
default:
$return = null;
}
if (is_null($return)) {
throw new Exception("Property $name is not defined.");
}
return $return;
};
$this->mockObject->expects($this->once())
->method('__get')
->will($this->returnCallback($callback));
$this->assertEquals($expectedData['myProperty'], $this->mockObject->myProperty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment