Skip to content

Commit 27b6c85

Browse files
authored
Improve query parameter test (#721)
Regardless of the implementation details, any new pattern needs thorough validation. By writing strict tests now, we can reduce regressions when we change the client’s internals in the future. follow-up: #719
1 parent a2a3e30 commit 27b6c85

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# LINE Messaging API SDK for PHP
22

33
[![Build Status](https://github.com/line/line-bot-sdk-php/actions/workflows/php-checks.yml/badge.svg?branch=master)](https://github.com/line/line-bot-sdk-php/actions)
4+
[![Packagist Version](https://img.shields.io/packagist/v/linecorp/line-bot-sdk)](https://packagist.org/packages/linecorp/line-bot-sdk)
45

56

67
## Introduction

test/clients/messaging-api/Api/MessagingApiApiTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace LINE\Tests\Clients\MessagingApi\Api;
1919

2020
use GuzzleHttp\ClientInterface;
21+
use GuzzleHttp\Psr7\Query;
2122
use GuzzleHttp\Psr7\Request;
2223
use GuzzleHttp\Psr7\Response;
2324
use LINE\Clients\MessagingApi\Api\MessagingApiApi;
@@ -30,6 +31,7 @@
3031
use LINE\Clients\MessagingApi\Model\MessagingApiPagerCouponListResponse;
3132
use Mockery;
3233
use PHPUnit\Framework\TestCase;
34+
use Psr\Http\Message\UriInterface;
3335

3436
class MessagingApiApiTest extends TestCase
3537
{
@@ -38,6 +40,25 @@ protected function setUp(): void
3840
parent::setUp();
3941
}
4042

43+
private function assertQueryEquals(array $expected, UriInterface $uri): void
44+
{
45+
$actual = Query::parse($uri->getQuery());
46+
47+
$normalize = function (array &$arr): void {
48+
foreach ($arr as &$v) {
49+
if (is_array($v)) {
50+
sort($v);
51+
}
52+
}
53+
ksort($arr);
54+
};
55+
56+
$normalize($expected);
57+
$normalize($actual);
58+
59+
$this->assertSame($expected, $actual, 'Query parameters mismatch');
60+
}
61+
4162
public function testGetFollowers(): void
4263
{
4364
$client = Mockery::mock(ClientInterface::class);
@@ -276,6 +297,11 @@ public function testListCoupon(): void
276297
$start = 'startToken';
277298
$limit = 10;
278299
$contentType = 'application/json';
300+
$expectedQuery = [
301+
'status' => $status,
302+
'start' => $start,
303+
'limit' => (string)$limit,
304+
];
279305
$expectedRequestBody = <<<JSON
280306
{
281307
"status": ["RUNNING", "CLOSED"],
@@ -296,14 +322,11 @@ public function testListCoupon(): void
296322
$client = Mockery::mock(ClientInterface::class);
297323
$client->shouldReceive('send')
298324
->with(
299-
Mockery::on(function (Request $request) use ($status, $start, $limit, $contentType) {
325+
Mockery::on(function (Request $request) use ($status, $start, $limit, $contentType, $expectedQuery) {
300326
$this->assertEquals('GET', $request->getMethod());
301327
$this->assertStringContainsString('https://api.line.me/v2/bot/coupon?', (string)$request->getUri());
302328
$this->assertEquals($contentType, $request->getHeaderLine('Content-Type'));
303-
$this->assertStringContainsString('status=RUNNING', (string)$request->getUri()->getQuery());
304-
$this->assertStringContainsString('status=CLOSED', (string)$request->getUri()->getQuery());
305-
$this->assertStringContainsString('start=startToken', (string)$request->getUri()->getQuery());
306-
$this->assertStringContainsString('limit=10', (string)$request->getUri()->getQuery());
329+
$this->assertQueryEquals($expectedQuery, $request->getUri());
307330
return true;
308331
}),
309332
[]

0 commit comments

Comments
 (0)