Last active
May 20, 2018 04:21
-
-
Save veewee/7022fee5f54a617079a7 to your computer and use it in GitHub Desktop.
A base Phpspec class for Zend Framework 2 controllers.
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 | |
namespace spec\Application\Controller; | |
use PhpSpec\ObjectBehavior; | |
use PhpSpec\Wrapper\WrapperInterface; | |
use Prophecy\Argument; | |
use Prophecy\Prophecy\ProphecyInterface; | |
use Prophecy\Prophet; | |
use Zend\Mvc\Controller\PluginManager; | |
abstract class BaseControllerSpec extends ObjectBehavior | |
{ | |
public function it_should_extend_abstract_controller() | |
{ | |
$this->shouldHaveType('Zend\Mvc\Controller\AbstractActionController'); | |
} | |
/** | |
* @param array $plugins | |
* | |
* @return PluginManager | |
*/ | |
protected function createPluginManager($plugins = []) | |
{ | |
$prophet = new Prophet(); | |
$pluginManager = $prophet->prophesize('Zend\Mvc\Controller\PluginManager'); | |
foreach ($plugins as $name => $plugin) { | |
$plugin = ($plugin instanceof WrapperInterface) ? $plugin->getWrappedObject() : $plugin; | |
$plugin = ($plugin instanceof ProphecyInterface) ? $plugin->reveal() : $plugin; | |
$pluginManager->get($name, Argument::cetera())->willReturn($plugin); | |
} | |
$pluginManager->setController(Argument::any())->willReturn(null); | |
$this->setPluginManager($pluginManager->reveal()); | |
return $pluginManager; | |
} | |
/** | |
* @param \Zend\Http\Request $request | |
* @param string $action | |
* | |
* @return mixed | |
*/ | |
protected function dispatchRequest($request, $action) | |
{ | |
$prophet = new Prophet(); | |
$routeMatch = $prophet->prophesize('Zend\Mvc\Router\RouteMatch'); | |
$routeMatch->getParam('action', Argument::any())->willReturn($action); | |
$mvcEvent = $prophet->prophesize('Zend\Mvc\MvcEvent'); | |
$mvcEvent->getRouteMatch()->willReturn($routeMatch); | |
$mvcEvent->getRequest()->willReturn($request); | |
$mvcEvent->setRequest(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->setResponse(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->setTarget(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->setName(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->stopPropagation(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->propagationIsStopped(Argument::any())->willReturn($mvcEvent); | |
$mvcEvent->setResult(Argument::any())->willReturn($mvcEvent); | |
$this->setEvent($mvcEvent); | |
return $this->dispatch($request); | |
} | |
} |
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 | |
namespace spec\Application\Controller; | |
use Prophecy\Argument; | |
use spec\Application\Controller\BaseControllerSpec; | |
class SampleControllerSpec extends BaseControllerSpec | |
{ | |
/** | |
* @param \Zend\Http\Request $request | |
* @param \Zend\Mvc\Controller\Plugin\Redirect $redirect | |
* @param \Zend\Mvc\Controller\Plugin\Params $params | |
* @param \Zend\Http\Response $response | |
*/ | |
public function it_should_redirect_on_invalid_request_method($request, $redirect, $params, $response) | |
{ | |
$request->isPost()->willReturn(false); | |
$params->fromRoute('id', Argument::any())->willReturn(5); | |
$redirect->toRoute(Argument::cetera())->willReturn($response); | |
$this->createPluginManager([ | |
'redirect' => $redirect, | |
'params' => $params, | |
]); | |
$result = $this->dispatchRequest($request, 'update'); | |
$result->shouldBe($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to future-self Googling to get here: This same code written up at http://circlical.com/blog/2016/9/8/testing-zend-framework-controllers-with-plugins-using-phpspec No idea which came first.