|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | 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; |
4 | 10 |
|
5 | 11 | /**
|
6 | 12 | * ConvertKit API class tests.
|
@@ -47,6 +53,42 @@ protected function setUp(): void
|
47 | 53 | $this->api = new \ConvertKit_API\ConvertKit_API($_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET']);
|
48 | 54 | }
|
49 | 55 |
|
| 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 | + |
50 | 92 | /**
|
51 | 93 | * Test that debug logging works when enabled and an API call is made.
|
52 | 94 | *
|
|
0 commit comments