/** * Get the stream for the new response. * * @param \Cake\Network\Response $response The cake response to extract the body from. * @return \Psr\Http\Message\StreamInterface|string The stream. */ protected static function getStream($response) { $stream = 'php://memory'; $body = $response->body(); if (is_string($body) && strlen($body)) { $stream = new Stream('php://memory', 'wb'); $stream->write($body); return $stream; } if (is_callable($body)) { $stream = new CallbackStream($body); return $stream; } $file = $response->getFile(); if ($file) { $stream = new Stream($file->path, 'rb'); return $stream; } return $stream; }
/** * test getFile method * * @return void */ public function testGetFile() { $response = new Response(); $this->assertNull($response->getFile(), 'No file to get'); $response->file(TEST_APP . 'vendor/css/test_asset.css'); $file = $response->getFile(); $this->assertInstanceOf('Cake\\Filesystem\\File', $file, 'Should get a file'); $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path); }
/** * Asserts that a file with the given name was sent in the response * * @param string $expected The file name that should be sent in the response * @param string $message The failure message that will be appended to the generated message. * @return void */ public function assertFileResponse($expected, $message = '') { if ($this->_response === null) { $this->fail('No response set, cannot assert file.'); } $actual = isset($this->_response->getFile()->path) ? $this->_response->getFile()->path : null; if ($actual === null) { $this->fail('No file was sent in this response'); } $this->assertEquals($expected, $actual, $message); }