|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of php-fast-forward/http-message. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @link https://github.com/php-fast-forward/http-message |
| 12 | + * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <[email protected]> |
| 13 | + * @license https://opensource.org/licenses/MIT MIT License |
| 14 | + */ |
| 15 | + |
| 16 | +namespace FastForward\Http\Message\Tests; |
| 17 | + |
| 18 | +use FastForward\Http\Message\RedirectResponse; |
| 19 | +use FastForward\Http\Message\StatusCode; |
| 20 | +use Nyholm\Psr7\Uri; |
| 21 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 22 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +#[CoversClass(RedirectResponse::class)] |
| 29 | +#[UsesClass(StatusCode::class)] |
| 30 | +final class RedirectResponseTest extends TestCase |
| 31 | +{ |
| 32 | + public function testConstructorWillSetLocationHeaderAndTemporaryStatus(): void |
| 33 | + { |
| 34 | + $uri = 'https://example.com'; |
| 35 | + |
| 36 | + $response = new RedirectResponse($uri); |
| 37 | + |
| 38 | + self::assertSame(StatusCode::Found->value, $response->getStatusCode()); |
| 39 | + self::assertSame(StatusCode::Found->getReasonPhrase(), $response->getReasonPhrase()); |
| 40 | + self::assertSame('https://example.com', $response->getHeaderLine('Location')); |
| 41 | + } |
| 42 | + |
| 43 | + public function testConstructorWillSetLocationHeaderAndPermanentStatus(): void |
| 44 | + { |
| 45 | + $uri = 'https://example.com/redirect'; |
| 46 | + |
| 47 | + $response = new RedirectResponse($uri, permanent: true); |
| 48 | + |
| 49 | + self::assertSame(StatusCode::MovedPermanently->value, $response->getStatusCode()); |
| 50 | + self::assertSame(StatusCode::MovedPermanently->getReasonPhrase(), $response->getReasonPhrase()); |
| 51 | + self::assertSame('https://example.com/redirect', $response->getHeaderLine('Location')); |
| 52 | + } |
| 53 | + |
| 54 | + public function testConstructorAcceptsUriInterfaceInstance(): void |
| 55 | + { |
| 56 | + $uri = new Uri('/relative/path'); |
| 57 | + |
| 58 | + $response = new RedirectResponse($uri); |
| 59 | + |
| 60 | + self::assertSame('/relative/path', $response->getHeaderLine('Location')); |
| 61 | + } |
| 62 | + |
| 63 | + public function testConstructorWillPreserveAdditionalHeaders(): void |
| 64 | + { |
| 65 | + $uri = 'https://example.com'; |
| 66 | + $headers = [ |
| 67 | + 'X-Custom-Header' => 'test-value', |
| 68 | + ]; |
| 69 | + |
| 70 | + $response = new RedirectResponse($uri, permanent: false, headers: $headers); |
| 71 | + |
| 72 | + self::assertSame('test-value', $response->getHeaderLine('X-Custom-Header')); |
| 73 | + self::assertSame('https://example.com', $response->getHeaderLine('Location')); |
| 74 | + } |
| 75 | +} |
0 commit comments