Skip to content

Commit

Permalink
mocking all sorts of guzzle calls, whether they are 5 or 6
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Mar 22, 2016
1 parent 13472c7 commit b9bf6f8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Connection.php
Expand Up @@ -21,9 +21,9 @@ class Connection
*/
protected $client;

public function __construct($access_token)
public function __construct($access_token, Client $client = null)
{
$this->client = new Client($this->getClientParams($access_token));
$this->client = $client ?: new Client($this->getClientParams($access_token));
}

/**
Expand Down
58 changes: 43 additions & 15 deletions tests/base/PHPushbulletTestBase.php
@@ -1,6 +1,7 @@
<?php

use GuzzleHttp\Client;
use PHPushbullet\Connection;
use PHPushbullet\PHPushbullet;

class PHPushbulletTestBase extends PHPUnit_Framework_TestCase
Expand All @@ -13,21 +14,42 @@ class PHPushbulletTestBase extends PHPUnit_Framework_TestCase

protected $guzzle_6;

protected $mock_handler;

public function setUp()
{
$this->pushbullet = new PHPushbullet('random');
$this->guzzle_6 = version_compare(Client::VERSION, 6, '>=');

if (!$this->guzzle_6) {
$this->guzzle_6 = version_compare(Client::VERSION, 6, '>=');

if ($this->guzzle_6) {
$this->mock_handler = new \GuzzleHttp\Handler\MockHandler();
$this->history = [];

$history = \GuzzleHttp\Middleware::history($this->history);
$stack = \GuzzleHttp\HandlerStack::create($this->mock_handler);
$stack->push($history);
$client = new Client(['handler' => $stack]);
} else {
$client = new Client();
$this->history = new \GuzzleHttp\Subscriber\History();
$this->pushbullet->getClient()->getEmitter()->attach($this->history);
$this->mock_handler = new \GuzzleHttp\Subscriber\Mock();

$client->getEmitter()->attach($this->history);
$client->getEmitter()->attach($this->mock_handler);
}

$connection = new Connection('random', $client);

$this->pushbullet = new PHPushbullet('random', $connection);
}

protected function okResponse($body)
{
$body = json_encode($body);

if ($this->guzzle_6) {
return new \GuzzleHttp\Psr7\Response(200, ['Content-Length' => strlen($body)], $body);
}

$response = [
'HTTP/1.1 200 OK',
'Content-Length: ' . strlen($body),
Expand All @@ -38,11 +60,15 @@ protected function okResponse($body)
return implode("\r\n", $response);
}

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

$this->pushbullet->getClient()->getEmitter()->attach($mock);
foreach ($mocks as $mock) {
if ($this->guzzle_6) {
$this->mock_handler->append($mock);
} else {
$this->mock_handler->addResponse($mock);
}
}
}

protected function pushResponse($vars)
Expand Down Expand Up @@ -102,20 +128,22 @@ protected function getDevice($type)
return $devices[$type];
}

protected function getFlow()
protected function getRuquestUrls()
{
if ($this->guzzle_6) {
return array_map(function ($request) {
return (string) $request['request']->getUri();
}, $this->history);
}

return array_map(function ($request) {
return $request->getUrl();
}, $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());
$this->assertSame($expected, $this->getRuquestUrls());
}

/** @test **/
Expand Down

0 comments on commit b9bf6f8

Please sign in to comment.