Browse Source

Fix remaining tests in Http package.

Mark Story 8 years ago
parent
commit
a296960cb9

+ 1 - 1
src/Http/ActionDispatcher.php

@@ -91,7 +91,7 @@ class ActionDispatcher
         }
         }
 
 
         $response = $this->_invoke($controller);
         $response = $this->_invoke($controller);
-        if (isset($request->params['return'])) {
+        if ($request->getParam('return')) {
             return $response;
             return $response;
         }
         }
 
 

+ 42 - 49
tests/TestCase/Http/ActionDispatcherTest.php

@@ -40,7 +40,6 @@ class ActionDispatcherTest extends TestCase
         Router::reload();
         Router::reload();
         static::setAppNamespace();
         static::setAppNamespace();
         $this->dispatcher = new ActionDispatcher();
         $this->dispatcher = new ActionDispatcher();
-        $this->dispatcher->addFilter(new ControllerFactoryFilter());
     }
     }
 
 
     /**
     /**
@@ -72,38 +71,46 @@ class ActionDispatcherTest extends TestCase
     /**
     /**
      * Ensure that filters connected to the DispatcherFactory are
      * Ensure that filters connected to the DispatcherFactory are
      * also applied
      * also applied
+     *
+     * @group deprecated
+     * @return void
      */
      */
     public function testDispatcherFactoryCompat()
     public function testDispatcherFactoryCompat()
     {
     {
-        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
-            ->setMethods(['beforeDispatch', 'afterDispatch'])
-            ->getMock();
-        DispatcherFactory::add($filter);
-        $dispatcher = new ActionDispatcher(null, null, DispatcherFactory::filters());
-        $this->assertCount(1, $dispatcher->getFilters());
-        $this->assertSame($filter, $dispatcher->getFilters()[0]);
+        $this->deprecated(function () {
+            $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
+                ->setMethods(['beforeDispatch', 'afterDispatch'])
+                ->getMock();
+            DispatcherFactory::add($filter);
+            $dispatcher = new ActionDispatcher(null, null, DispatcherFactory::filters());
+            $this->assertCount(1, $dispatcher->getFilters());
+            $this->assertSame($filter, $dispatcher->getFilters()[0]);
+        });
     }
     }
 
 
     /**
     /**
      * Test adding routing filters
      * Test adding routing filters
      *
      *
+     * @group deprecated
      * @return void
      * @return void
      */
      */
     public function testAddFilter()
     public function testAddFilter()
     {
     {
-        $this->assertCount(1, $this->dispatcher->getFilters());
-        $events = $this->dispatcher->getEventManager();
-        $this->assertCount(1, $events->listeners('Dispatcher.beforeDispatch'));
-        $this->assertCount(1, $events->listeners('Dispatcher.afterDispatch'));
+        $this->deprecated(function () {
+            $this->assertCount(0, $this->dispatcher->getFilters());
+            $events = $this->dispatcher->getEventManager();
+            $this->assertCount(0, $events->listeners('Dispatcher.beforeDispatch'));
+            $this->assertCount(0, $events->listeners('Dispatcher.afterDispatch'));
 
 
-        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
-            ->setMethods(['beforeDispatch', 'afterDispatch'])
-            ->getMock();
-        $this->dispatcher->addFilter($filter);
+            $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
+                ->setMethods(['beforeDispatch', 'afterDispatch'])
+                ->getMock();
+            $this->dispatcher->addFilter($filter);
 
 
-        $this->assertCount(2, $this->dispatcher->getFilters());
-        $this->assertCount(2, $events->listeners('Dispatcher.beforeDispatch'));
-        $this->assertCount(2, $events->listeners('Dispatcher.afterDispatch'));
+            $this->assertCount(1, $this->dispatcher->getFilters());
+            $this->assertCount(1, $events->listeners('Dispatcher.beforeDispatch'));
+            $this->assertCount(1, $events->listeners('Dispatcher.afterDispatch'));
+        });
     }
     }
 
 
     /**
     /**
@@ -115,16 +122,12 @@ class ActionDispatcherTest extends TestCase
     {
     {
         $response = new Response();
         $response = new Response();
         $dispatcher = new ActionDispatcher();
         $dispatcher = new ActionDispatcher();
-        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
-            ->setMethods(['beforeDispatch', 'afterDispatch'])
-            ->getMock();
-        $filter->expects($this->once())
-            ->method('beforeDispatch')
-            ->will($this->returnValue($response));
 
 
         $req = new ServerRequest();
         $req = new ServerRequest();
         $res = new Response();
         $res = new Response();
-        $dispatcher->addFilter($filter);
+        $dispatcher->getEventManager()->on('Dispatcher.beforeDispatch', function () use ($response) {
+            return $response;
+        });
         $result = $dispatcher->dispatch($req, $res);
         $result = $dispatcher->dispatch($req, $res);
         $this->assertSame($response, $result, 'Should be response from filter.');
         $this->assertSame($response, $result, 'Should be response from filter.');
     }
     }
@@ -136,15 +139,6 @@ class ActionDispatcherTest extends TestCase
      */
      */
     public function testDispatchAfterDispatchEventModifyResponse()
     public function testDispatchAfterDispatchEventModifyResponse()
     {
     {
-        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
-            ->setMethods(['beforeDispatch', 'afterDispatch'])
-            ->getMock();
-        $filter->expects($this->once())
-            ->method('afterDispatch')
-            ->will($this->returnCallback(function (Event $event) {
-                $event->getData('response')->body('Filter body');
-            }));
-
         $req = new ServerRequest([
         $req = new ServerRequest([
             'url' => '/cakes',
             'url' => '/cakes',
             'params' => [
             'params' => [
@@ -156,9 +150,12 @@ class ActionDispatcherTest extends TestCase
             'session' => new Session
             'session' => new Session
         ]);
         ]);
         $res = new Response();
         $res = new Response();
-        $this->dispatcher->addFilter($filter);
+        $this->dispatcher->getEventManager()->on('Dispatcher.afterDispatch', function (Event $event) {
+            $response = $event->getData('response');
+            $event->setData('response', $response->withStringBody('Filter body'));
+        });
         $result = $this->dispatcher->dispatch($req, $res);
         $result = $this->dispatcher->dispatch($req, $res);
-        $this->assertSame('Filter body', $result->body(), 'Should be response from filter.');
+        $this->assertSame('Filter body', (string)$result->getBody(), 'Should be response from filter.');
     }
     }
 
 
     /**
     /**
@@ -169,12 +166,6 @@ class ActionDispatcherTest extends TestCase
      */
      */
     public function testDispatchActionReturnResponseNoAfterDispatch()
     public function testDispatchActionReturnResponseNoAfterDispatch()
     {
     {
-        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
-            ->setMethods(['beforeDispatch', 'afterDispatch'])
-            ->getMock();
-        $filter->expects($this->never())
-            ->method('afterDispatch');
-
         $req = new ServerRequest([
         $req = new ServerRequest([
             'url' => '/cakes',
             'url' => '/cakes',
             'params' => [
             'params' => [
@@ -186,9 +177,11 @@ class ActionDispatcherTest extends TestCase
             ],
             ],
         ]);
         ]);
         $res = new Response();
         $res = new Response();
-        $this->dispatcher->addFilter($filter);
+        $this->dispatcher->getEventManager()->on('Dispatcher.afterDispatch', function () {
+            $this->fail('no afterDispatch event should be fired');
+        });
         $result = $this->dispatcher->dispatch($req, $res);
         $result = $this->dispatcher->dispatch($req, $res);
-        $this->assertSame('Hello Jane', $result->body(), 'Response from controller.');
+        $this->assertSame('Hello Jane', (string)$result->getBody(), 'Response from controller.');
     }
     }
 
 
     /**
     /**
@@ -254,7 +247,7 @@ class ActionDispatcherTest extends TestCase
         $response = new Response();
         $response = new Response();
         $result = $this->dispatcher->dispatch($request, $response);
         $result = $this->dispatcher->dispatch($request, $response);
         $this->assertInstanceOf('Cake\Http\Response', $result);
         $this->assertInstanceOf('Cake\Http\Response', $result);
-        $this->assertContains('posts index', $result->body());
+        $this->assertContains('posts index', (string)$result->getBody());
     }
     }
 
 
     /**
     /**
@@ -275,7 +268,7 @@ class ActionDispatcherTest extends TestCase
         $response = new Response();
         $response = new Response();
         $result = $this->dispatcher->dispatch($request, $response);
         $result = $this->dispatcher->dispatch($request, $response);
         $this->assertInstanceOf('Cake\Http\Response', $result);
         $this->assertInstanceOf('Cake\Http\Response', $result);
-        $this->assertContains('autoRender false body', $result->body());
+        $this->assertContains('autoRender false body', (string)$result->getBody());
     }
     }
 
 
     /**
     /**
@@ -382,7 +375,7 @@ class ActionDispatcherTest extends TestCase
         ]);
         ]);
         $response = new Response();
         $response = new Response();
         $result = $this->dispatcher->dispatch($request, $response);
         $result = $this->dispatcher->dispatch($request, $response);
-        $this->assertSame('startup stop', $result->body());
+        $this->assertSame('startup stop', (string)$result->getBody());
     }
     }
 
 
     /**
     /**
@@ -404,6 +397,6 @@ class ActionDispatcherTest extends TestCase
         ]);
         ]);
         $response = new Response();
         $response = new Response();
         $result = $this->dispatcher->dispatch($request, $response);
         $result = $this->dispatcher->dispatch($request, $response);
-        $this->assertSame('shutdown stop', $result->body());
+        $this->assertSame('shutdown stop', (string)$result->getBody());
     }
     }
 }
 }

+ 4 - 4
tests/TestCase/Http/Middleware/CsrfProtectionMiddlewareTest.php

@@ -79,12 +79,12 @@ class CsrfProtectionMiddlewareTest extends TestCase
         $response = new Response();
         $response = new Response();
 
 
         $closure = function ($request, $response) {
         $closure = function ($request, $response) {
-            $cookie = $response->cookie('csrfToken');
+            $cookie = $response->getCookie('csrfToken');
             $this->assertNotEmpty($cookie, 'Should set a token.');
             $this->assertNotEmpty($cookie, 'Should set a token.');
             $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
             $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
             $this->assertEquals(0, $cookie['expire'], 'session duration.');
             $this->assertEquals(0, $cookie['expire'], 'session duration.');
             $this->assertEquals('/dir/', $cookie['path'], 'session path.');
             $this->assertEquals('/dir/', $cookie['path'], 'session path.');
-            $this->assertEquals($cookie['value'], $request->params['_csrfToken']);
+            $this->assertEquals($cookie['value'], $request->getParam('_csrfToken'));
         };
         };
 
 
         $middleware = new CsrfProtectionMiddleware();
         $middleware = new CsrfProtectionMiddleware();
@@ -266,8 +266,8 @@ class CsrfProtectionMiddlewareTest extends TestCase
         $response = new Response();
         $response = new Response();
 
 
         $closure = function ($request, $response) {
         $closure = function ($request, $response) {
-            $this->assertEmpty($response->cookie('csrfToken'));
-            $cookie = $response->cookie('token');
+            $this->assertEmpty($response->getCookie('csrfToken'));
+            $cookie = $response->getCookie('token');
             $this->assertNotEmpty($cookie, 'Should set a token.');
             $this->assertNotEmpty($cookie, 'Should set a token.');
             $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
             $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
             $this->assertWithinRange((new Time('+1 hour'))->format('U'), $cookie['expire'], 1, 'session duration.');
             $this->assertWithinRange((new Time('+1 hour'))->format('U'), $cookie['expire'], 1, 'session duration.');

+ 6 - 12
tests/test_app/TestApp/Controller/CakesController.php

@@ -22,9 +22,7 @@ class CakesController extends Controller
      */
      */
     public function index()
     public function index()
     {
     {
-        $this->response->body('Hello Jane');
-
-        return $this->response;
+        return $this->response->withStringBody('Hello Jane');
     }
     }
 
 
     /**
     /**
@@ -35,7 +33,7 @@ class CakesController extends Controller
     public function noRender()
     public function noRender()
     {
     {
         $this->autoRender = false;
         $this->autoRender = false;
-        $this->response->body('autoRender false body');
+        $this->response = $this->response->withStringBody('autoRender false body');
     }
     }
 
 
     /**
     /**
@@ -54,10 +52,8 @@ class CakesController extends Controller
     public function startupProcess()
     public function startupProcess()
     {
     {
         parent::startupProcess();
         parent::startupProcess();
-        if ($this->request->param('stop') === 'startup') {
-            $this->response->body('startup stop');
-
-            return $this->response;
+        if ($this->request->getParam('stop') === 'startup') {
+            return $this->response->withStringBody('startup stop');
         }
         }
     }
     }
 
 
@@ -67,10 +63,8 @@ class CakesController extends Controller
     public function shutdownProcess()
     public function shutdownProcess()
     {
     {
         parent::shutdownProcess();
         parent::shutdownProcess();
-        if ($this->request->param('stop') === 'shutdown') {
-            $this->response->body('shutdown stop');
-
-            return $this->response;
+        if ($this->request->getParam('stop') === 'shutdown') {
+            return $this->response->withStringBody('shutdown stop');
         }
         }
     }
     }
 }
 }

+ 4 - 11
tests/test_app/TestApp/Controller/PostsController.php

@@ -41,7 +41,7 @@ class PostsController extends AppController
      */
      */
     public function beforeFilter(Event $event)
     public function beforeFilter(Event $event)
     {
     {
-        if ($this->request->param('action') !== 'securePost') {
+        if ($this->request->getParam('action') !== 'securePost') {
             $this->getEventManager()->off($this->Security);
             $this->getEventManager()->off($this->Security);
         }
         }
     }
     }
@@ -55,10 +55,7 @@ class PostsController extends AppController
     public function index($layout = 'default')
     public function index($layout = 'default')
     {
     {
         $this->Flash->error('An error message');
         $this->Flash->error('An error message');
-        $this->response->cookie([
-            'name' => 'remember_me',
-            'value' => 1
-        ]);
+        $this->response = $this->response->withCookie('remember_me', 1);
         $this->set('test', 'value');
         $this->set('test', 'value');
         $this->viewBuilder()->setLayout($layout);
         $this->viewBuilder()->setLayout($layout);
     }
     }
@@ -92,15 +89,11 @@ class PostsController extends AppController
      */
      */
     public function securePost()
     public function securePost()
     {
     {
-        $this->response->body('Request was accepted');
-
-        return $this->response;
+        return $this->response->withStringBody('Request was accepted');
     }
     }
 
 
     public function file()
     public function file()
     {
     {
-        $this->response->file(__FILE__);
-
-        return $this->response;
+        return $this->response->withFile(__FILE__);
     }
     }
 }
 }