Skip to content

Commit

Permalink
Complete withQueryIfMissing tests for advanced array handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mohammadrasoulasghari committed Mar 6, 2025
1 parent fb5d447 commit a536748
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tests/Support/SupportUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function test_with_query_if_missing()

$uri = $uri->withQueryIfMissing([
'new' => 'parameter',
'existing' => 'new_value'
'existing' => 'new_value',
]);

$this->assertEquals('existing=value&new=parameter', $uri->query()->decode());
Expand All @@ -151,9 +151,41 @@ public function test_with_query_if_missing()
'tags' => [
'person',
'employee',
]
],
]);

$this->assertEquals('name=Taylor&role[title]=Developer&role[focus]=PHP&tags[0]=person&tags[1]=employee', $uri->query()->decode());

// Test partial array merging and preserving indexed arrays
$uri = Uri::of('https://laravel.com?name=Taylor&tags[0]=person');

$uri = $uri->withQueryIfMissing([
'name' => 'Changed',
'age' => 38,
'tags' => ['should', 'not', 'change'],
]);

$this->assertEquals('name=Taylor&tags[0]=person&age=38', $uri->query()->decode());
$this->assertEquals(['name' => 'Taylor', 'tags' => ['person'], 'age' => 38], $uri->query()->all());

$uri = Uri::of('https://laravel.com?user[name]=Taylor');

$uri = $uri->withQueryIfMissing([
'user' => [
'name' => 'Should Not Change',
'age' => 38,
],
'settings' => [
'theme' => 'dark'
],
]);
$this->assertEquals([
'user' => [
'name' => 'Taylor',
],
'settings' => [
'theme' => 'dark'
],
], $uri->query()->all());
}
}

0 comments on commit a536748

Please sign in to comment.