|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/zendframework/zend-diactoros for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace ZendTest\Diactoros\Response; |
| 11 | + |
| 12 | +use InvalidArgumentException; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Psr\Http\Message\StreamInterface; |
| 15 | +use Zend\Diactoros\Response\CsvResponse; |
| 16 | + |
| 17 | +class CsvResponseTest extends TestCase |
| 18 | +{ |
| 19 | + const VALID_CSV_BODY = <<<EOF |
| 20 | +"first","last","email","dob", |
| 21 | +"john","citizen","[email protected]","01/01/1970", |
| 22 | +EOF; |
| 23 | + |
| 24 | + public function testConstructorAcceptsBodyAsString() |
| 25 | + { |
| 26 | + $response = new CsvResponse(self::VALID_CSV_BODY); |
| 27 | + $this->assertSame(self::VALID_CSV_BODY, (string) $response->getBody()); |
| 28 | + $this->assertSame(200, $response->getStatusCode()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testConstructorAllowsPassingStatus() |
| 32 | + { |
| 33 | + $status = 404; |
| 34 | + |
| 35 | + $response = new CsvResponse(self::VALID_CSV_BODY, $status); |
| 36 | + $this->assertSame(404, $response->getStatusCode()); |
| 37 | + $this->assertSame(self::VALID_CSV_BODY, (string) $response->getBody()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testConstructorAllowsPassingHeaders() |
| 41 | + { |
| 42 | + $status = 404; |
| 43 | + $headers = [ |
| 44 | + 'x-custom' => [ 'foo-bar' ], |
| 45 | + ]; |
| 46 | + |
| 47 | + $response = new CsvResponse(self::VALID_CSV_BODY, $status, $headers); |
| 48 | + $this->assertSame(['foo-bar'], $response->getHeader('x-custom')); |
| 49 | + $this->assertSame('text/csv; charset=utf-8', $response->getHeaderLine('content-type')); |
| 50 | + $this->assertSame(404, $response->getStatusCode()); |
| 51 | + $this->assertSame(self::VALID_CSV_BODY, (string) $response->getBody()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testAllowsStreamsForResponseBody() |
| 55 | + { |
| 56 | + $stream = $this->prophesize(StreamInterface::class); |
| 57 | + $body = $stream->reveal(); |
| 58 | + $response = new CsvResponse($body); |
| 59 | + $this->assertSame($body, $response->getBody()); |
| 60 | + } |
| 61 | + |
| 62 | + public function invalidContent() |
| 63 | + { |
| 64 | + return [ |
| 65 | + 'null' => [null], |
| 66 | + 'true' => [true], |
| 67 | + 'false' => [false], |
| 68 | + 'zero' => [0], |
| 69 | + 'int' => [1], |
| 70 | + 'zero-float' => [0.0], |
| 71 | + 'float' => [1.1], |
| 72 | + 'array' => [['php://temp']], |
| 73 | + 'object' => [(object) ['php://temp']], |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @dataProvider invalidContent |
| 79 | + */ |
| 80 | + public function testRaisesExceptionforNonStringNonStreamBodyContent($body) |
| 81 | + { |
| 82 | + $this->expectException(InvalidArgumentException::class); |
| 83 | + |
| 84 | + new CsvResponse($body); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @group 115 |
| 89 | + */ |
| 90 | + public function testConstructorRewindsBodyStream() |
| 91 | + { |
| 92 | + $response = new CsvResponse(self::VALID_CSV_BODY); |
| 93 | + |
| 94 | + $actual = $response->getBody()->getContents(); |
| 95 | + $this->assertSame(self::VALID_CSV_BODY, $actual); |
| 96 | + } |
| 97 | +} |
0 commit comments