diff --git a/composer.json b/composer.json index f0b16ac6..979383cd 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,11 @@ ], "require": { "php": ">=5.4", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^5.3", + "indigophp/hash-compat": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^5.3.2", + "phpunit/phpunit": "^4.8.24", "phpmd/phpmd": "~2.4", "squizlabs/php_codesniffer": "~2.6", "apigen/apigen": "~4.1" diff --git a/src/LINEBot/HTTPClient/GuzzleHTTPClient.php b/src/LINEBot/HTTPClient/GuzzleHTTPClient.php index f8589a6b..90c9d274 100644 --- a/src/LINEBot/HTTPClient/GuzzleHTTPClient.php +++ b/src/LINEBot/HTTPClient/GuzzleHTTPClient.php @@ -18,6 +18,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Stream\Stream; use LINE\LINEBot; use LINE\LINEBot\DownloadedContents; use LINE\LINEBot\Exception\ContentsDownloadingFailedException; @@ -67,7 +68,7 @@ public function __construct(array $args) public function get($url) { try { - $res = $this->guzzle->request('GET', $url, ['headers' => $this->credentials()]); + $res = $this->guzzle->get($url, ['headers' => $this->credentials()]); } catch (BadResponseException $e) { $res = $e->getResponse(); } @@ -108,7 +109,7 @@ public function post($url, array $data) ]); try { - $res = $this->guzzle->request('POST', $url, [ + $res = $this->guzzle->post($url, [ 'headers' => $headers, 'body' => $json, ]); @@ -145,11 +146,11 @@ public function downloadContents($url, $fileHandler = null) if ($fileHandler === null) { $fileHandler = tmpfile(); } - $stream = \GuzzleHttp\Psr7\stream_for($fileHandler); + $stream = Stream::factory($fileHandler); try { - $res = $this->guzzle->request('GET', $url, [ - 'sink' => $stream, + $res = $this->guzzle->get($url, [ + 'save_to' => $stream, 'headers' => $this->credentials(), ]); } catch (BadResponseException $e) { diff --git a/tests/LINEBot/GettingContentsTest.php b/tests/LINEBot/GettingContentsTest.php index b31f11a7..0f82c4fa 100644 --- a/tests/LINEBot/GettingContentsTest.php +++ b/tests/LINEBot/GettingContentsTest.php @@ -16,58 +16,66 @@ */ namespace LINE\Tests\LINEBot; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\Message\Request; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; +use GuzzleHttp\Subscriber\History; +use GuzzleHttp\Subscriber\Mock; use LINE\LINEBot; class GettingContentsTest extends \PHPUnit_Framework_TestCase { public function testGetUserProfile() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'GET'); - $this->assertEquals( - $req->getUri(), - 'https://trialbot-api.line.me/v1/profiles?mids=DUMMY_MID_GET_DISPLAY_NAME' - ); - - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"start":1,"display":1,"total":1,"count":1,"contacts":[{"displayName":"BOT API","mid":"u0047556f2e40dba2456887320ba7c76d","pictureUrl":"http://example.com/abcdefghijklmn","statusMessage":"Hello, LINE!"}]}' - ); - }, + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"start":1,"display":1,"total":1,"count":1,"contacts":[{"displayName":"BOT API","mid":"u0047556f2e40dba2456887320ba7c76d","pictureUrl":"http://example.com/abcdefghijklmn","statusMessage":"Hello, LINE!"}]}') + ), ]); - $mockHandler = HandlerStack::create($mock); $config = [ 'channelId' => '1000000000', 'channelSecret' => 'testsecret', 'channelMid' => 'TEST_MID', ]; + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); + $sdk = new LINEBot( $config, - new LINEBot\HTTPClient\GuzzleHTTPClient(array_merge($config, ['handler' => $mockHandler])) + new LINEBot\HTTPClient\GuzzleHTTPClient(array_merge($config, ['emitter' => $emitter])) ); $res = $sdk->getUserProfile('DUMMY_MID_GET_DISPLAY_NAME'); $this->assertEquals($res['count'], 1); $this->assertEquals($res['contacts'][0]['displayName'], 'BOT API'); + + $history = $histories->getIterator()[0]; + + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'GET'); + $this->assertEquals( + $req->getUrl(), + 'https://trialbot-api.line.me/v1/profiles?mids=DUMMY_MID_GET_DISPLAY_NAME' + ); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } } diff --git a/tests/LINEBot/MessageSendingTest.php b/tests/LINEBot/MessageSendingTest.php index 0d862735..de2e9a8e 100644 --- a/tests/LINEBot/MessageSendingTest.php +++ b/tests/LINEBot/MessageSendingTest.php @@ -16,10 +16,12 @@ */ namespace LINE\Tests\LINEBot; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\Message\Request; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; +use GuzzleHttp\Subscriber\History; +use GuzzleHttp\Subscriber\Mock; use LINE\LINEBot; use LINE\LINEBot\Constant\ContentType; use LINE\LINEBot\Constant\RecipientType; @@ -35,48 +37,34 @@ class MessageSendingTest extends \PHPUnit_Framework_TestCase public function testSendText() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - $this->assertEquals($data['content']['text'], 'hello!'); - $this->assertEquals($data['content']['contentType'], ContentType::TEXT); - $this->assertEquals($data['content']['toType'], RecipientType::USER); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460826285060","timestamp":1460826285060,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460826285060","timestamp":1460826285060,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendText(['DUMMY_MID'], 'hello!'); @@ -104,53 +92,63 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + $this->assertEquals($data['content']['text'], 'hello!'); + $this->assertEquals($data['content']['contentType'], ContentType::TEXT); + $this->assertEquals($data['content']['toType'], RecipientType::USER); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); + } public function testSendImage() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/image.jpg'); - $this->assertEquals($data['content']['previewImageUrl'], 'http://example.com/preview.jpg'); - $this->assertEquals($data['content']['contentType'], ContentType::IMAGE); - $this->assertEquals($data['content']['toType'], RecipientType::USER); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460826285060","timestamp":1460826285060,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460826285060","timestamp":1460826285060,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}')), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendImage(['DUMMY_MID'], 'http://example.com/image.jpg', 'http://example.com/preview.jpg'); @@ -178,53 +176,64 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/image.jpg'); + $this->assertEquals($data['content']['previewImageUrl'], 'http://example.com/preview.jpg'); + $this->assertEquals($data['content']['contentType'], ContentType::IMAGE); + $this->assertEquals($data['content']['toType'], RecipientType::USER); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } public function testSendVideo() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/video.mp4'); - $this->assertEquals($data['content']['previewImageUrl'], 'http://example.com/preview.jpg'); - $this->assertEquals($data['content']['contentType'], ContentType::VIDEO); - $this->assertEquals($data['content']['toType'], RecipientType::USER); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendVideo(['DUMMY_MID'], 'http://example.com/video.mp4', 'http://example.com/preview.jpg'); @@ -252,53 +261,64 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/video.mp4'); + $this->assertEquals($data['content']['previewImageUrl'], 'http://example.com/preview.jpg'); + $this->assertEquals($data['content']['contentType'], ContentType::VIDEO); + $this->assertEquals($data['content']['toType'], RecipientType::USER); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } public function testSendAudio() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/sound.m4a'); - $this->assertEquals($data['content']['contentMetadata']['AUDLEN'], '5000'); - $this->assertEquals($data['content']['contentType'], ContentType::AUDIO); - $this->assertEquals($data['content']['toType'], RecipientType::USER); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendAudio(['DUMMY_MID'], 'http://example.com/sound.m4a', 5000); @@ -326,58 +346,61 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + $this->assertEquals($data['content']['originalContentUrl'], 'http://example.com/sound.m4a'); + $this->assertEquals($data['content']['contentMetadata']['AUDLEN'], '5000'); + $this->assertEquals($data['content']['contentType'], ContentType::AUDIO); + $this->assertEquals($data['content']['toType'], RecipientType::USER); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } public function testSendLocation() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - - $content = $data['content']; - $location = $content['location']; - - $this->assertEquals($content['contentType'], ContentType::LOCATION); - $this->assertEquals($content['text'], '2 Chome-21-1 Shibuya Tokyo 150-0002, Japan'); - $this->assertEquals($location['title'], $content['text']); - $this->assertEquals($location['latitude'], 35.658240); - $this->assertEquals($location['longitude'], 139.703478); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}')), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory('{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}') ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendLocation(['DUMMY_MID'], '2 Chome-21-1 Shibuya Tokyo 150-0002, Japan', 35.658240, 139.703478); @@ -405,54 +428,69 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + + $content = $data['content']; + $location = $content['location']; + + $this->assertEquals($content['contentType'], ContentType::LOCATION); + $this->assertEquals($content['text'], '2 Chome-21-1 Shibuya Tokyo 150-0002, Japan'); + $this->assertEquals($location['title'], $content['text']); + $this->assertEquals($location['latitude'], 35.658240); + $this->assertEquals($location['longitude'], 139.703478); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } public function testSendSticker() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - - $this->assertEquals($data['content']['contentType'], ContentType::STICKER); - $this->assertEquals($data['content']['contentMetadata']['STKID'], '1'); - $this->assertEquals($data['content']['contentMetadata']['STKPKGID'], '2'); - $this->assertEquals($data['content']['contentMetadata']['STKVER'], '100'); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $res = $sdk->sendSticker(['DUMMY_MID'], 1, 2, 100); @@ -480,5 +518,32 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + + $this->assertEquals($data['content']['contentType'], ContentType::STICKER); + $this->assertEquals($data['content']['contentMetadata']['STKID'], '1'); + $this->assertEquals($data['content']['contentMetadata']['STKPKGID'], '2'); + $this->assertEquals($data['content']['contentMetadata']['STKVER'], '100'); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } } \ No newline at end of file diff --git a/tests/LINEBot/MultipleMessagesSendingTest.php b/tests/LINEBot/MultipleMessagesSendingTest.php index 266e43cc..12387244 100644 --- a/tests/LINEBot/MultipleMessagesSendingTest.php +++ b/tests/LINEBot/MultipleMessagesSendingTest.php @@ -16,10 +16,12 @@ */ namespace LINE\Tests\LINEBot; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\Message\Request; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; +use GuzzleHttp\Subscriber\History; +use GuzzleHttp\Subscriber\Mock; use LINE\LINEBot; use LINE\LINEBot\Constant\ContentType; use LINE\LINEBot\HTTPClient\GuzzleHTTPClient; @@ -29,90 +31,38 @@ class MultipleMessagesSendingTest extends \PHPUnit_Framework_TestCase { public function testMultipleMessages() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 140177271400161403); - $this->assertEquals($data['to'], ['DUMMY_MID']); - - $this->assertEquals(sizeof($data['content']['messages']), 6); - - { - $content = $data['content']['messages'][0]; - $this->assertEquals($content['text'], 'hello!'); - $this->assertEquals($content['contentType'], ContentType::TEXT); - } - { - $content = $data['content']['messages'][1]; - $this->assertEquals($content['originalContentUrl'], 'http://example.com/image.jpg'); - $this->assertEquals($content['previewImageUrl'], 'http://example.com/preview.jpg'); - $this->assertEquals($content['contentType'], ContentType::IMAGE); - } - { - $content = $data['content']['messages'][2]; - $this->assertEquals($content['originalContentUrl'], 'http://example.com/audio.m4a'); - $this->assertEquals($content['contentMetadata']['AUDLEN'], '6000'); - $this->assertEquals($content['contentType'], ContentType::AUDIO); - } - { - $content = $data['content']['messages'][3]; - $this->assertEquals($content['originalContentUrl'], 'http://example.com/video.mp4'); - $this->assertEquals($content['previewImageUrl'], 'http://example.com/video_preview.jpg'); - $this->assertEquals($content['contentType'], ContentType::VIDEO); - } - { - $content = $data['content']['messages'][4]; - $location = $content['location']; - $this->assertEquals($content['text'], '2 Chome-21-1 Shibuya Tokyo 150-0002, Japan'); - $this->assertEquals($location['title'], $content['text']); - $this->assertEquals($location['latitude'], 35.658240); - $this->assertEquals($location['longitude'], 139.703478); - $this->assertEquals($content['contentType'], ContentType::LOCATION); - } - { - $content = $data['content']['messages'][5]; - $this->assertEquals($content['contentType'], ContentType::STICKER); - $this->assertEquals($content['contentMetadata']['STKID'], '1'); - $this->assertEquals($content['contentMetadata']['STKPKGID'], '2'); - $this->assertEquals($content['contentMetadata']['STKVER'], '100'); - } - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); $config = [ 'channelId' => '1000000000', 'channelSecret' => 'testsecret', 'channelMid' => 'TEST_MID', ]; - $sdk = new LINEBot($config, new GuzzleHTTPClient(array_merge($config, ['handler' => $mockHandler]))); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); + + $sdk = new LINEBot($config, new GuzzleHTTPClient(array_merge($config, ['emitter' => $emitter]))); $multipleMessages = (new MultipleMessages()) ->addText('hello!') @@ -147,6 +97,69 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); - } + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 140177271400161403); + $this->assertEquals($data['to'], ['DUMMY_MID']); + + $this->assertEquals(sizeof($data['content']['messages']), 6); + + { + $content = $data['content']['messages'][0]; + $this->assertEquals($content['text'], 'hello!'); + $this->assertEquals($content['contentType'], ContentType::TEXT); + } + { + $content = $data['content']['messages'][1]; + $this->assertEquals($content['originalContentUrl'], 'http://example.com/image.jpg'); + $this->assertEquals($content['previewImageUrl'], 'http://example.com/preview.jpg'); + $this->assertEquals($content['contentType'], ContentType::IMAGE); + } + { + $content = $data['content']['messages'][2]; + $this->assertEquals($content['originalContentUrl'], 'http://example.com/audio.m4a'); + $this->assertEquals($content['contentMetadata']['AUDLEN'], '6000'); + $this->assertEquals($content['contentType'], ContentType::AUDIO); + } + { + $content = $data['content']['messages'][3]; + $this->assertEquals($content['originalContentUrl'], 'http://example.com/video.mp4'); + $this->assertEquals($content['previewImageUrl'], 'http://example.com/video_preview.jpg'); + $this->assertEquals($content['contentType'], ContentType::VIDEO); + } + { + $content = $data['content']['messages'][4]; + $location = $content['location']; + $this->assertEquals($content['text'], '2 Chome-21-1 Shibuya Tokyo 150-0002, Japan'); + $this->assertEquals($location['title'], $content['text']); + $this->assertEquals($location['latitude'], 35.658240); + $this->assertEquals($location['longitude'], 139.703478); + $this->assertEquals($content['contentType'], ContentType::LOCATION); + } + { + $content = $data['content']['messages'][5]; + $this->assertEquals($content['contentType'], ContentType::STICKER); + $this->assertEquals($content['contentMetadata']['STKID'], '1'); + $this->assertEquals($content['contentMetadata']['STKPKGID'], '2'); + $this->assertEquals($content['contentMetadata']['STKVER'], '100'); + } + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); + } } \ No newline at end of file diff --git a/tests/LINEBot/RichMessageSendingTest.php b/tests/LINEBot/RichMessageSendingTest.php index 65191c7c..90cb62fb 100644 --- a/tests/LINEBot/RichMessageSendingTest.php +++ b/tests/LINEBot/RichMessageSendingTest.php @@ -16,10 +16,13 @@ */ namespace LINE\Tests\LINEBot; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Client; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\Message\Request; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; +use GuzzleHttp\Subscriber\History; +use GuzzleHttp\Subscriber\Mock; use LINE\LINEBot; use LINE\LINEBot\HTTPClient\GuzzleHTTPClient; use LINE\LINEBot\Message\RichMessage\Markup; @@ -28,107 +31,38 @@ class RichMessageSendingTest extends \PHPUnit_Framework_TestCase { public function testRichMessage() { - $mock = new MockHandler([ - function (Request $req) { - $this->assertEquals($req->getMethod(), 'POST'); - $this->assertEquals($req->getUri(), 'https://trialbot-api.line.me/v1/events'); - - $data = json_decode($req->getBody(), true); - $this->assertEquals($data['eventType'], 138311608800106203); - $this->assertEquals($data['to'], ['DUMMY_MID']); - $this->assertEquals($data['content']['contentMetadata']['ALT_TEXT'], 'Alt text'); - $this->assertEquals( - $data['content']['contentMetadata']['DOWNLOAD_URL'], - 'http://example.com/image.jpg' - ); - - $json = $data['content']['contentMetadata']['MARKUP_JSON']; - $this->assertEquals( - json_decode($json, true), - [ - 'scenes' => [ - 'scene1' => [ - 'listeners' => [ - [ - 'params' => [ - 0, - 0, - 520, - 520, - ], - 'type' => 'touch', - 'action' => 'SOMETHING', - ], - ], - 'draws' => [ - [ - 'image' => 'image1', - 'x' => 0, - 'y' => 0, - 'w' => 1040, - 'h' => 1040, - ], - ], - ], - ], - 'images' => [ - 'image1' => [ - 'x' => 0, - 'y' => 0, - 'w' => 1040, - 'h' => 1040, - ], - ], - 'actions' => [ - 'SOMETHING' => [ - 'text' => 'something', - 'params' => [ - 'linkUri' => 'https://line.me', - ], - 'type' => 'web', - ], - ], - 'canvas' => [ - 'initialScene' => 'scene1', - 'width' => 1040, - 'height' => 1040, - ], - ] - ); - - $channelIdHeader = $req->getHeader('X-Line-ChannelID'); - $this->assertEquals(sizeof($channelIdHeader), 1); - $this->assertEquals($channelIdHeader[0], '1000000000'); - - $channelSecretHeader = $req->getHeader('X-Line-ChannelSecret'); - $this->assertEquals(sizeof($channelSecretHeader), 1); - $this->assertEquals($channelSecretHeader[0], 'testsecret'); - - $channelMidHeader = $req->getHeader('X-Line-Trusted-User-With-ACL'); - $this->assertEquals(sizeof($channelMidHeader), 1); - $this->assertEquals($channelMidHeader[0], 'TEST_MID'); - - return new Response( - 200, - [], - '{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}' - ); - }, - new Response(400, [], '{"statusCode":"422","statusMessage":"invalid users"}'), + $mock = new Mock([ + new Response( + 200, + [], + Stream::factory('{"failed":[],"messageId":"1460867315795","timestamp":1460867315795,"version":1}') + ), + new Response( + 400, + [], + Stream::factory('{"statusCode":"422","statusMessage":"invalid users"}') + ), new Response( 500, [], - '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + Stream::factory( + '{"statusCode":"500","statusMessage":"unexpected error found at call bot api sendMessage"}' + ) ), ]); - $mockHandler = HandlerStack::create($mock); $config = [ 'channelId' => '1000000000', 'channelSecret' => 'testsecret', 'channelMid' => 'TEST_MID', ]; - $sdk = new LINEBot($config, new GuzzleHTTPClient(array_merge($config, ['handler' => $mockHandler]))); + + $histories = new History(); + $emitter = new Emitter(); + $emitter->attach($mock); + $emitter->attach($histories); + + $sdk = new LINEBot($config, new GuzzleHTTPClient(array_merge($config, ['emitter' => $emitter]))); $markup = (new Markup(1040)) ->setAction('SOMETHING', 'something', 'https://line.me') @@ -159,5 +93,87 @@ function (Request $req) { $this->assertEquals(500, $res->getHTTPStatus()); $this->assertEquals('500', $res->getStatusCode()); $this->assertEquals('unexpected error found at call bot api sendMessage', $res->getStatusMessage()); + + $history = $histories->getIterator()[0]; + /** @var Request $req */ + $req = $history['request']; + + $this->assertEquals($req->getMethod(), 'POST'); + $this->assertEquals($req->getUrl(), 'https://trialbot-api.line.me/v1/events'); + + $data = json_decode($req->getBody(), true); + $this->assertEquals($data['eventType'], 138311608800106203); + $this->assertEquals($data['to'], ['DUMMY_MID']); + $this->assertEquals($data['content']['contentMetadata']['ALT_TEXT'], 'Alt text'); + $this->assertEquals( + $data['content']['contentMetadata']['DOWNLOAD_URL'], + 'http://example.com/image.jpg' + ); + + $json = $data['content']['contentMetadata']['MARKUP_JSON']; + $this->assertEquals( + json_decode($json, true), + [ + 'scenes' => [ + 'scene1' => [ + 'listeners' => [ + [ + 'params' => [ + 0, + 0, + 520, + 520, + ], + 'type' => 'touch', + 'action' => 'SOMETHING', + ], + ], + 'draws' => [ + [ + 'image' => 'image1', + 'x' => 0, + 'y' => 0, + 'w' => 1040, + 'h' => 1040, + ], + ], + ], + ], + 'images' => [ + 'image1' => [ + 'x' => 0, + 'y' => 0, + 'w' => 1040, + 'h' => 1040, + ], + ], + 'actions' => [ + 'SOMETHING' => [ + 'text' => 'something', + 'params' => [ + 'linkUri' => 'https://line.me', + ], + 'type' => 'web', + ], + ], + 'canvas' => [ + 'initialScene' => 'scene1', + 'width' => 1040, + 'height' => 1040, + ], + ] + ); + + $channelIdHeader = $req->getHeaderAsArray('X-Line-ChannelID'); + $this->assertEquals(sizeof($channelIdHeader), 1); + $this->assertEquals($channelIdHeader[0], '1000000000'); + + $channelSecretHeader = $req->getHeaderAsArray('X-Line-ChannelSecret'); + $this->assertEquals(sizeof($channelSecretHeader), 1); + $this->assertEquals($channelSecretHeader[0], 'testsecret'); + + $channelMidHeader = $req->getHeaderAsArray('X-Line-Trusted-User-With-ACL'); + $this->assertEquals(sizeof($channelMidHeader), 1); + $this->assertEquals($channelMidHeader[0], 'TEST_MID'); } } \ No newline at end of file diff --git a/tests/LINEBotTest.php b/tests/LINEBotTest.php index e474c8f4..73ecca47 100644 --- a/tests/LINEBotTest.php +++ b/tests/LINEBotTest.php @@ -16,9 +16,10 @@ */ namespace LINE\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Event\Emitter; +use GuzzleHttp\Message\Response; +use GuzzleHttp\Stream\Stream; +use GuzzleHttp\Subscriber\Mock; use LINE\LINEBot; use LINE\LINEBot\HTTPClient\GuzzleHTTPClient; @@ -35,14 +36,16 @@ class LINEBotTest extends \PHPUnit_Framework_TestCase */ public function testLINEBotAPIExceptionCausedByEmptyResponseBody() { - $mock = new MockHandler([ + $mock = new Mock([ new Response(200, []), // missing body ]); - $mockHandler = HandlerStack::create($mock); + + $emitter = new Emitter(); + $emitter->attach($mock); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $sdk->sendText(['DUMMY_MID'], 'hello!'); @@ -53,14 +56,16 @@ public function testLINEBotAPIExceptionCausedByEmptyResponseBody() */ public function textLINEBotAPIExceptionCausedByInvalidResponseBody() { - $mock = new MockHandler([ - new Response(200, [], 'I AM NOT A JSON'), // not JSON + $mock = new Mock([ + new Response(200, [], Stream::factory('I AM NOT A JSON')), // not JSON ]); - $mockHandler = HandlerStack::create($mock); + + $emitter = new Emitter(); + $emitter->attach($mock); $sdk = new LINEBot( $this::$config, - new GuzzleHTTPClient(array_merge($this::$config, ['handler' => $mockHandler])) + new GuzzleHTTPClient(array_merge($this::$config, ['emitter' => $emitter])) ); $sdk->sendText(['DUMMY_MID'], 'hello!');