From 9f139374059e250534031790b67a4a70b6921bd7 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Sat, 28 Oct 2023 14:13:44 +0700 Subject: [PATCH] chore: Showcase different behaviors of PHP when receiving multiple values of query parameter --- .../src/Service/HttpClientService.php | 2 +- .../consumer/tests/Service/MatchersTest.php | 17 +++++++++++-- .../matchersConsumer-matchersProvider.json | 24 ++++++++++++++++++- example/matchers/provider/public/index.php | 1 + 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/example/matchers/consumer/src/Service/HttpClientService.php b/example/matchers/consumer/src/Service/HttpClientService.php index 21376139e..efbf8338c 100644 --- a/example/matchers/consumer/src/Service/HttpClientService.php +++ b/example/matchers/consumer/src/Service/HttpClientService.php @@ -20,7 +20,7 @@ public function getMatchers(): array { $response = $this->httpClient->get("{$this->baseUri}/matchers", [ 'headers' => ['Accept' => 'application/json'], - 'query' => 'ignore=statusCode&pages=2&pages=3', + 'query' => 'ignore=statusCode&pages=2&pages=3&locales[]=fr-BE&locales[]=ru-RU', ]); return \json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); diff --git a/example/matchers/consumer/tests/Service/MatchersTest.php b/example/matchers/consumer/tests/Service/MatchersTest.php index 6ceae1e63..5f33ff058 100644 --- a/example/matchers/consumer/tests/Service/MatchersTest.php +++ b/example/matchers/consumer/tests/Service/MatchersTest.php @@ -27,8 +27,11 @@ public function testGetMatchers() ->setPath($this->matcher->regex('/matchers', '^\/matchers$')) ->setQuery([ 'ignore' => 'statusCode', - 'pages' => [ - json_encode($this->matcher->regex([1], '\d+')), + 'pages' => [ // Consumer send multiple values, but provider receive single (last) value + json_encode($this->matcher->regex([1, 22], '\d+')), + ], + 'locales[]' => [ // Consumer send multiple values, provider receive all values + json_encode($this->matcher->regex(['en-US', 'en-AU'], '^[a-z]{2}-[A-Z]{2}$')), ], ]) ->addHeader('Accept', 'application/json'); @@ -77,6 +80,11 @@ public function testGetMatchers() 'notEmpty' => $this->matcher->notEmpty(['1','2','3']), 'semver' => $this->matcher->semver('10.0.0-alpha4'), 'contentType' => $this->matcher->contentType('text/html'), + 'query' => [ + 'ignore' => 'statusCode', + 'pages' => '22', + 'locales' => ['en-US', 'en-AU'], + ], ]); $config = new MockServerConfig(); @@ -140,6 +148,11 @@ public function testGetMatchers() 'notEmpty' => ['1', '2', '3'], 'semver' => '10.0.0-alpha4', 'contentType' => 'text/html', + 'query' => [ + 'ignore' => 'statusCode', + 'pages' => '22', + 'locales' => ['en-US', 'en-AU'], + ], ], $matchersResult); } } diff --git a/example/matchers/pacts/matchersConsumer-matchersProvider.json b/example/matchers/pacts/matchersConsumer-matchersProvider.json index 5eb16048a..beecd99d9 100644 --- a/example/matchers/pacts/matchersConsumer-matchersProvider.json +++ b/example/matchers/pacts/matchersConsumer-matchersProvider.json @@ -37,6 +37,15 @@ "regex": "\\d+" } ] + }, + "$['locales[]']": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "^[a-z]{2}-[A-Z]{2}$" + } + ] } } }, @@ -46,8 +55,13 @@ "ignore": [ "statusCode" ], + "locales[]": [ + "en-US", + "en-AU" + ], "pages": [ - "1" + "1", + "22" ] } }, @@ -106,6 +120,14 @@ ], "nullValue": null, "number": 123, + "query": { + "ignore": "statusCode", + "locales": [ + "en-US", + "en-AU" + ], + "pages": "22" + }, "regex": "500 miles", "semver": "10.0.0-alpha4", "time": "23:59::58", diff --git a/example/matchers/provider/public/index.php b/example/matchers/provider/public/index.php index b991881e2..4721e98a3 100644 --- a/example/matchers/provider/public/index.php +++ b/example/matchers/provider/public/index.php @@ -60,6 +60,7 @@ HTML, + 'query' => $request->getQueryParams(), ])); return $response->withHeader('Content-Type', 'application/json');