From c4a7368d043f26ad1ca510b13c112fa2ccdba74f Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 29 Jun 2016 13:07:06 +0200 Subject: php-zendframework-zend-mvc-plugin-prg: 1.0.0 (New Package) --- .../test/PostRedirectGetTest.php | 258 +++++++++++++++++++++ .../test/TestAsset/SampleController.php | 20 ++ 2 files changed, 278 insertions(+) create mode 100644 zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/PostRedirectGetTest.php create mode 100644 zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/TestAsset/SampleController.php (limited to 'zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test') diff --git a/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/PostRedirectGetTest.php b/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/PostRedirectGetTest.php new file mode 100644 index 0000000..d0538d5 --- /dev/null +++ b/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/PostRedirectGetTest.php @@ -0,0 +1,258 @@ +addRoute('home', LiteralRoute::factory([ + 'route' => '/', + 'defaults' => [ + 'controller' => TestAsset\SampleController::class, + ] + ])); + + $router->addRoute('sub', SegmentRoute::factory([ + 'route' => '/foo/:param', + 'defaults' => [ + 'param' => 1 + ] + ])); + + $router->addRoute('ctl', SegmentRoute::factory([ + 'route' => '/ctl/:controller', + 'defaults' => [ + '__NAMESPACE__' => 'ZendTest\Mvc\Plugin\Prg\TestAsset', + 'controller' => 'sample' + ] + ])); + + $this->controller = new TestAsset\SampleController(); + $this->request = new Request(); + $this->event = new MvcEvent(); + $this->routeMatch = new RouteMatch(['controller' => 'controller-sample', 'action' => 'postPage']); + + $this->event->setRequest($this->request); + $this->event->setRouteMatch($this->routeMatch); + $this->event->setRouter($router); + + $this->controller->setEvent($this->event); + + $this->plugin = new PostRedirectGet(); + $this->plugin->setController($this->controller); + } + + public function testReturnsFalseOnInitialGet() + { + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $this->assertFalse($plugin('home')); + } + + public function testRedirectsToUrlOnPost() + { + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters([ + 'postval1' => 'value' + ])); + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultUrl = $plugin('/test/getPage', true); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultUrl); + $this->assertTrue($prgResultUrl->getHeaders()->has('Location')); + $this->assertEquals('/test/getPage', $prgResultUrl->getHeaders()->get('Location')->getUri()); + $this->assertEquals(303, $prgResultUrl->getStatusCode()); + } + + public function testRedirectsToRouteOnPost() + { + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters([ + 'postval1' => 'value1' + ])); + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin('home'); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + } + + public function testReturnsPostOnRedirectGet() + { + $params = [ + 'postval1' => 'value1' + ]; + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters($params)); + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin('home'); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + + // Do GET + $this->request = new Request(); + $this->controller->dispatch($this->request, $this->response); + $prgResult = $plugin('home'); + + $this->assertEquals($params, $prgResult); + + // Do GET again to make sure data is empty + $this->request = new Request(); + $this->controller->dispatch($this->request, $this->response); + $prgResult = $plugin('home'); + + $this->assertFalse($prgResult); + } + + public function testThrowsExceptionOnRouteWithoutRouter() + { + $controller = $this->controller; + $controller = $controller->getEvent()->setRouter(new SimpleRouteStack); + + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters([ + 'postval1' => 'value' + ])); + $this->controller->dispatch($this->request, $this->response); + + $this->setExpectedException(RuntimeException::class); + $plugin = $this->plugin; + $prgResultRoute = $plugin('some/route'); + } + + public function testNullRouteUsesMatchedRouteName() + { + $this->controller->getEvent()->getRouteMatch()->setMatchedRouteName('home'); + + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters([ + 'postval1' => 'value1' + ])); + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin(); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri()); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + } + + public function testReuseMatchedParameters() + { + $this->controller->getEvent()->getRouteMatch()->setMatchedRouteName('sub'); + + $this->request->setMethod('POST'); + $this->request->setPost(new Parameters([ + 'postval1' => 'value1' + ])); + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin(); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals('/foo/1', $prgResultRoute->getHeaders()->get('Location')->getUri()); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + } + + public function testReuseMatchedParametersWithSegmentController() + { + $expects = '/ctl/sample'; + $this->request->setMethod('POST'); + $this->request->setUri($expects); + $this->request->setPost(new Parameters([ + 'postval1' => 'value1' + ])); + + $routeMatch = $this->event->getRouter()->match($this->request); + $this->event->setRouteMatch($routeMatch); + + $moduleRouteListener = new ModuleRouteListener; + $moduleRouteListener->onRoute($this->event); + + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin(); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals( + $expects, + $prgResultRoute->getHeaders()->get('Location')->getUri(), + 'expects to redirect for the same url' + ); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + } + + public function testKeepUrlQueryParameters() + { + $expects = '/ctl/sample'; + $this->request->setMethod('POST'); + $this->request->setUri($expects); + $this->request->setQuery(new Parameters([ + 'id' => '123', + ])); + + $routeMatch = $this->event->getRouter()->match($this->request); + $this->event->setRouteMatch($routeMatch); + + $moduleRouteListener = new ModuleRouteListener; + $moduleRouteListener->onRoute($this->event); + + $this->controller->dispatch($this->request, $this->response); + + $plugin = $this->plugin; + $prgResultRoute = $plugin(); + + $this->assertInstanceOf('Zend\Http\Response', $prgResultRoute); + $this->assertTrue($prgResultRoute->getHeaders()->has('Location')); + $this->assertEquals( + $expects . '?id=123', + $prgResultRoute->getHeaders()->get('Location')->getUri(), + 'expects to redirect for the same url' + ); + $this->assertEquals(303, $prgResultRoute->getStatusCode()); + } +} diff --git a/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/TestAsset/SampleController.php b/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/TestAsset/SampleController.php new file mode 100644 index 0000000..f44f240 --- /dev/null +++ b/zend-mvc-plugin-prg-8c7ccb9f0004e92ff258b483447d914f42cb7448/test/TestAsset/SampleController.php @@ -0,0 +1,20 @@ +