/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
- <?php
- namespace ZendTest\Mvc\Controller;
- use PHPUnit_Framework_TestCase as TestCase,
- Zend\EventManager\StaticEventManager,
- Zend\Http\Request,
- Zend\Http\Response,
- Zend\Mvc\Controller\PluginBroker,
- Zend\Mvc\MvcEvent,
- Zend\Mvc\Router\RouteMatch;
- class ActionControllerTest extends TestCase
- {
- public $controller;
- public $event;
- public $request;
- public $response;
- public function setUp()
- {
- $this->controller = new TestAsset\SampleController();
- $this->request = new Request();
- $this->routeMatch = new RouteMatch(array('controller' => 'controller-sample'));
- $this->event = new MvcEvent();
- $this->event->setRouteMatch($this->routeMatch);
- $this->controller->setEvent($this->event);
- StaticEventManager::resetInstance();
- }
- public function testDispatchInvokesIndexActionWhenNoActionPresentInRouteMatch()
- {
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertTrue(isset($result['content']));
- $this->assertContains('Placeholder page', $result['content']);
- }
- public function testDispatchInvokesNotFoundActionWhenInvalidActionPresentInRouteMatch()
- {
- $this->routeMatch->setParam('action', 'totally-made-up-action');
- $result = $this->controller->dispatch($this->request, $this->response);
- $response = $this->controller->getResponse();
- $this->assertEquals(404, $response->getStatusCode());
- $this->assertTrue(isset($result['content']));
- $this->assertContains('Page not found', $result['content']);
- }
- public function testDispatchInvokesProvidedActionWhenMethodExists()
- {
- $this->routeMatch->setParam('action', 'test');
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertTrue(isset($result['content']));
- $this->assertContains('test', $result['content']);
- }
- public function testDispatchCallsActionMethodBasedOnNormalizingAction()
- {
- $this->routeMatch->setParam('action', 'test.some-strangely_separated.words');
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertTrue(isset($result['content']));
- $this->assertContains('Test Some Strangely Separated Words', $result['content']);
- }
- public function testShortCircuitsBeforeActionIfPreDispatchReturnsAResponse()
- {
- $response = new Response();
- $response->setContent('short circuited!');
- $this->controller->events()->attach('dispatch', function($e) use ($response) {
- return $response;
- }, 100);
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertSame($response, $result);
- }
- public function testPostDispatchEventAllowsReplacingResponse()
- {
- $response = new Response();
- $response->setContent('short circuited!');
- $this->controller->events()->attach('dispatch', function($e) use ($response) {
- return $response;
- }, -10);
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertSame($response, $result);
- }
- public function testEventManagerListensOnDispatchableInterfaceByDefault()
- {
- $response = new Response();
- $response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
- $events->attach('Zend\Stdlib\Dispatchable', 'dispatch', function($e) use ($response) {
- return $response;
- }, 10);
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertSame($response, $result);
- }
- public function testEventManagerListensOnActionControllerClassByDefault()
- {
- $response = new Response();
- $response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
- $events->attach('Zend\Mvc\Controller\ActionController', 'dispatch', function($e) use ($response) {
- return $response;
- }, 10);
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertSame($response, $result);
- }
- public function testEventManagerListensOnClassNameByDefault()
- {
- $response = new Response();
- $response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
- $events->attach(get_class($this->controller), 'dispatch', function($e) use ($response) {
- return $response;
- }, 10);
- $result = $this->controller->dispatch($this->request, $this->response);
- $this->assertSame($response, $result);
- }
- public function testDispatchInjectsEventIntoController()
- {
- $this->controller->dispatch($this->request, $this->response);
- $event = $this->controller->getEvent();
- $this->assertNotNull($event);
- $this->assertSame($this->event, $event);
- }
- public function testControllerIsLocatorAware()
- {
- $this->assertInstanceOf('Zend\Mvc\LocatorAware', $this->controller);
- }
- public function testControllerIsEventAware()
- {
- $this->assertInstanceOf('Zend\Mvc\InjectApplicationEvent', $this->controller);
- }
- public function testControllerIsPluggable()
- {
- $this->assertInstanceOf('Zend\Loader\Pluggable', $this->controller);
- }
- public function testComposesPluginBrokerByDefault()
- {
- $broker = $this->controller->getBroker();
- $this->assertInstanceOf('Zend\Mvc\Controller\PluginBroker', $broker);
- }
- public function testPluginBrokerComposesController()
- {
- $broker = $this->controller->getBroker();
- $controller = $broker->getController();
- $this->assertSame($this->controller, $controller);
- }
- public function testInjectingBrokerSetsControllerWhenPossible()
- {
- $broker = new PluginBroker();
- $this->assertNull($broker->getController());
- $this->controller->setBroker($broker);
- $this->assertSame($this->controller, $broker->getController());
- $this->assertSame($broker, $this->controller->getBroker());
- }
- public function testMethodOverloadingShouldReturnPluginWhenFound()
- {
- $plugin = $this->controller->url();
- $this->assertInstanceOf('Zend\Mvc\Controller\Plugin\Url', $plugin);
- }
- }