Skip to content

Commit

Permalink
Fix dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
moznion committed Apr 27, 2016
1 parent 3b5539d commit 1e4ca1c
Show file tree
Hide file tree
Showing 7 changed files with 550 additions and 441 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions src/LINEBot/HTTPClient/GuzzleHTTPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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) {
Expand Down
74 changes: 41 additions & 33 deletions tests/LINEBot/GettingContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Loading

0 comments on commit 1e4ca1c

Please sign in to comment.