|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Network; |
| 4 | + |
| 5 | +use Chargebee\ChargebeeClient; |
| 6 | +use Chargebee\Environment; |
| 7 | +use Chargebee\HttpClient\PsrClientAdapter; |
| 8 | +use Chargebee\ValueObjects\Transporters\ChargebeePayload; |
| 9 | +use GuzzleHttp\Psr7\Response; |
| 10 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 11 | +use PHPUnit\Framework\Attributes\TestDox; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Psr\Http\Client\ClientInterface; |
| 14 | +use Psr\Http\Message\RequestFactoryInterface; |
| 15 | +use Psr\Http\Message\RequestInterface; |
| 16 | +use Psr\Http\Message\ResponseInterface; |
| 17 | +use Psr\Http\Message\StreamFactoryInterface; |
| 18 | +use Psr\Http\Message\StreamInterface; |
| 19 | +use Psr\Http\Message\UriInterface; |
| 20 | + |
| 21 | +#[CoversClass(PsrClientAdapter::class)] |
| 22 | +final class PsrClientAdapterTest extends TestCase |
| 23 | +{ |
| 24 | + private function makePayload( |
| 25 | + string $method, |
| 26 | + string $url, |
| 27 | + ?string $params = null, |
| 28 | + array $headers = [] |
| 29 | + ): ChargebeePayload { |
| 30 | + $env = new Environment('test-site', 'test-api-key'); |
| 31 | + return new ChargebeePayload($url, $method, $params, $headers, $env); |
| 32 | + } |
| 33 | + |
| 34 | + private function makeStubClient(?ResponseInterface $response = null): ClientInterface |
| 35 | + { |
| 36 | + $response ??= new Response(200, [], '{}'); |
| 37 | + |
| 38 | + return new class($response) implements ClientInterface { |
| 39 | + private ResponseInterface $response; |
| 40 | + public array $sentRequests = []; |
| 41 | + |
| 42 | + public function __construct(ResponseInterface $response) |
| 43 | + { |
| 44 | + $this->response = $response; |
| 45 | + } |
| 46 | + |
| 47 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 48 | + { |
| 49 | + $this->sentRequests[] = $request; |
| 50 | + return $this->response; |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + #[TestDox('PsrClientAdapter::create() returns the injected PSR-18 client')] |
| 56 | + public function testCreateReturnsInjectedClient(): void |
| 57 | + { |
| 58 | + $client = $this->makeStubClient(); |
| 59 | + $adapter = new PsrClientAdapter($client); |
| 60 | + |
| 61 | + $this->assertSame($client, $adapter->create()); |
| 62 | + } |
| 63 | + |
| 64 | + #[TestDox('GET request with no PSR-17 factories discovers them automatically')] |
| 65 | + public function testGetRequestWithDiscoveredFactories(): void |
| 66 | + { |
| 67 | + $client = $this->makeStubClient(); |
| 68 | + $adapter = new PsrClientAdapter($client); |
| 69 | + |
| 70 | + $payload = $this->makePayload('get', 'https://example.com/api/v2/customers', 'limit=10', [ |
| 71 | + 'Authorization' => 'Basic dGVzdA==', |
| 72 | + ]); |
| 73 | + |
| 74 | + $request = $adapter->createRequest($payload); |
| 75 | + |
| 76 | + $this->assertSame('GET', strtoupper($request->getMethod())); |
| 77 | + $this->assertStringContainsString('limit=10', (string) $request->getUri()); |
| 78 | + $this->assertSame('Basic dGVzdA==', $request->getHeaderLine('Authorization')); |
| 79 | + } |
| 80 | + |
| 81 | + #[TestDox('POST request with no PSR-17 factories discovers them automatically')] |
| 82 | + public function testPostRequestWithDiscoveredFactories(): void |
| 83 | + { |
| 84 | + $client = $this->makeStubClient(); |
| 85 | + $adapter = new PsrClientAdapter($client); |
| 86 | + |
| 87 | + $payload = $this->makePayload('post', 'https://example.com/api/v2/customers', 'first_name=John', [ |
| 88 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
| 89 | + ]); |
| 90 | + |
| 91 | + $request = $adapter->createRequest($payload); |
| 92 | + |
| 93 | + $this->assertSame('POST', strtoupper($request->getMethod())); |
| 94 | + $this->assertSame('first_name=John', (string) $request->getBody()); |
| 95 | + } |
| 96 | + |
| 97 | + #[TestDox('GET request with PSR-17 factories uses them for request building')] |
| 98 | + public function testGetRequestWithPsr17Factories(): void |
| 99 | + { |
| 100 | + $client = $this->makeStubClient(); |
| 101 | + [$requestFactory, $streamFactory] = $this->makePsr17Factories(); |
| 102 | + |
| 103 | + $adapter = new PsrClientAdapter($client, $requestFactory, $streamFactory); |
| 104 | + |
| 105 | + $payload = $this->makePayload('get', 'https://example.com/api/v2/customers', 'limit=5', [ |
| 106 | + 'Authorization' => 'Basic dGVzdA==', |
| 107 | + ]); |
| 108 | + |
| 109 | + $request = $adapter->createRequest($payload); |
| 110 | + |
| 111 | + $this->assertSame('GET', strtoupper($request->getMethod())); |
| 112 | + $this->assertStringContainsString('limit=5', (string) $request->getUri()); |
| 113 | + $this->assertSame('Basic dGVzdA==', $request->getHeaderLine('Authorization')); |
| 114 | + } |
| 115 | + |
| 116 | + #[TestDox('POST request with PSR-17 factories uses them for request building')] |
| 117 | + public function testPostRequestWithPsr17Factories(): void |
| 118 | + { |
| 119 | + $client = $this->makeStubClient(); |
| 120 | + [$requestFactory, $streamFactory] = $this->makePsr17Factories(); |
| 121 | + |
| 122 | + $adapter = new PsrClientAdapter($client, $requestFactory, $streamFactory); |
| 123 | + |
| 124 | + $payload = $this->makePayload('post', 'https://example.com/api/v2/customers', 'first_name=Jane', [ |
| 125 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
| 126 | + ]); |
| 127 | + |
| 128 | + $request = $adapter->createRequest($payload); |
| 129 | + |
| 130 | + $this->assertSame('POST', strtoupper($request->getMethod())); |
| 131 | + $this->assertSame('first_name=Jane', (string) $request->getBody()); |
| 132 | + } |
| 133 | + |
| 134 | + #[TestDox('Invalid HTTP method throws an exception')] |
| 135 | + public function testInvalidHttpMethodThrows(): void |
| 136 | + { |
| 137 | + $client = $this->makeStubClient(); |
| 138 | + $adapter = new PsrClientAdapter($client); |
| 139 | + |
| 140 | + $payload = $this->makePayload('delete', 'https://example.com/api/v2/customers/123'); |
| 141 | + |
| 142 | + $this->expectException(\Exception::class); |
| 143 | + $this->expectExceptionMessageMatches('/Invalid http method delete/i'); |
| 144 | + |
| 145 | + $adapter->createRequest($payload); |
| 146 | + } |
| 147 | + |
| 148 | + #[TestDox('Existing HttpClientFactory injection still works (regression)')] |
| 149 | + public function testExistingHttpClientFactoryInjectionStillWorks(): void |
| 150 | + { |
| 151 | + $mockFactory = new MockGuzzleFactory( |
| 152 | + 1.0, |
| 153 | + 3.0, |
| 154 | + [], |
| 155 | + 200, |
| 156 | + '{"list":[],"next_offset":null}' |
| 157 | + ); |
| 158 | + |
| 159 | + $chargebeeClient = new ChargebeeClient([ |
| 160 | + 'site' => 'test-site', |
| 161 | + 'apiKey' => 'test-api-key', |
| 162 | + ], $mockFactory); |
| 163 | + |
| 164 | + // Verify no exception is thrown during construction |
| 165 | + $this->assertInstanceOf(ChargebeeClient::class, $chargebeeClient); |
| 166 | + } |
| 167 | + |
| 168 | + #[TestDox('PSR-18 ClientInterface can be injected directly into ChargebeeClient')] |
| 169 | + public function testPsrClientInterfaceInjectionIntoChargebeeClient(): void |
| 170 | + { |
| 171 | + $stubClient = $this->makeStubClient( |
| 172 | + new Response(200, [], '{"list":[],"next_offset":null}') |
| 173 | + ); |
| 174 | + |
| 175 | + $chargebeeClient = new ChargebeeClient([ |
| 176 | + 'site' => 'test-site', |
| 177 | + 'apiKey' => 'test-api-key', |
| 178 | + ], $stubClient); |
| 179 | + |
| 180 | + $this->assertInstanceOf(ChargebeeClient::class, $chargebeeClient); |
| 181 | + } |
| 182 | + |
| 183 | + #[TestDox('PSR-18 + PSR-17 factories can be injected into ChargebeeClient')] |
| 184 | + public function testPsrClientWithFactoriesInjectionIntoChargebeeClient(): void |
| 185 | + { |
| 186 | + $stubClient = $this->makeStubClient( |
| 187 | + new Response(200, [], '{"list":[],"next_offset":null}') |
| 188 | + ); |
| 189 | + [$requestFactory, $streamFactory] = $this->makePsr17Factories(); |
| 190 | + |
| 191 | + $chargebeeClient = new ChargebeeClient([ |
| 192 | + 'site' => 'test-site', |
| 193 | + 'apiKey' => 'test-api-key', |
| 194 | + ], $stubClient, $requestFactory, $streamFactory); |
| 195 | + |
| 196 | + $this->assertInstanceOf(ChargebeeClient::class, $chargebeeClient); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Returns minimal anonymous-class implementations of PSR-17 factories |
| 201 | + * backed by Guzzle PSR-7 objects, so no extra test dependency is needed. |
| 202 | + * |
| 203 | + * @return array{RequestFactoryInterface, StreamFactoryInterface} |
| 204 | + */ |
| 205 | + private function makePsr17Factories(): array |
| 206 | + { |
| 207 | + $requestFactory = new class implements RequestFactoryInterface { |
| 208 | + public function createRequest(string $method, $uri): RequestInterface |
| 209 | + { |
| 210 | + return new \GuzzleHttp\Psr7\Request($method, $uri); |
| 211 | + } |
| 212 | + }; |
| 213 | + |
| 214 | + $streamFactory = new class implements StreamFactoryInterface { |
| 215 | + public function createStream(string $content = ''): StreamInterface |
| 216 | + { |
| 217 | + return \GuzzleHttp\Psr7\Utils::streamFor($content); |
| 218 | + } |
| 219 | + |
| 220 | + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 221 | + { |
| 222 | + return \GuzzleHttp\Psr7\Utils::streamFor(fopen($filename, $mode)); |
| 223 | + } |
| 224 | + |
| 225 | + public function createStreamFromResource($resource): StreamInterface |
| 226 | + { |
| 227 | + return \GuzzleHttp\Psr7\Utils::streamFor($resource); |
| 228 | + } |
| 229 | + }; |
| 230 | + |
| 231 | + return [$requestFactory, $streamFactory]; |
| 232 | + } |
| 233 | +} |
0 commit comments