|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\HTTP; |
| 15 | + |
| 16 | +use CodeIgniter\Config\Services; |
| 17 | +use CodeIgniter\HTTP\Exceptions\HTTPException; |
| 18 | +use CodeIgniter\Input\InputData; |
| 19 | +use CodeIgniter\Superglobals; |
| 20 | +use CodeIgniter\Test\CIUnitTestCase; |
| 21 | +use Config\App; |
| 22 | +use PHPUnit\Framework\Attributes\BackupGlobals; |
| 23 | +use PHPUnit\Framework\Attributes\Group; |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +#[BackupGlobals(true)] |
| 29 | +#[Group('SeparateProcess')] |
| 30 | +final class RequestInputTest extends CIUnitTestCase |
| 31 | +{ |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + Services::injectMock('superglobals', new Superglobals([], [], [], [], [], [])); |
| 37 | + } |
| 38 | + |
| 39 | + private function createRequest(?App $config = null, ?string $body = null): IncomingRequest |
| 40 | + { |
| 41 | + $config ??= new App(); |
| 42 | + |
| 43 | + return new IncomingRequest( |
| 44 | + $config, |
| 45 | + new SiteURI($config, ''), |
| 46 | + $body, |
| 47 | + new UserAgent(), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testInputReturnsRequestInput(): void |
| 52 | + { |
| 53 | + $request = $this->createRequest(); |
| 54 | + $input = $request->input(); |
| 55 | + |
| 56 | + $this->assertInstanceOf(RequestInput::class, $input); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGetReadsGetData(): void |
| 60 | + { |
| 61 | + service('superglobals')->setGet('page', '3'); |
| 62 | + service('superglobals')->setGet('filters', ['active' => 'true']); |
| 63 | + service('superglobals')->setPost('page', '10'); |
| 64 | + |
| 65 | + $input = $this->createRequest()->input()->get(); |
| 66 | + |
| 67 | + $this->assertInstanceOf(InputData::class, $input); |
| 68 | + $this->assertSame(3, $input->integer('page')); |
| 69 | + $this->assertTrue($input->boolean('filters.active')); |
| 70 | + $this->assertSame(1, $input->integer('missing', 1)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testPostReadsPostData(): void |
| 74 | + { |
| 75 | + service('superglobals')->setGet('remember', '0'); |
| 76 | + service('superglobals')->setPost('remember', '1'); |
| 77 | + service('superglobals')->setPost('tags', ['php', 'ci4']); |
| 78 | + |
| 79 | + $input = $this->createRequest()->input()->post(); |
| 80 | + |
| 81 | + $this->assertInstanceOf(InputData::class, $input); |
| 82 | + $this->assertTrue($input->boolean('remember')); |
| 83 | + $this->assertSame(['php', 'ci4'], $input->array('tags')); |
| 84 | + } |
| 85 | + |
| 86 | + public function testJsonReadsJsonBody(): void |
| 87 | + { |
| 88 | + $json = json_encode([ |
| 89 | + 'page' => '4', |
| 90 | + 'filters' => ['active' => 'true'], |
| 91 | + 'nullable' => null, |
| 92 | + ]); |
| 93 | + |
| 94 | + $input = $this->createRequest(new App(), $json)->input()->json(); |
| 95 | + |
| 96 | + $this->assertInstanceOf(InputData::class, $input); |
| 97 | + $this->assertSame(4, $input->integer('page')); |
| 98 | + $this->assertTrue($input->boolean('filters.active')); |
| 99 | + $this->assertTrue($input->has('nullable')); |
| 100 | + } |
| 101 | + |
| 102 | + public function testJsonReturnsEmptyInputForEmptyJsonBody(): void |
| 103 | + { |
| 104 | + $input = $this->createRequest(new App())->input()->json(); |
| 105 | + |
| 106 | + $this->assertInstanceOf(InputData::class, $input); |
| 107 | + $this->assertFalse($input->has('name')); |
| 108 | + } |
| 109 | + |
| 110 | + public function testJsonRejectsScalarJsonBody(): void |
| 111 | + { |
| 112 | + $this->expectException(HTTPException::class); |
| 113 | + $this->expectExceptionMessage('The provided JSON format is not supported.'); |
| 114 | + |
| 115 | + $this->createRequest(new App(), '"hello"')->input()->json(); |
| 116 | + } |
| 117 | + |
| 118 | + public function testJsonKeepsInvalidJsonError(): void |
| 119 | + { |
| 120 | + $this->expectException(HTTPException::class); |
| 121 | + $this->expectExceptionMessage('Failed to parse JSON string. Error: Syntax error'); |
| 122 | + |
| 123 | + $this->createRequest(new App(), 'Invalid JSON string')->input()->json(); |
| 124 | + } |
| 125 | + |
| 126 | + public function testRawReadsRawInputData(): void |
| 127 | + { |
| 128 | + $input = $this->createRequest(new App(), 'title=Hello&published=1')->input()->raw(); |
| 129 | + |
| 130 | + $this->assertInstanceOf(InputData::class, $input); |
| 131 | + $this->assertSame('Hello', $input->string('title')); |
| 132 | + $this->assertTrue($input->boolean('published')); |
| 133 | + } |
| 134 | +} |
0 commit comments