Skip to content

Instantly share code, notes, and snippets.

@Saeven
Created March 1, 2017 17:12
Show Gist options
  • Save Saeven/fd264d8db391202215ec82abb8fd3f53 to your computer and use it in GitHub Desktop.
Save Saeven/fd264d8db391202215ec82abb8fd3f53 to your computer and use it in GitHub Desktop.
Sample dispatch test
<?php
namespace Spec\Application\Controller;
use Application\Controller\Plugin\JsonWrapper;
use Application\Entity\User;
use Application\Mapper\ConfigurationMapper;
use Application\Mapper\CouponMapper;
use Application\Mapper\UserServerPlanMapper;
use Application\Service\CartService;
use Application\Service\DomainService;
use Application\Service\HostingService;
use Application\Service\Payment\PaymentService;
use Application\Service\Registrar\RegistrarService;
use CirclicalUser\Controller\Plugin\AuthenticationPlugin;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Prophet;
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill as FormElementManager;
use Zend\Http\Request;
use Zend\Mvc\Controller\Plugin\Params;
use Zend\Mvc\Controller\PluginManager;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Console\Router\RouteMatch;
class IndexControllerSpec extends ObjectBehavior
{
function let(FormElementManager $formElementManager, CartService $cartService, ConfigurationMapper $configurationMapper, HostingService $hostingService,
PaymentService $paymentService, CouponMapper $couponMapper, DomainService $domainService, RegistrarService $registrarService,
AuthenticationPlugin $auth, JsonWrapper $jsonWrapper, UserServerPlanMapper $userServerPlanMapper)
{
$hostingService->getHostingPlans()->willReturn([]);
$cartService->getProductTemplates()->willReturn([]);
$auth->getIdentity()->willReturn(null);
$this->beConstructedWith(
$formElementManager, $cartService, $configurationMapper, $hostingService, $paymentService,
['circlical' => ['default_country' => 'US']],
$couponMapper, $domainService, $registrarService, $userServerPlanMapper
);
}
/**
* Convenience method to mock plugins into controller specs
*
* @param array $plugins
*
* @return \Prophecy\Prophecy\ObjectProphecy
*/
private function createPluginManager($plugins = [])
{
$prophet = new Prophet();
$pluginManager = $prophet->prophesize(PluginManager::class);
foreach ($plugins as $name => $plugin) {
$pluginManager->get($name, Argument::cetera())->willReturn($plugin->getWrappedObject());
}
$pluginManager->setController(Argument::any())->willReturn(null);
$this->setPluginManager($pluginManager->reveal());
}
/**
* Simulate dispatching the request, lets you modify getRequest()'s response
*
* @param $request
*
* @return mixed
*/
private function dispatchRequest($request)
{
$prophet = new Prophet();
/** @var RouteMatch $routeMatch */
$routeMatch = $prophet->prophesize(RouteMatch::class);
$routeMatch->getParam('action', Argument::any())->willReturn('unused');
$mvcEvent = $prophet->prophesize(MvcEvent::class);
$mvcEvent->getName()->willReturn('unused');
$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);
$mvcEvent->getResult()->willReturn($mvcEvent);
$this->setEvent($mvcEvent);
return $this->dispatch($request);
}
function it_is_initializable()
{
$this->shouldHaveType('Zend\Mvc\Controller\AbstractActionController');
}
function it_returns_a_view($auth, $jsonWrapper)
{
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
$this->createPluginManager([
'auth' => $auth,
'json' => $jsonWrapper,
]);
/** @var Subject $ret */
$ret = $this->indexAction();
$ret->shouldHaveType(ViewModel::class);
}
function it_injects_name_when_a_user_exists($auth, $jsonWrapper, User $user, $userServerPlanMapper)
{
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
$user->getFirstName()->willReturn('Alex');
$user->getLastName()->willReturn('Lemaire');
$auth->getIdentity()->willReturn($user);
$userServerPlanMapper->getUserPlansForAliasing($user)->willReturn([]);
$this->createPluginManager([
'auth' => $auth,
'json' => $jsonWrapper,
]);
/** @var Subject $ret */
$ret = $this->indexAction();
$ret->shouldHaveType(ViewModel::class);
$ret->getVariable('first_name')->shouldBe('Alex');
}
function it_can_use_params_with_post_now(Params $params, Request $request)
{
$params->__invoke(Argument::any(), Argument::any())->willReturn($params);
$params->fromRoute('id', Argument::any())->willReturn(1);
$this->createPluginManager([
'params' => $params,
]);
$request->isPost()->willReturn(true);
$this->dispatchRequest($request);
$viewModel = $this->testAction();
$viewModel->shouldHaveType(ViewModel::class);
$viewModel->getVariable('id')->shouldBe(1);
$viewModel->getVariable('p')->shouldBe(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment