Last active
July 26, 2022 06:16
-
-
Save RWhar/21a47c58beaede6acef9 to your computer and use it in GitHub Desktop.
PHPUnit - Mock _get() on an Abstract Class
This file contains hidden or 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
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