/tests/Zend/Mvc/Controller/ActionControllerTest.php

https://github.com/leerbag/zf2 · PHP · 173 lines · 151 code · 22 blank · 0 comment · 0 complexity · 9d3243ceb907f3f41c50eb2ca3397bbd MD5 · raw file

  1. <?php
  2. namespace ZendTest\Mvc\Controller;
  3. use PHPUnit_Framework_TestCase as TestCase,
  4. Zend\EventManager\StaticEventManager,
  5. Zend\Http\Request,
  6. Zend\Http\Response,
  7. Zend\Mvc\Controller\PluginBroker,
  8. Zend\Mvc\MvcEvent,
  9. Zend\Mvc\Router\RouteMatch;
  10. class ActionControllerTest extends TestCase
  11. {
  12. public $controller;
  13. public $event;
  14. public $request;
  15. public $response;
  16. public function setUp()
  17. {
  18. $this->controller = new TestAsset\SampleController();
  19. $this->request = new Request();
  20. $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample'));
  21. $this->event = new MvcEvent();
  22. $this->event->setRouteMatch($this->routeMatch);
  23. $this->controller->setEvent($this->event);
  24. StaticEventManager::resetInstance();
  25. }
  26. public function testDispatchInvokesIndexActionWhenNoActionPresentInRouteMatch()
  27. {
  28. $result = $this->controller->dispatch($this->request, $this->response);
  29. $this->assertTrue(isset($result['content']));
  30. $this->assertContains('Placeholder page', $result['content']);
  31. }
  32. public function testDispatchInvokesNotFoundActionWhenInvalidActionPresentInRouteMatch()
  33. {
  34. $this->routeMatch->setParam('action', 'totally-made-up-action');
  35. $result = $this->controller->dispatch($this->request, $this->response);
  36. $response = $this->controller->getResponse();
  37. $this->assertEquals(404, $response->getStatusCode());
  38. $this->assertTrue(isset($result['content']));
  39. $this->assertContains('Page not found', $result['content']);
  40. }
  41. public function testDispatchInvokesProvidedActionWhenMethodExists()
  42. {
  43. $this->routeMatch->setParam('action', 'test');
  44. $result = $this->controller->dispatch($this->request, $this->response);
  45. $this->assertTrue(isset($result['content']));
  46. $this->assertContains('test', $result['content']);
  47. }
  48. public function testDispatchCallsActionMethodBasedOnNormalizingAction()
  49. {
  50. $this->routeMatch->setParam('action', 'test.some-strangely_separated.words');
  51. $result = $this->controller->dispatch($this->request, $this->response);
  52. $this->assertTrue(isset($result['content']));
  53. $this->assertContains('Test Some Strangely Separated Words', $result['content']);
  54. }
  55. public function testShortCircuitsBeforeActionIfPreDispatchReturnsAResponse()
  56. {
  57. $response = new Response();
  58. $response->setContent('short circuited!');
  59. $this->controller->events()->attach('dispatch', function($e) use ($response) {
  60. return $response;
  61. }, 100);
  62. $result = $this->controller->dispatch($this->request, $this->response);
  63. $this->assertSame($response, $result);
  64. }
  65. public function testPostDispatchEventAllowsReplacingResponse()
  66. {
  67. $response = new Response();
  68. $response->setContent('short circuited!');
  69. $this->controller->events()->attach('dispatch', function($e) use ($response) {
  70. return $response;
  71. }, -10);
  72. $result = $this->controller->dispatch($this->request, $this->response);
  73. $this->assertSame($response, $result);
  74. }
  75. public function testEventManagerListensOnDispatchableInterfaceByDefault()
  76. {
  77. $response = new Response();
  78. $response->setContent('short circuited!');
  79. $events = StaticEventManager::getInstance();
  80. $events->attach('Zend\Stdlib\Dispatchable', 'dispatch', function($e) use ($response) {
  81. return $response;
  82. }, 10);
  83. $result = $this->controller->dispatch($this->request, $this->response);
  84. $this->assertSame($response, $result);
  85. }
  86. public function testEventManagerListensOnActionControllerClassByDefault()
  87. {
  88. $response = new Response();
  89. $response->setContent('short circuited!');
  90. $events = StaticEventManager::getInstance();
  91. $events->attach('Zend\Mvc\Controller\ActionController', 'dispatch', function($e) use ($response) {
  92. return $response;
  93. }, 10);
  94. $result = $this->controller->dispatch($this->request, $this->response);
  95. $this->assertSame($response, $result);
  96. }
  97. public function testEventManagerListensOnClassNameByDefault()
  98. {
  99. $response = new Response();
  100. $response->setContent('short circuited!');
  101. $events = StaticEventManager::getInstance();
  102. $events->attach(get_class($this->controller), 'dispatch', function($e) use ($response) {
  103. return $response;
  104. }, 10);
  105. $result = $this->controller->dispatch($this->request, $this->response);
  106. $this->assertSame($response, $result);
  107. }
  108. public function testDispatchInjectsEventIntoController()
  109. {
  110. $this->controller->dispatch($this->request, $this->response);
  111. $event = $this->controller->getEvent();
  112. $this->assertNotNull($event);
  113. $this->assertSame($this->event, $event);
  114. }
  115. public function testControllerIsLocatorAware()
  116. {
  117. $this->assertInstanceOf('Zend\Mvc\LocatorAware', $this->controller);
  118. }
  119. public function testControllerIsEventAware()
  120. {
  121. $this->assertInstanceOf('Zend\Mvc\InjectApplicationEvent', $this->controller);
  122. }
  123. public function testControllerIsPluggable()
  124. {
  125. $this->assertInstanceOf('Zend\Loader\Pluggable', $this->controller);
  126. }
  127. public function testComposesPluginBrokerByDefault()
  128. {
  129. $broker = $this->controller->getBroker();
  130. $this->assertInstanceOf('Zend\Mvc\Controller\PluginBroker', $broker);
  131. }
  132. public function testPluginBrokerComposesController()
  133. {
  134. $broker = $this->controller->getBroker();
  135. $controller = $broker->getController();
  136. $this->assertSame($this->controller, $controller);
  137. }
  138. public function testInjectingBrokerSetsControllerWhenPossible()
  139. {
  140. $broker = new PluginBroker();
  141. $this->assertNull($broker->getController());
  142. $this->controller->setBroker($broker);
  143. $this->assertSame($this->controller, $broker->getController());
  144. $this->assertSame($broker, $this->controller->getBroker());
  145. }
  146. public function testMethodOverloadingShouldReturnPluginWhenFound()
  147. {
  148. $plugin = $this->controller->url();
  149. $this->assertInstanceOf('Zend\Mvc\Controller\Plugin\Url', $plugin);
  150. }
  151. }