Skip to content

Commit a972fdf

Browse files
committed
tests: Add tests for Parameters
1 parent 7aa832f commit a972fdf

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\Exceptions\InvalidArgumentException;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use PHPUnit\Framework\Attributes\Group;
19+
use stdClass;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[Group('Others')]
25+
final class InputParametersTest extends CIUnitTestCase
26+
{
27+
/**
28+
* @var array<string, mixed>
29+
*/
30+
private array $original = [
31+
'title' => '',
32+
'toolbar' => '1',
33+
'path' => 'public/index.php',
34+
'current_time' => 1741522635.661474,
35+
'debug' => true,
36+
'pages' => 15,
37+
'filters' => [
38+
0 => 'name',
39+
1 => 'sum',
40+
],
41+
'sort' => [
42+
'date' => 'ASC',
43+
'age' => 'DESC',
44+
],
45+
];
46+
47+
public function testGetNonScalarValues(): void
48+
{
49+
$parameters = new InputParameters($this->original);
50+
51+
$this->assertNull($parameters->get('undefined_or_null', null));
52+
53+
$this->expectException(InvalidArgumentException::class);
54+
55+
$parameters->get('undefined_throw', new stdClass());
56+
}
57+
58+
public function testAttemptSetNullValues(): void
59+
{
60+
$parameters = new InputParameters($this->original);
61+
62+
$this->expectException(InvalidArgumentException::class);
63+
64+
$parameters->set('nullable', null);
65+
}
66+
67+
public function testAttemptSetNonScalarValues(): void
68+
{
69+
$parameters = new InputParameters($this->original);
70+
71+
$this->expectException(InvalidArgumentException::class);
72+
73+
$parameters->set('nullable', null);
74+
}
75+
76+
public function testUpdateAndSetNewValues(): void
77+
{
78+
/**
79+
* @var array<string, mixed>
80+
*/
81+
$expected = [
82+
'title' => 'CodeIgniter',
83+
'toolbar' => '0',
84+
'path' => '',
85+
'current_time' => 1741522888.661434,
86+
'debug' => false,
87+
'pages' => 10,
88+
'filters' => [
89+
0 => 'sum',
90+
1 => 'name',
91+
],
92+
'sort' => [
93+
'age' => 'ASC',
94+
'date' => 'DESC',
95+
],
96+
'slug' => 'Ben-i-need-help',
97+
];
98+
99+
$parameters = new InputParameters($this->original);
100+
101+
foreach (array_keys($expected) as $key) {
102+
$parameters->set($key, $expected[$key]);
103+
}
104+
105+
$this->assertSame($expected, $parameters->all());
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)