Skip to content

Allow swapping Guzzle/ClientInterface #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace ConvertKit_API;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

use GuzzleHttp\ClientInterface;
use Monolog\Handler\StreamHandler;
/**
* ConvertKit API Class
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ class ConvertKit_API
/**
* Guzzle Http Client
*
* @var \GuzzleHttp\Client
* @var \GuzzleHttp\ClientInterface
*/
protected $client;

Expand Down Expand Up @@ -105,6 +105,18 @@ public function __construct(string $api_key, string $api_secret, bool $debug = f
}
}

/**
* Set the Guzzle client implementation to use.
*
* @param ClientInterface $client Guzzle client implementation.
*
* @return void
*/
public function set_http_client(ClientInterface $client)
{
$this->client = $client;
}

/**
* Add an entry to monologger.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Exception\RequestException;

/**
* ConvertKit API class tests.
Expand Down Expand Up @@ -47,6 +53,37 @@ protected function setUp(): void
$this->api = new \ConvertKit_API\ConvertKit_API($_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET']);
}

/**
* Test that a ClientInterface can be injected.
*
*
* @return void
*/
public function testClientInterfaceInjection()
{
// Setup API with a mock Guzzle client.
$mock = new MockHandler([
new Response(200, [], json_encode(
[
'name' => 'Test Account for Guzzle Mock',
'plan_type' => 'free',
'primary_email_address' => '[email protected]',
]
)),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

$this->api->set_http_client($client);

$result = $this->api->get_account();

$this->assertSame('Test Account for Guzzle Mock', $result->name);
$this->assertSame('free', $result->plan_type);
$this->assertSame('[email protected]', $result->primary_email_address);
}

/**
* Test that debug logging works when enabled and an API call is made.
*
Expand Down