From 0ef9d475a18bb9ff2ccba49b42216b5a7f8cb10a Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 13 Jun 2016 22:44:12 -0400 Subject: [PATCH] Rough out Middleware Dispatcher This is a very very rough prototype of how PSR7 request dispatching could be integrated into into the TestSuite. There is still a lot TODO, but a very basic GET request is somewhat functional right now. --- src/TestSuite/IntegrationTestCase.php | 9 +- src/TestSuite/MiddlewareDispatcher.php | 88 +++++++++++++++++++ .../TestSuite/IntegrationTestCaseTest.php | 52 ++++++++++- tests/test_app/TestApp/Application.php | 39 ++++++++ 4 files changed, 183 insertions(+), 5 deletions(-) create mode 100644 src/TestSuite/MiddlewareDispatcher.php create mode 100644 tests/test_app/TestApp/Application.php diff --git a/src/TestSuite/IntegrationTestCase.php b/src/TestSuite/IntegrationTestCase.php index 639a3662275..be5ebd2042d 100644 --- a/src/TestSuite/IntegrationTestCase.php +++ b/src/TestSuite/IntegrationTestCase.php @@ -386,9 +386,7 @@ public function delete($url) protected function _sendRequest($url, $method, $data = []) { if ($this->_useHttpServer) { - // The PSR7 mode will need to convert back into a cake request. - // and figure out how to handle the session data. - throw new \LogicException('Not implemented yet.'); + $dispatcher = new MiddlewareDispatcher($this); } else { $dispatcher = new RequestDispatcher($this); } @@ -414,8 +412,11 @@ protected function _sendRequest($url, $method, $data = []) * @param \Cake\Controller\Controller $controller Controller instance. * @return void */ - public function controllerSpy($event, $controller) + public function controllerSpy($event, $controller = null) { + if (!$controller) { + $controller = $event->subject(); + } $this->_controller = $controller; $events = $controller->eventManager(); $events->on('View.beforeRender', function ($event, $viewFile) { diff --git a/src/TestSuite/MiddlewareDispatcher.php b/src/TestSuite/MiddlewareDispatcher.php new file mode 100644 index 00000000000..a3f664d9952 --- /dev/null +++ b/src/TestSuite/MiddlewareDispatcher.php @@ -0,0 +1,88 @@ +_test = $test; + } + + /** + * Run a request and get the response. + * + * @param \Cake\Network\Request $request The request to execute. + * @return \Cake\Network\Response The generated response. + */ + public function execute($request) + { + try { + $namespace = Configure::read('App.namespace'); + $reflect = new ReflectionClass($namespace . '\Application'); + $app = $reflect->newInstance('./config'); + } catch (ReflectionException $e) { + throw new LogicException(sprintf( + 'Cannot load "%s" for use in integration testing.', + $class + )); + } + + // Spy on the controller using the initialize hook instead + // of the dispatcher hooks as those will be going away one day. + EventManager::instance()->on( + 'Controller.initialize', + [$this->_test, 'controllerSpy'] + ); + + $server = new Server($app); + + // TODO How to handle passing all headers. + // The Request doesn't expose a way to read all headers values. + // TODO How to pass session data? PSR7 requests don't handle sessions.. + // TODO pass php://input stream, base, webroot + $serverData = [ + 'REQUEST_URI' => $request->here, + 'REQUEST_METHOD' => $request->method(), + ]; + $psrRequest = ServerRequestFactory::fromGlobals( + array_merge($_SERVER, $serverData), + $request->query, + $request->data(), + $request->cookies + ); + $response = $server->run($psrRequest); + return ResponseTransformer::toCake($response); + } +} diff --git a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php index 356f30feba8..642c1fba4af 100644 --- a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php +++ b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php @@ -43,6 +43,7 @@ public function setUp() DispatcherFactory::clear(); DispatcherFactory::add('Routing'); DispatcherFactory::add('ControllerFactory'); + $this->useHttpServer(false); } /** @@ -172,6 +173,23 @@ public function testGet() $this->assertEquals('This is a test', $this->_response->body()); } + /** + * Test sending get requests with Http\Server + * + * @return void + */ + public function testGetHttpServer() + { + DispatcherFactory::clear(); + $this->useHttpServer(true); + $this->assertNull($this->_response); + + $this->get('/request_action/test_request_action'); + $this->assertNotEmpty($this->_response); + $this->assertInstanceOf('Cake\Network\Response', $this->_response); + $this->assertEquals('This is a test', $this->_response->body()); + } + /** * Test sending requests stores references to controller/view/layout. * @@ -191,6 +209,39 @@ public function testRequestSetsProperties() $this->assertEquals('value', $this->viewVariable('test')); } + /** + * Test PSR7 requests store references to controller/view/layout + * + * @return void + */ + public function testRequestSetsPropertiesHttpServer() + { + $this->markTestIncomplete('not done'); + DispatcherFactory::clear(); + $this->useHttpServer(true); + + $this->post('/posts/index'); + $this->assertInstanceOf('Cake\Controller\Controller', $this->_controller); + $this->assertNotEmpty($this->_viewName, 'View name not set'); + $this->assertContains('Template' . DS . 'Posts' . DS . 'index.ctp', $this->_viewName); + $this->assertNotEmpty($this->_layoutName, 'Layout name not set'); + $this->assertContains('Template' . DS . 'Layout' . DS . 'default.ctp', $this->_layoutName); + + $this->assertTemplate('index'); + $this->assertLayout('default'); + $this->assertEquals('value', $this->viewVariable('test')); + } + + /** + * Test that the PSR7 requests get post, cookies, and other request data passed along. + * + * @return void + */ + public function testPsrRequestData() + { + $this->markTestIncomplete('not done'); + } + /** * Assert that the stored template doesn't change when cells are rendered. * @@ -242,7 +293,6 @@ public function testCookieNotSetFailure() $this->assertCookieNotSet('remember_me'); } - /** * Tests the failure message for assertCookieNotSet when no * response whas generated diff --git a/tests/test_app/TestApp/Application.php b/tests/test_app/TestApp/Application.php new file mode 100644 index 00000000000..d998fd55e83 --- /dev/null +++ b/tests/test_app/TestApp/Application.php @@ -0,0 +1,39 @@ +push(new RoutingMiddleware()); + return $middleware; + } +}