|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\DataResponse\Tests\Formatter; |
| 6 | + |
| 7 | +use LogicException; |
| 8 | +use Yiisoft\DataResponse\Formatter\PlainTextDataResponseFormatter; |
| 9 | +use Yiisoft\DataResponse\Tests\TestCase; |
| 10 | + |
| 11 | +final class PlainTextDataResponseFormatterTest extends TestCase |
| 12 | +{ |
| 13 | + public function testCorrectFormat(): void |
| 14 | + { |
| 15 | + $dataResponse = $this->createDataResponse('test'); |
| 16 | + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); |
| 17 | + $result->getBody()->rewind(); |
| 18 | + |
| 19 | + $this->assertSame( |
| 20 | + 'test', |
| 21 | + $result->getBody()->getContents(), |
| 22 | + ); |
| 23 | + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); |
| 24 | + } |
| 25 | + |
| 26 | + public function testWithEncoding(): void |
| 27 | + { |
| 28 | + $dataResponse = $this->createDataResponse('test'); |
| 29 | + $result = (new PlainTextDataResponseFormatter()) |
| 30 | + ->withEncoding('ISO-8859-1') |
| 31 | + ->format($dataResponse); |
| 32 | + $result->getBody()->rewind(); |
| 33 | + |
| 34 | + $this->assertSame( |
| 35 | + 'test', |
| 36 | + $result->getBody()->getContents(), |
| 37 | + ); |
| 38 | + $this->assertSame(['text/plain; charset=ISO-8859-1'], $result->getHeader('Content-Type')); |
| 39 | + } |
| 40 | + |
| 41 | + public function testWithContentType(): void |
| 42 | + { |
| 43 | + $dataResponse = $this->createDataResponse('test'); |
| 44 | + $result = (new PlainTextDataResponseFormatter()) |
| 45 | + ->withContentType('text/html') |
| 46 | + ->format($dataResponse); |
| 47 | + $result->getBody()->rewind(); |
| 48 | + |
| 49 | + $this->assertSame( |
| 50 | + 'test', |
| 51 | + $result->getBody()->getContents(), |
| 52 | + ); |
| 53 | + $this->assertSame('text/html; charset=UTF-8', $result->getHeaderLine('Content-Type')); |
| 54 | + } |
| 55 | + |
| 56 | + public function testWithIncorrectType(): void |
| 57 | + { |
| 58 | + $dataResponse = $this->createDataResponse(['test']); |
| 59 | + $formatter = new PlainTextDataResponseFormatter(); |
| 60 | + |
| 61 | + $this->expectException(LogicException::class); |
| 62 | + $this->expectExceptionMessage('Data must be either a scalar value, null, or a stringable object. array given.'); |
| 63 | + $formatter->format($dataResponse); |
| 64 | + } |
| 65 | + |
| 66 | + public function testDataWithNull(): void |
| 67 | + { |
| 68 | + $dataResponse = $this->createDataResponse(null); |
| 69 | + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); |
| 70 | + $result->getBody()->rewind(); |
| 71 | + |
| 72 | + $this->assertSame( |
| 73 | + '', |
| 74 | + $result->getBody()->getContents(), |
| 75 | + ); |
| 76 | + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); |
| 77 | + } |
| 78 | + |
| 79 | + public function testDataWithStringableObject(): void |
| 80 | + { |
| 81 | + $data = new class () { |
| 82 | + public function __toString(): string |
| 83 | + { |
| 84 | + return 'test'; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + $dataResponse = $this->createDataResponse($data); |
| 89 | + $result = (new PlainTextDataResponseFormatter())->format($dataResponse); |
| 90 | + $result->getBody()->rewind(); |
| 91 | + |
| 92 | + $this->assertSame( |
| 93 | + 'test', |
| 94 | + $result->getBody()->getContents(), |
| 95 | + ); |
| 96 | + $this->assertSame(['text/plain; charset=UTF-8'], $result->getHeader('Content-Type')); |
| 97 | + } |
| 98 | + |
| 99 | + public function testImmutability(): void |
| 100 | + { |
| 101 | + $formatter = new PlainTextDataResponseFormatter(); |
| 102 | + $this->assertNotSame($formatter, $formatter->withContentType('text/html')); |
| 103 | + $this->assertNotSame($formatter, $formatter->withEncoding('utf-8')); |
| 104 | + } |
| 105 | +} |
0 commit comments