Last active
December 17, 2015 18:09
-
-
Save asgrim/5650743 to your computer and use it in GitHub Desktop.
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 | |
abstract class AbstractController | |
{ | |
public function dispatch($action) | |
{ | |
$actionName = $action . "Action"; | |
if (!method_exists($this, $actionName)) | |
{ | |
throw new Exception("Action method '{$actionName}' does not exist"); | |
} | |
$this->$actionName(); | |
} | |
} |
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 | |
require_once("src/AbstractController.php"); | |
class AbstractControllerTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testDispatchWillDispatchActionRequest() | |
{ | |
$controller = $this->getMockForAbstractClass('AbstractController', array(), '', true, true, true, array('indexAction')); | |
$controller->expects($this->once())->method('indexAction'); | |
$controller->dispatch('index'); | |
} | |
} |
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
$ phpunit | |
PHPUnit 3.7.21 by Sebastian Bergmann. | |
Configuration read from /home/james/Workspace/methodtest/phpunit.xml | |
E | |
Time: 0 seconds, Memory: 3.25Mb | |
There was 1 error: | |
1) AbstractControllerTest::testDispatchWillDispatchActionRequest | |
Exception: Action method 'indexAction' does not exist | |
/home/james/Workspace/methodtest/src/AbstractController.php:11 | |
/home/james/Workspace/methodtest/tests/AbstractControllerTest.php:13 | |
/usr/share/php/PHPUnit/TextUI/Command.php:176 | |
/usr/share/php/PHPUnit/TextUI/Command.php:129 | |
FAILURES! | |
Tests: 1, Assertions: 0, Errors: 1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Solution
@benjie pointed out here that the
$mockedMethods
array is only added to$methods
if it actually exists, unlike if you call$this->getMock()
directly instead.My workaround/solution is as follows:
This only works because I haven't declared any abstract functions within my abstract class - if I did, I expect it would break, but I haven't tested so can't vouch for that assumption ;)