|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 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\Parameters; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use PHPUnit\Framework\Attributes\Group; |
| 18 | +use stdClass; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('Others')] |
| 24 | +final class ParametersTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var array<string, mixed> |
| 28 | + */ |
| 29 | + private array $original = []; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->original = [ |
| 34 | + 'DOCUMENT_ROOT' => '', |
| 35 | + 'DISPLAY' => '1', |
| 36 | + 'SCRIPT_NAME' => 'vendor/bin/phpunit', |
| 37 | + 'REQUEST_TIME_FLOAT' => 1741522635.661474, |
| 38 | + 'IS_HTTPS' => true, |
| 39 | + 'PHP_NULLABLE_VAR' => null, |
| 40 | + 'OBJECT' => new stdClass(), |
| 41 | + 'argc' => 3, |
| 42 | + 'argv' => [ |
| 43 | + 0 => 'vendor/bin/phpunit', |
| 44 | + 1 => './tests/ParametersTest.php', |
| 45 | + 2 => '--no-coverage', |
| 46 | + ], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + public function testCreateParametersAndCompareIdentity(): void |
| 51 | + { |
| 52 | + $parameters = new Parameters($this->original); |
| 53 | + |
| 54 | + $this->assertSame($this->original, $parameters->all()); |
| 55 | + $this->assertSame($this->original, iterator_to_array($parameters->getIterator())); |
| 56 | + } |
| 57 | + |
| 58 | + public function testCreateEmptyParameters(): void |
| 59 | + { |
| 60 | + $parameters = new Parameters(); |
| 61 | + |
| 62 | + $this->assertSame([], $parameters->all()); |
| 63 | + } |
| 64 | + |
| 65 | + public function testGetValues(): void |
| 66 | + { |
| 67 | + $parameters = new Parameters($this->original); |
| 68 | + |
| 69 | + foreach ($parameters->keys() as $key) { |
| 70 | + $this->assertSame($this->original[$key], $parameters->get($key)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public function testUpdateAndSetNewValues(): void |
| 75 | + { |
| 76 | + /** |
| 77 | + * @var array<string, mixed> |
| 78 | + */ |
| 79 | + $expected = [ |
| 80 | + 'DOCUMENT_ROOT' => '/www', |
| 81 | + 'DISPLAY' => '0', |
| 82 | + 'SCRIPT_NAME' => '', |
| 83 | + 'REQUEST_TIME_FLOAT' => 1741522600.661400, |
| 84 | + 'IS_HTTPS' => false, |
| 85 | + 'PHP_NULLABLE_VAR' => null, |
| 86 | + 'OBJECT' => new stdClass(), |
| 87 | + 'argc' => 2, |
| 88 | + 'argv' => [ |
| 89 | + 0 => 'bin/phpunit', |
| 90 | + 1 => './ParametersTest.php', |
| 91 | + ], |
| 92 | + 'XDEBUG' => 'enabled', |
| 93 | + ]; |
| 94 | + |
| 95 | + $parameters = new Parameters($this->original); |
| 96 | + |
| 97 | + foreach (array_keys($expected) as $key) { |
| 98 | + $parameters->set($key, $expected[$key]); |
| 99 | + } |
| 100 | + |
| 101 | + $this->assertSame($expected, $parameters->all()); |
| 102 | + } |
| 103 | + |
| 104 | + public function testOverrideParameters(): void |
| 105 | + { |
| 106 | + /** |
| 107 | + * @var array<string, mixed> |
| 108 | + */ |
| 109 | + $expected = [ |
| 110 | + 'XDEBUG' => 'enabled', |
| 111 | + 'DOCUMENT_ROOT' => '/www', |
| 112 | + 'DISPLAY' => '0', |
| 113 | + 'SCRIPT_NAME' => '', |
| 114 | + 'REQUEST_TIME_FLOAT' => 1741522600.661400, |
| 115 | + 'IS_HTTPS' => false, |
| 116 | + 'PHP_NULLABLE_VAR' => null, |
| 117 | + 'OBJECT' => new stdClass(), |
| 118 | + 'argc' => 2, |
| 119 | + 'argv' => [ |
| 120 | + 0 => 'bin/phpunit', |
| 121 | + 1 => './ParametersTest.php', |
| 122 | + ], |
| 123 | + ]; |
| 124 | + |
| 125 | + $parameters = new Parameters($this->original); |
| 126 | + |
| 127 | + $parameters->override($expected); |
| 128 | + |
| 129 | + $this->assertSame($expected, $parameters->all()); |
| 130 | + } |
| 131 | + |
| 132 | + public function testGetUndefinedParametersWithDefaultValueReturn(): void |
| 133 | + { |
| 134 | + $parameters = new Parameters([]); |
| 135 | + |
| 136 | + $this->assertNull($parameters->get('undefined')); |
| 137 | + $this->assertInstanceOf(stdClass::class, $parameters->get('undefined', new stdClass())); |
| 138 | + $this->assertSame('', $parameters->get('undefined', '')); |
| 139 | + $this->assertSame(1000, $parameters->get('undefined', 1000)); |
| 140 | + $this->assertSame(['name' => 'Ivan'], $parameters->get('undefined', ['name' => 'Ivan'])); |
| 141 | + $this->assertEqualsWithDelta(10.00, $parameters->get('undefined', 10.00), PHP_FLOAT_EPSILON); |
| 142 | + } |
| 143 | + |
| 144 | + public function testRemoveKeys(): void |
| 145 | + { |
| 146 | + $parameters = new Parameters($this->original); |
| 147 | + |
| 148 | + $parameters->remove('argc'); |
| 149 | + $parameters->remove('argv'); |
| 150 | + |
| 151 | + unset($this->original['argc'], $this->original['argv']); |
| 152 | + |
| 153 | + $this->assertSame($this->original, $parameters->all()); |
| 154 | + } |
| 155 | + |
| 156 | + public function testCount(): void |
| 157 | + { |
| 158 | + $parameters = new Parameters($this->original); |
| 159 | + |
| 160 | + $this->assertSame(count($this->original), $parameters->count()); |
| 161 | + |
| 162 | + $parameters->remove('DOCUMENT_ROOT'); |
| 163 | + $parameters->remove('DISPLAY'); |
| 164 | + |
| 165 | + $this->assertSame(count($this->original) - 2, $parameters->count()); |
| 166 | + } |
| 167 | +} |
0 commit comments