From a536748e082adc3b6ed0236ba8aba8087ccd7263 Mon Sep 17 00:00:00 2001 From: mohammadrasoulasghari Date: Thu, 6 Mar 2025 20:45:32 +0330 Subject: [PATCH] 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 --- tests/Support/SupportUriTest.php | 36 ++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/Support/SupportUriTest.php b/tests/Support/SupportUriTest.php index d48a40916024..7e878233ec98 100644 --- a/tests/Support/SupportUriTest.php +++ b/tests/Support/SupportUriTest.php @@ -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()); @@ -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()); } }