diff --git a/composer.json b/composer.json index 30196e2..33caa6b 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,17 @@ "name": "Adam Zimmermann", "email": "adam@chromatichq.com", "homepage": "https://www.drupal.org/u/adamzimmermann" + }, + { + "name": "Kristofer Widholm", + "email": "kristofer@chromatichq.com", + "homepage": "https://www.drupal.org/u/apotek" } ], "require": { "php": ">=8.1", "ext-json": "*", - "guzzlehttp/guzzle": "^6 || ^7" + "guzzlehttp/guzzle": "^6 || ^7 || ^8" }, "autoload": { "psr-4": { diff --git a/src/Exceptions/OrangeDamException.php b/src/Exceptions/OrangeDamException.php index 2a09b6b..dc76d8f 100644 --- a/src/Exceptions/OrangeDamException.php +++ b/src/Exceptions/OrangeDamException.php @@ -44,7 +44,6 @@ public static function create(RequestException $guzzleException): self $guzzleException->getCode(), $guzzleException ); - $e->response = $guzzleException->getResponse(); return $e; diff --git a/src/Factory.php b/src/Factory.php index 8de7842..26884fb 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -39,9 +39,8 @@ class Factory */ public function __construct(array $config = [], ?Client $client = null, array $clientOptions = []) { - if (is_null($client)) { - $client = new Client($config, null, $clientOptions); - } + // Null coalesce assignment of new instance if necessary. + $client ??= new Client($config, null, $clientOptions); $this->client = $client; } diff --git a/src/Http/Client.php b/src/Http/Client.php index 261d559..0739c49 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -77,14 +77,13 @@ public function __construct(array $config = [], ?GuzzleClient $httpClient = null throw new OrangeDamException('You must provide Orange DAM API Base path.'); } - // Creates a new instance - if (is_null($httpClient)) { - $httpClient = new GuzzleClient([ - 'cookies' => true, - 'timeout' => self::REQUEST_TIMEOUT, - 'base_uri' => $config['base_path'], - ]); - } + // Null coalesce assignment of new instance. + $httpClient ??= new GuzzleClient([ + 'cookies' => true, + 'timeout' => self::REQUEST_TIMEOUT, + 'base_uri' => $config['base_path'], + ]); + $this->httpClient = $httpClient; } diff --git a/src/Http/Response.php b/src/Http/Response.php index b102cae..1409806 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -59,9 +59,8 @@ public function __get($name) */ public function getData() { - if (is_null($this->data)) { - $this->data = $this->getDataFromResponse($this->response); - } + // Null coalesce assignment of new data if data is null. + $this->data ??= $this->getDataFromResponse($this->response); return $this->data; } diff --git a/tests/Endpoints/AssetLinkTest.php b/tests/Chromatic/OrangeDam/Endpoints/AssetLinkTest.php similarity index 100% rename from tests/Endpoints/AssetLinkTest.php rename to tests/Chromatic/OrangeDam/Endpoints/AssetLinkTest.php diff --git a/tests/Chromatic/OrangeDam/Exceptions/OrangeDamExceptionTest.php b/tests/Chromatic/OrangeDam/Exceptions/OrangeDamExceptionTest.php new file mode 100644 index 0000000..aa0be3a --- /dev/null +++ b/tests/Chromatic/OrangeDam/Exceptions/OrangeDamExceptionTest.php @@ -0,0 +1,69 @@ +assertInstanceOf(OrangeDamException::class, $exception); + } + + /** + * Test OrangeDamException message sanitization works as expected. + */ + public function testSanitizeResponse(): void + { + // Handle Guzzle RequestException constructor change in version 8. + $client_class = ClientInterface::class; + if (defined("$client_class::MAJOR_VERSION") && ClientInterface::MAJOR_VERSION > 7) { + $e = new \GuzzleHttp\Exception\ResponseException( + 'token=12345 Test response message.', + new Request('GET', '/'), + new Response(200), + ); + } else { + $e = new RequestException( + 'token=12345 Test response message.', + new Request('GET', '/'), + new Response(200), + ); + } + $orangeException = OrangeDamException::create($e); + $this->assertSame($orangeException->getMessage(), 'token=*** Test response message.'); + } + + /** + * Test OrangeDamException getResponse() method. + */ + public function testGetResponse(): void + { + // Handle Guzzle RequestException constructor change in version 8. + $client_class = ClientInterface::class; + if (defined("$client_class::MAJOR_VERSION") && ClientInterface::MAJOR_VERSION > 7) { + $e = new \GuzzleHttp\Exception\ResponseException( + '', + new Request('GET', '/'), + new Response(200), + ); + } else { + $e = new RequestException( + '', + new Request('GET', '/'), + new Response(200), + ); + } + $orangeException = OrangeDamException::create($e); + $this->assertInstanceOf(Response::class, $orangeException->getResponse()); + } +} diff --git a/tests/FactoryTest.php b/tests/Chromatic/OrangeDam/FactoryTest.php similarity index 100% rename from tests/FactoryTest.php rename to tests/Chromatic/OrangeDam/FactoryTest.php diff --git a/tests/Http/ClientTest.php b/tests/Chromatic/OrangeDam/Http/ClientTest.php similarity index 97% rename from tests/Http/ClientTest.php rename to tests/Chromatic/OrangeDam/Http/ClientTest.php index 87a9a41..b3faff5 100644 --- a/tests/Http/ClientTest.php +++ b/tests/Chromatic/OrangeDam/Http/ClientTest.php @@ -69,7 +69,7 @@ public function testRequestUrlHasNoTrailingQuestionMark(): void $guzzle = new GuzzleClient(['handler' => $handlerStack, 'base_uri' => 'https://test.com']); $client = new Client(['base_path' => 'https://test.com'], $guzzle); $client->token = 'test-token'; - $client->request('get', '/some/endpoint'); + $client->request('GET', '/some/endpoint'); $uri = (string) $container[0]['request']->getUri(); $this->assertStringNotContainsString('?', $uri); diff --git a/tests/Http/ResponseTest.php b/tests/Chromatic/OrangeDam/Http/ResponseTest.php similarity index 100% rename from tests/Http/ResponseTest.php rename to tests/Chromatic/OrangeDam/Http/ResponseTest.php diff --git a/tests/Exceptions/OrangeDamExceptionTest.php b/tests/Exceptions/OrangeDamExceptionTest.php deleted file mode 100644 index 7ca30d0..0000000 --- a/tests/Exceptions/OrangeDamExceptionTest.php +++ /dev/null @@ -1,48 +0,0 @@ -assertInstanceOf(OrangeDamException::class, $exception); - } - - /** - * Test OrangeDamException message sanitization works as expected. - */ - public function testSanitizeResponse(): void - { - $e = new RequestException( - 'token=12345 Test response message.', - new Request('GET', '/'), - new Response(200) - ); - $orangeException = OrangeDamException::create($e); - $this->assertSame($orangeException->getMessage(), 'token=*** Test response message.'); - } - - /** - * Test OrangeDamException getResponse() method. - */ - public function testGetResponse(): void - { - $e = new RequestException( - '', - new Request('GET', '/'), - new Response(200) - ); - $orangeException = OrangeDamException::create($e); - $this->assertInstanceOf(Response::class, $orangeException->getResponse()); - } -}