Skip to content

Commit 415075b

Browse files
authored
Merge pull request #68 from ConvertKit/use-guzzle-clientinterface
Add `set_http_client` method
2 parents 562a14f + 1f68f32 commit 415075b

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/ConvertKit_API.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Monolog\Logger;
1111
use Monolog\Handler\StreamHandler;
1212
use GuzzleHttp\Client;
13+
use GuzzleHttp\ClientInterface;
1314
use GuzzleHttp\Psr7\Request;
1415

1516
/**
@@ -67,9 +68,9 @@ class ConvertKit_API
6768
protected $debug_logger;
6869

6970
/**
70-
* Guzzle Http Client
71+
* Guzzle Http ClientInterface
7172
*
72-
* @var \GuzzleHttp\Client
73+
* @var \GuzzleHttp\ClientInterface
7374
*/
7475
protected $client;
7576

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

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

109+
/**
110+
* Set the Guzzle client implementation to use for API requests.
111+
*
112+
* @param ClientInterface $client Guzzle client implementation.
113+
*
114+
* @since 1.3.0
115+
*
116+
* @return void
117+
*/
118+
public function set_http_client(ClientInterface $client)
119+
{
120+
$this->client = $client;
121+
}
122+
108123
/**
109124
* Add an entry to monologger.
110125
*

tests/ConvertKitAPITest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
use PHPUnit\Framework\TestCase;
4+
use GuzzleHttp\Client;
5+
use GuzzleHttp\HandlerStack;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use GuzzleHttp\Handler\MockHandler;
9+
use GuzzleHttp\Exception\RequestException;
410

511
/**
612
* ConvertKit API class tests.
@@ -47,6 +53,42 @@ protected function setUp(): void
4753
$this->api = new \ConvertKit_API\ConvertKit_API($_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET']);
4854
}
4955

56+
/**
57+
* Test that a ClientInterface can be injected.
58+
*
59+
* @since 1.3.0
60+
*
61+
* @return void
62+
*/
63+
public function testClientInterfaceInjection()
64+
{
65+
// Setup API with a mock Guzzle client.
66+
$mock = new MockHandler([
67+
new Response(200, [], json_encode(
68+
[
69+
'name' => 'Test Account for Guzzle Mock',
70+
'plan_type' => 'free',
71+
'primary_email_address' => '[email protected]',
72+
]
73+
)),
74+
]);
75+
76+
// Define client with mock handler.
77+
$handlerStack = HandlerStack::create($mock);
78+
$client = new Client(['handler' => $handlerStack]);
79+
80+
// Assign the client to the API class.
81+
$this->api->set_http_client($client);
82+
83+
// Perform an API request.
84+
$result = $this->api->get_account();
85+
86+
// Confirm mocked data was returned.
87+
$this->assertSame('Test Account for Guzzle Mock', $result->name);
88+
$this->assertSame('free', $result->plan_type);
89+
$this->assertSame('[email protected]', $result->primary_email_address);
90+
}
91+
5092
/**
5193
* Test that debug logging works when enabled and an API call is made.
5294
*

0 commit comments

Comments
 (0)