Skip to content

Commit 8bed88d

Browse files
[12.x] Add test coverage for Uri::withQueryIfMissing method (#54923)
* Add initial test for Uri::withQueryIfMissing method This test verifies that the withQueryIfMissing method adds parameters only when they don't exist while preserving the values of existing parameters. * Expand withQueryIfMissing tests to cover nested arrays This commit adds cases for complex nested arrays and indexed arrays to ensure the method correctly handles multi-dimensional data structures in query parameters. * Complete withQueryIfMissing tests for advanced array handling This commit finalizes tests for the withQueryIfMissing method, covering: - Partial merging of associative arrays - Preservation of indexed arrays - Verification of both encoded query strings and parsed arrays * docs: add PHPDoc annotation for withQueryIfMissing method * Update Uri.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 77672fd commit 8bed88d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/Support/SupportUriTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,66 @@ public function test_decoding_the_entire_uri()
126126

127127
$this->assertEquals('https://laravel.com/docs/11.x/installation?tags[0]=first&tags[1]=second', $uri->decode());
128128
}
129+
130+
public function test_with_query_if_missing()
131+
{
132+
// Test adding new parameters while preserving existing ones
133+
$uri = Uri::of('https://laravel.com?existing=value');
134+
135+
$uri = $uri->withQueryIfMissing([
136+
'new' => 'parameter',
137+
'existing' => 'new_value',
138+
]);
139+
140+
$this->assertEquals('existing=value&new=parameter', $uri->query()->decode());
141+
142+
// Test adding complex nested arrays to empty query string
143+
$uri = Uri::of('https://laravel.com');
144+
145+
$uri = $uri->withQueryIfMissing([
146+
'name' => 'Taylor',
147+
'role' => [
148+
'title' => 'Developer',
149+
'focus' => 'PHP',
150+
],
151+
'tags' => [
152+
'person',
153+
'employee',
154+
],
155+
]);
156+
157+
$this->assertEquals('name=Taylor&role[title]=Developer&role[focus]=PHP&tags[0]=person&tags[1]=employee', $uri->query()->decode());
158+
159+
// Test partial array merging and preserving indexed arrays
160+
$uri = Uri::of('https://laravel.com?name=Taylor&tags[0]=person');
161+
162+
$uri = $uri->withQueryIfMissing([
163+
'name' => 'Changed',
164+
'age' => 38,
165+
'tags' => ['should', 'not', 'change'],
166+
]);
167+
168+
$this->assertEquals('name=Taylor&tags[0]=person&age=38', $uri->query()->decode());
169+
$this->assertEquals(['name' => 'Taylor', 'tags' => ['person'], 'age' => 38], $uri->query()->all());
170+
171+
$uri = Uri::of('https://laravel.com?user[name]=Taylor');
172+
173+
$uri = $uri->withQueryIfMissing([
174+
'user' => [
175+
'name' => 'Should Not Change',
176+
'age' => 38,
177+
],
178+
'settings' => [
179+
'theme' => 'dark',
180+
],
181+
]);
182+
$this->assertEquals([
183+
'user' => [
184+
'name' => 'Taylor',
185+
],
186+
'settings' => [
187+
'theme' => 'dark',
188+
],
189+
], $uri->query()->all());
190+
}
129191
}

0 commit comments

Comments
 (0)