Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
68 / 68 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use Nyholm\Psr7\Response; |
| 13 | use Phexium\Presentation\ResponseBuilder; |
| 14 | |
| 15 | describe('Response creation', function (): void { |
| 16 | it('initializes with custom response', function (): void { |
| 17 | $customResponse = new Response(404, ['X-Custom' => 'toto'], 'HTML'); |
| 18 | |
| 19 | $builder = new ResponseBuilder($customResponse); |
| 20 | |
| 21 | $result = $builder->build(); |
| 22 | |
| 23 | expect($result->getStatusCode())->toBe(404); |
| 24 | expect($result->getHeader('X-Custom'))->toBe(['toto']); |
| 25 | expect($result->getBody()->getContents())->toBe('HTML'); |
| 26 | }); |
| 27 | |
| 28 | it('replaces response with withResponse()', function (): void { |
| 29 | $customResponse = new Response(404, ['X-Custom' => 'toto'], 'HTML'); |
| 30 | |
| 31 | $builder = new ResponseBuilder(new Response()); |
| 32 | |
| 33 | $result = $builder->build(); |
| 34 | |
| 35 | expect($result->getStatusCode())->toBe(200); |
| 36 | expect($result->getHeader('X-Custom'))->toBe([]); |
| 37 | expect($result->getBody()->getContents())->toBe(''); |
| 38 | |
| 39 | $result = $builder->withResponse($customResponse)->build(); |
| 40 | |
| 41 | expect($result->getStatusCode())->toBe(404); |
| 42 | expect($result->getHeader('X-Custom'))->toBe(['toto']); |
| 43 | expect($result->getBody()->getContents())->toBe('HTML'); |
| 44 | }); |
| 45 | |
| 46 | it('builds HTML response with default charset', function (): void { |
| 47 | $builder = new ResponseBuilder(new Response()); |
| 48 | |
| 49 | $result = $builder |
| 50 | ->withHtml('<h1>Hello World</h1>') |
| 51 | ->build() |
| 52 | ; |
| 53 | |
| 54 | expect($result->getStatusCode())->toBe(200); |
| 55 | expect($result->getHeaderLine('Content-Type'))->toBe('text/html; charset=utf-8'); |
| 56 | expect((string) $result->getBody())->toBe('<h1>Hello World</h1>'); |
| 57 | }); |
| 58 | |
| 59 | it('builds JSON response with default charset', function (): void { |
| 60 | $builder = new ResponseBuilder(new Response()); |
| 61 | |
| 62 | $result = $builder |
| 63 | ->withJson(['message' => 'Hello World']) |
| 64 | ->build() |
| 65 | ; |
| 66 | |
| 67 | expect($result->getStatusCode())->toBe(200); |
| 68 | expect($result->getHeaderLine('Content-Type'))->toBe('application/json; charset=utf-8'); |
| 69 | expect((string) $result->getBody())->json()->toBe(['message' => 'Hello World']); |
| 70 | }); |
| 71 | |
| 72 | it('sets custom status code', function (): void { |
| 73 | $builder = new ResponseBuilder(new Response()); |
| 74 | |
| 75 | $result = $builder->withStatus(404)->build(); |
| 76 | |
| 77 | expect($result->getStatusCode())->toBe(404); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | describe('Headers', function (): void { |
| 82 | it('replaces existing header with withHeader()', function (): void { |
| 83 | $builder = new ResponseBuilder(new Response()); |
| 84 | |
| 85 | $result = $builder |
| 86 | ->withHeader('X-Custom', 'value1') |
| 87 | ->withHeader('X-Custom', 'value2') |
| 88 | ->build() |
| 89 | ; |
| 90 | |
| 91 | expect($result->getHeader('X-Custom'))->toBe(['value2']); |
| 92 | }); |
| 93 | |
| 94 | it('appends header value with addHeader()', function (): void { |
| 95 | $builder = new ResponseBuilder(new Response()); |
| 96 | |
| 97 | $result = $builder |
| 98 | ->withHeader('X-Custom', 'value1') |
| 99 | ->addHeader('X-Custom', 'value2') |
| 100 | ->build() |
| 101 | ; |
| 102 | |
| 103 | expect($result->getHeader('X-Custom'))->toBe(['value1', 'value2']); |
| 104 | }); |
| 105 | }); |