Skip to content

Commit

Permalink
guzzle 5x working
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Mar 21, 2016
1 parent ca22ba4 commit 13472c7
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 87 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~5.0|~6.0"
"guzzlehttp/guzzle": "~5.3|~6.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.*"
Expand Down
19 changes: 8 additions & 11 deletions src/Connection.php
Expand Up @@ -36,24 +36,21 @@ public function client()

protected function getClientParams($access_token)
{
if (substr(Client::VERSION, 0, 1) == 6) {
$headers = [
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
];

if (version_compare(Client::VERSION, 6, '>=')) {
return [
'base_uri' => $this->base_url,
'headers' => [
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
],
'headers' => $headers,
];
}

return [
'base_url' => [$this->base_url, []],
'defaults' => [
'headers' => [
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
],
],
'defaults' => ['headers' => $headers],
];
}
}
11 changes: 9 additions & 2 deletions src/PHPushbullet.php
Expand Up @@ -48,15 +48,17 @@ class PHPushbullet
*/
protected $all_devices = [];

public function __construct($access_token = null)
public function __construct($access_token = null, Connection $connection = null)
{
$access_token = $access_token ?: getenv('pushbullet.access_token');

if (!$access_token) {
throw new \Exception('Your Pushbullet access token is not set.');
}

$this->api = (new Connection($access_token))->client();
$connection = $connection ?: new Connection($access_token);

$this->api = $connection->client();
}

/**
Expand Down Expand Up @@ -168,6 +170,11 @@ public function push($request)
return $responses;
}

public function getClient()
{
return $this->api;
}

/**
* Create push request and... push it
*
Expand Down
11 changes: 8 additions & 3 deletions src/Request/PushFile.php
Expand Up @@ -26,14 +26,19 @@ public function __construct($file_name, $file_url, $body = null)
* Get the file type based on the file url
*
* @param string $file_url
*
* @return string
*/

protected function getFileType($file_url)
{
$client = new Client();
$file_info = $client->head($file_url);
$file_info = (new Client())->head($file_url);
$file_type = $file_info->getHeader('content-type');

if (is_array($file_type)) {
return reset($file_type);
}

return $file_info->getHeader('content-type');
return $file_type;
}
}
10 changes: 2 additions & 8 deletions tests/AddressTest.php
Expand Up @@ -37,10 +37,7 @@ public function it_can_push_an_address_as_a_string()
$this->assertSame('Home', $first['name']);
$this->assertSame('123 Sesame Street', $first['address']);

$expected_flow = [ 'devices', 'pushes' ];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}

/** @test */
Expand Down Expand Up @@ -83,9 +80,6 @@ public function it_can_push_an_address_as_an_array()
$this->assertSame('Home', $first['name']);
$this->assertSame('123 Sesame Street New York NY 10001', $first['address']);

$expected_flow = [ 'devices', 'pushes' ];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}
}
10 changes: 2 additions & 8 deletions tests/ChannelTest.php
Expand Up @@ -24,10 +24,7 @@ public function it_can_chain_channels()
$this->assertInternalType('array', $response);
$this->assertCount(1, $response);

$expected_flow = [ 'pushes' ];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['pushes']);
}

/** @test */
Expand All @@ -50,9 +47,6 @@ public function it_can_pass_multiple_channels()
$this->assertInternalType('array', $response);
$this->assertCount(2, $response);

$expected_flow = [ 'pushes', 'pushes' ];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['pushes', 'pushes']);
}
}
15 changes: 3 additions & 12 deletions tests/DeviceTest.php
Expand Up @@ -25,10 +25,7 @@ public function it_can_list_available_devices()
$this->assertInternalType('object', reset($response));
$this->assertInstanceOf('PHPushbullet\Device', reset($response));

$expected_flow = ['devices'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices']);
}

/** @test */
Expand Down Expand Up @@ -61,10 +58,7 @@ public function it_can_chain_devices()
$this->assertInternalType('array', $response);
$this->assertCount(2, $response);

$expected_flow = ['devices', 'pushes', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes', 'pushes']);
}

/** @test */
Expand Down Expand Up @@ -97,10 +91,7 @@ public function it_can_pass_multiple_devices()
$this->assertInternalType('array', $response);
$this->assertCount(2, $response);

$expected_flow = ['devices', 'pushes', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes', 'pushes']);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions tests/FileTest.php
Expand Up @@ -41,10 +41,7 @@ public function it_can_push_a_file()
$this->assertSame('image/jpeg', $first['file_type']);
$this->assertSame('Just a baby cat.', $first['body']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}

/** @test */
Expand Down Expand Up @@ -82,9 +79,6 @@ public function it_can_push_a_file_without_a_body()
$this->assertSame('http://placehold.it/350x150', $first['file_url']);
$this->assertSame('image/jpeg', $first['file_type']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}
}
10 changes: 2 additions & 8 deletions tests/LinkTest.php
Expand Up @@ -39,10 +39,7 @@ public function it_can_push_a_link()
$this->assertSame('http://www.google.com', $first['url']);
$this->assertSame('Search it', $first['body']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}

/** @test */
Expand Down Expand Up @@ -78,9 +75,6 @@ public function it_can_push_a_link_without_a_body()
$this->assertSame('Google', $first['title']);
$this->assertSame('http://www.google.com', $first['url']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}
}
5 changes: 1 addition & 4 deletions tests/ListTest.php
Expand Up @@ -49,9 +49,6 @@ public function it_can_push_a_list()
'Other thing',
], $first['items']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}
}
5 changes: 1 addition & 4 deletions tests/NoteTest.php
Expand Up @@ -37,9 +37,6 @@ public function it_can_push_a_note()
$this->assertSame('Reminder', $first['title']);
$this->assertSame('Do this thing', $first['body']);

$expected_flow = ['devices', 'pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['devices', 'pushes']);
}
}
5 changes: 1 addition & 4 deletions tests/UserTest.php
Expand Up @@ -23,9 +23,6 @@ public function it_can_send_to_a_user()
$this->assertInternalType('array', $response);
$this->assertCount(1, $response);

$expected_flow = ['pushes'];
$actual_flow = $this->getFlow();

$this->assertSame($expected_flow, $actual_flow);
$this->assertRequestHistory(['pushes']);
}
}
34 changes: 20 additions & 14 deletions tests/base/PHPushbulletTestBase.php
@@ -1,9 +1,6 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Subscriber\History;
use PHPushbullet\PHPushbullet;

class PHPushbulletTestBase extends PHPUnit_Framework_TestCase
Expand All @@ -14,17 +11,17 @@ class PHPushbulletTestBase extends PHPUnit_Framework_TestCase

protected $history;

protected $guzzle_6;

public function setUp()
{
$reflected = new ReflectionClass('PHPushbullet\PHPushbullet');

$this->history = new History();

$this->client = $reflected->getProperty('api');
$this->client->setAccessible(true);
$this->pushbullet = new PHPushbullet('random');
$this->client->setValue($this->pushbullet, new Client());
$this->client->getValue($this->pushbullet)->getEmitter()->attach($this->history);
$this->guzzle_6 = version_compare(Client::VERSION, 6, '>=');

if (!$this->guzzle_6) {
$this->history = new \GuzzleHttp\Subscriber\History();
$this->pushbullet->getClient()->getEmitter()->attach($this->history);
}
}

protected function okResponse($body)
Expand All @@ -43,9 +40,9 @@ protected function okResponse($body)

protected function mock($mock)
{
$mock = new Mock($mock);
$mock = new \GuzzleHttp\Subscriber\Mock($mock);

$this->client->getValue($this->pushbullet)->getEmitter()->attach($mock);
$this->pushbullet->getClient()->getEmitter()->attach($mock);
}

protected function pushResponse($vars)
Expand Down Expand Up @@ -102,7 +99,7 @@ protected function getDevice($type)
]
];

return $devices[ $type ];
return $devices[$type];
}

protected function getFlow()
Expand All @@ -112,6 +109,15 @@ protected function getFlow()
}, $this->history->getRequests());
}

protected function assertRequestHistory(array $expected)
{
$expected = array_map(function ($endpoint) {
return 'https://api.pushbullet.com/v2/' . $endpoint;
}, $expected);

$this->assertSame($expected, $this->getFlow());
}

/** @test **/

public function nothing()
Expand Down

0 comments on commit 13472c7

Please sign in to comment.