Browse Source

Fix a few tests in the Response tests.

mark_story 8 years ago
parent
commit
86b8c7dbce
2 changed files with 119 additions and 114 deletions
  1. 24 21
      src/Http/Response.php
  2. 95 93
      tests/TestCase/Http/ResponseTest.php

+ 24 - 21
src/Http/Response.php

@@ -919,11 +919,7 @@ class Response implements ResponseInterface
         if (!isset($this->_statusCodes[$code])) {
             throw new InvalidArgumentException('Unknown status code');
         }
-        if (isset($this->_statusCodes[$code])) {
-            $this->_reasonPhrase = $this->_statusCodes[$code];
-        }
-        $this->_status = $code;
-        $this->_setContentType();
+        $this->_setStatus($code);
 
         return $code;
     }
@@ -967,17 +963,29 @@ class Response implements ResponseInterface
     public function withStatus($code, $reasonPhrase = '')
     {
         $new = clone $this;
-        $new->_status = $code;
-        if (empty($reasonPhrase) && isset($new->_statusCodes[$code])) {
-            $reasonPhrase = $new->_statusCodes[$code];
-        }
-        $new->_reasonPhrase = $reasonPhrase;
-        $new->_setContentType();
+        $new->_setStatus($code, $reasonPhrase);
 
         return $new;
     }
 
     /**
+     * Modifier for response status
+     *
+     * @param int $code The code to set.
+     * @param string $reasonPhrase The response reason phrase.
+     * @return void
+     */
+    protected function _setStatus($code, $reasonPhrase = '')
+    {
+        $this->_status = $code;
+        if (empty($reasonPhrase) && isset($this->_statusCodes[$code])) {
+            $reasonPhrase = $this->_statusCodes[$code];
+        }
+        $this->_reasonPhrase = $reasonPhrase;
+        $this->_setContentType();
+    }
+
+    /**
      * Gets the response reason phrase associated with the status code.
      *
      * Because a reason phrase is not a required element in a response
@@ -2578,20 +2586,15 @@ class Response implements ResponseInterface
         }
 
         if ($start > $end || $end > $lastByte || $start > $lastByte) {
-            $this->statusCode(416);
-            $this->header([
-                'Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize
-            ]);
+            $this->_setStatus(416);
+            $this->_setHeader('Content-Range', 'bytes 0-' . $lastByte . '/' . $fileSize);
 
             return;
         }
 
-        $this->header([
-            'Content-Length' => $end - $start + 1,
-            'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize
-        ]);
-
-        $this->statusCode(206);
+        $this->_setHeader('Content-Length', $end - $start + 1);
+        $this->_setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $fileSize);
+        $this->_setStatus(206);
         $this->_fileRange = [$start, $end];
     }
 

+ 95 - 93
tests/TestCase/Http/ResponseTest.php

@@ -2696,143 +2696,145 @@ class ResponseTest extends TestCase
     /**
      * testFileRangeOffsetsNoDownload method
      *
+     * @group deprecated
      * @dataProvider rangeProvider
      * @return void
      */
     public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
     {
-        $_SERVER['HTTP_RANGE'] = $range;
-        $response = $this->getMockBuilder('Cake\Http\Response')
-            ->setMethods([
-                'header',
-                'type',
-                '_sendHeader',
-                '_isActive',
-            ])
-            ->getMock();
+        $this->deprecated(function () use ($range, $length, $offsetResponse) {
+            $_SERVER['HTTP_RANGE'] = $range;
+            $response = $this->getMockBuilder('Cake\Http\Response')
+                ->setMethods([
+                    'header',
+                    'type',
+                    '_sendHeader',
+                    '_isActive',
+                ])
+                ->getMock();
 
-        $response->expects($this->at(1))
-            ->method('header')
-            ->with('Accept-Ranges', 'bytes');
+            $response->expects($this->at(1))
+                ->method('header')
+                ->with('Accept-Ranges', 'bytes');
 
-        $response->expects($this->at(2))
-            ->method('header')
-            ->with([
-                'Content-Length' => $length,
-                'Content-Range' => $offsetResponse,
-            ]);
+            $response->expects($this->at(2))
+                ->method('header')
+                ->with([
+                    'Content-Length' => $length,
+                    'Content-Range' => $offsetResponse,
+                ]);
 
-        $response->expects($this->any())
-            ->method('_isActive')
-            ->will($this->returnValue(true));
+            $response->expects($this->any())
+                ->method('_isActive')
+                ->will($this->returnValue(true));
 
-        $response->file(
-            TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
-            ['download' => false]
-        );
+            $response->file(
+                TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
+                ['download' => false]
+            );
 
-        ob_start();
-        $result = $response->send();
-        ob_get_clean();
+            ob_start();
+            $result = $response->send();
+            ob_get_clean();
+        });
     }
 
     /**
      * testFileRangeNoDownload method
      *
+     * @group deprecated
      * @return void
      */
     public function testFileRangeNoDownload()
     {
-        $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
-        $response = $this->getMockBuilder('Cake\Http\Response')
-            ->setMethods([
-                'header',
-                'type',
-                '_sendHeader',
-                '_isActive',
-            ])
-            ->getMock();
+        $this->deprecated(function () {
+            $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
+            $response = $this->getMockBuilder('Cake\Http\Response')
+                ->setMethods([
+                    'header',
+                    'type',
+                    '_sendHeader',
+                    '_isActive',
+                ])
+                ->getMock();
 
-        $response->expects($this->exactly(1))
-            ->method('type')
-            ->with('css')
-            ->will($this->returnArgument(0));
+            $response->expects($this->exactly(1))
+                ->method('type')
+                ->with('css')
+                ->will($this->returnArgument(0));
 
-        $response->expects($this->at(1))
-            ->method('header')
-            ->with('Accept-Ranges', 'bytes');
+            $response->expects($this->at(1))
+                ->method('header')
+                ->with('Accept-Ranges', 'bytes');
 
-        $response->expects($this->at(2))
-            ->method('header')
-            ->with([
-                'Content-Length' => 18,
-                'Content-Range' => 'bytes 8-25/38',
-            ]);
-
-        $response->expects($this->any())
-            ->method('_isActive')
-            ->will($this->returnValue(true));
+            $response->expects($this->at(2))
+                ->method('header')
+                ->with([
+                    'Content-Length' => 18,
+                    'Content-Range' => 'bytes 8-25/38',
+                ]);
 
-        $response->file(
-            TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
-            ['download' => false]
-        );
+            $response->expects($this->any())
+                ->method('_isActive')
+                ->will($this->returnValue(true));
 
-        ob_start();
-        $result = $response->send();
-        $output = ob_get_clean();
-        $this->assertEquals(206, $response->statusCode());
-        $this->assertEquals('is the test asset ', $output);
-        $this->assertNotSame(false, $result);
+            $response->file(
+                TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
+                ['download' => false]
+            );
+
+            ob_start();
+            $result = $response->send();
+            $output = ob_get_clean();
+            $this->assertEquals(206, $response->statusCode());
+            $this->assertEquals('is the test asset ', $output);
+            $this->assertNotSame(false, $result);
+        });
     }
 
     /**
      * testFileRangeInvalidNoDownload method
      *
+     * @group deprecated
      * @return void
      */
     public function testFileRangeInvalidNoDownload()
     {
-        $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
-        $response = $this->getMockBuilder('Cake\Http\Response')
-            ->setMethods([
-                'header',
-                'type',
-                '_sendHeader',
-                '_isActive',
-            ])
-            ->getMock();
-
-        $response->expects($this->at(1))
-            ->method('header')
-            ->with('Accept-Ranges', 'bytes');
-
-        $response->expects($this->at(2))
-            ->method('header')
-            ->with([
-                'Content-Range' => 'bytes 0-37/38',
-            ]);
+        $this->deprecated(function () {
+            $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
+            $response = $this->getMockBuilder('Cake\Http\Response')
+                ->setMethods([
+                    '_sendHeader',
+                ])
+                ->getMock();
 
-        $response->file(
-            TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
-            ['download' => false]
-        );
+            $response->file(
+                TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
+                ['download' => false]
+            );
 
-        $this->assertEquals(416, $response->statusCode());
-        $result = $response->send();
+            $this->assertEquals(416, $response->statusCode());
+            $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
+            $this->assertEquals('text/css', $response->getType());
+            $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
+            $result = $response->send();
+        });
     }
 
     /**
      * Test the location method.
      *
+     * @group deprecated
      * @return void
      */
     public function testLocation()
     {
-        $response = new Response();
-        $this->assertNull($response->location(), 'No header should be set.');
-        $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
-        $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
+        $this->deprecated(function () {
+            $response = new Response();
+            $this->assertNull($response->location(), 'No header should be set.');
+            $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
+            $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
+        });
     }
 
     /**