Skip to content
Open
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
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/OrangeDamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static function create(RequestException $guzzleException): self
$guzzleException->getCode(),
$guzzleException
);

$e->response = $guzzleException->getResponse();

return $e;
Expand Down
5 changes: 2 additions & 3 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 7 additions & 8 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/Chromatic/OrangeDam/Exceptions/OrangeDamExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Chromatic\OrangeDam\Exceptions;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

final class OrangeDamExceptionTest extends TestCase
{
/**
* Test creation of OrangeDamException results in expected object.
*/
public function testClassConstructor(): void
{
$exception = new OrangeDamException('Test exception text.');
$this->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());
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 0 additions & 48 deletions tests/Exceptions/OrangeDamExceptionTest.php

This file was deleted.

Loading