Skip to content
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

Add set_http_client method #68

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
21 changes: 18 additions & 3 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;

/**
Expand Down Expand Up @@ -67,9 +68,9 @@ class ConvertKit_API
protected $debug_logger;

/**
* Guzzle Http Client
* Guzzle Http ClientInterface
*
* @var \GuzzleHttp\Client
* @var \GuzzleHttp\ClientInterface
*/
protected $client;

Expand All @@ -87,7 +88,7 @@ public function __construct(string $api_key, string $api_secret, bool $debug = f
$this->api_secret = $api_secret;
$this->debug = $debug;

// Specify a User-Agent for API requests.
// Set the Guzzle client.
$this->client = new Client(
[
'headers' => [
Expand All @@ -105,6 +106,20 @@ public function __construct(string $api_key, string $api_secret, bool $debug = f
}
}

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

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

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

/**
* ConvertKit API class tests.
Expand Down Expand Up @@ -47,6 +53,42 @@ 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.
*
* @since 1.3.0
*
* @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' => 'mock@guzzle.mock',
]
)),
]);

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

// Assign the client to the API class.
$this->api->set_http_client($client);

// Perform an API request.
$result = $this->api->get_account();

// Confirm mocked data was returned.
$this->assertSame('Test Account for Guzzle Mock', $result->name);
$this->assertSame('free', $result->plan_type);
$this->assertSame('mock@guzzle.mock', $result->primary_email_address);
}

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