Browse Source

Fix deprecation warnings in ControllerTest.

A few changes were required for Controller::redirect() to make immutable
method usage able to create the same results as the mutable methods.
mark_story 8 years ago
parent
commit
ca9b8c40a3
2 changed files with 14 additions and 14 deletions
  1. 3 3
      src/Controller/Controller.php
  2. 11 11
      tests/TestCase/Controller/ControllerTest.php

+ 3 - 3
src/Controller/Controller.php

@@ -538,18 +538,18 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
     {
         $this->autoRender = false;
 
-        $response = $this->response;
         if ($status) {
-            $response = $response->withStatus($status);
+            $this->response = $this->response->withStatus($status);
         }
 
-        $event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]);
+        $event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $this->response]);
         if ($event->getResult() instanceof Response) {
             return $this->response = $event->getResult();
         }
         if ($event->isStopped()) {
             return null;
         }
+        $response = $this->response;
 
         if (!$response->getHeaderLine('Location')) {
             $response = $response->withLocation(Router::url($url, true));

+ 11 - 11
tests/TestCase/Controller/ControllerTest.php

@@ -443,7 +443,7 @@ class ControllerTest extends TestCase
         $debug = Configure::read('debug');
         Configure::write('debug', false);
         $result = $Controller->render('index');
-        $this->assertEquals('{"test":"value"}', $result->body());
+        $this->assertEquals('{"test":"value"}', (string)$result->getBody());
         Configure::write('debug', $debug);
     }
 
@@ -495,8 +495,8 @@ class ControllerTest extends TestCase
 
         $response = $Controller->redirect('http://cakephp.org', (int)$code);
         $this->assertSame($response, $Controller->response);
-        $this->assertEquals($code, $response->statusCode());
-        $this->assertEquals('http://cakephp.org', $response->header()['Location']);
+        $this->assertEquals($code, $response->getStatusCode());
+        $this->assertEquals('http://cakephp.org', $response->getHeaderLine('Location'));
         $this->assertFalse($Controller->autoRender);
     }
 
@@ -510,12 +510,13 @@ class ControllerTest extends TestCase
         $Controller = new Controller(null, new Response());
 
         $Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
-            $response = $response->withLocation('https://book.cakephp.org');
+            $controller = $event->getSubject();
+            $controller->response = $response->withLocation('https://book.cakephp.org');
         });
 
         $response = $Controller->redirect('http://cakephp.org', 301);
-        $this->assertEquals('https://book.cakephp.org', $response->header()['Location']);
-        $this->assertEquals(301, $response->statusCode());
+        $this->assertEquals('https://book.cakephp.org', $response->getHeaderLine('Location'));
+        $this->assertEquals(301, $response->getStatusCode());
     }
 
     /**
@@ -525,13 +526,12 @@ class ControllerTest extends TestCase
      */
     public function testRedirectBeforeRedirectModifyingStatusCode()
     {
-        $Response = $this->getMockBuilder('Cake\Http\Response')
-            ->setMethods(['stop'])
-            ->getMock();
-        $Controller = new Controller(null, $Response);
+        $response = new Response();
+        $Controller = new Controller(null, $response);
 
         $Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
-            $response = $response->withStatus(302);
+            $controller = $event->getSubject();
+            $controller->response = $response->withStatus(302);
         });
 
         $response = $Controller->redirect('http://cakephp.org', 301);