From aa28aa20156d1bbc566c1a7e2be3b89d84441355 Mon Sep 17 00:00:00 2001 From: Jonathan Maron Date: Tue, 30 Apr 2019 08:53:33 +0200 Subject: [PATCH] Moved validation of ENV base URI to Assert component --- src/AbstractReportingCloud.php | 62 +++++++----------------- src/Assert/Assert.php | 1 + src/Assert/AssertBaseUriTrait.php | 55 +++++++++++++++++++++ src/Stdlib/ConsoleUtils.php | 47 ++++++++++++++++-- test/Assert/AssertBaseUriTestTrait.php | 66 ++++++++++++++++++++++++++ test/Assert/AssertTest.php | 1 + test/ReportingCloudTest.php | 3 +- 7 files changed, 183 insertions(+), 52 deletions(-) create mode 100644 src/Assert/AssertBaseUriTrait.php create mode 100644 test/Assert/AssertBaseUriTestTrait.php diff --git a/src/AbstractReportingCloud.php b/src/AbstractReportingCloud.php index a17e838f..9a93654b 100644 --- a/src/AbstractReportingCloud.php +++ b/src/AbstractReportingCloud.php @@ -18,11 +18,11 @@ use GuzzleHttp\Exception\TransferException; use GuzzleHttp\RequestOptions; use Psr\Http\Message\ResponseInterface; +use TxTextControl\ReportingCloud\Assert\Assert; use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; use TxTextControl\ReportingCloud\Exception\RuntimeException; use TxTextControl\ReportingCloud\Filter\Filter; use TxTextControl\ReportingCloud\Stdlib\ConsoleUtils; -use TxTextControl\ReportingCloud\Stdlib\StringUtils; /** * Abstract ReportingCloud @@ -60,35 +60,35 @@ abstract class AbstractReportingCloud * * @const DEFAULT_BASE_URI */ - protected const DEFAULT_BASE_URI = 'https://api.reporting.cloud'; + public const DEFAULT_BASE_URI = 'https://api.reporting.cloud'; /** - * Default version string of backend + * Default debug flag of REST client * - * @const DEFAULT_VERSION + * @const DEFAULT_DEBUG */ - protected const DEFAULT_VERSION = 'v1'; + protected const DEFAULT_DEBUG = false; /** - * Default timeout of backend in seconds + * Default test flag of backend * - * @const DEFAULT_TIMEOUT + * @const DEFAULT_TEST */ - protected const DEFAULT_TIMEOUT = 120; + protected const DEFAULT_TEST = false; /** - * Default test flag of backend + * Default timeout of backend in seconds * - * @const DEFAULT_TEST + * @const DEFAULT_TIMEOUT */ - protected const DEFAULT_TEST = false; + protected const DEFAULT_TIMEOUT = 120; /** - * Default debug flag of REST client + * Default version string of backend * - * @const DEFAULT_DEBUG + * @const DEFAULT_VERSION */ - protected const DEFAULT_DEBUG = false; + protected const DEFAULT_VERSION = 'v1'; // @@ -506,7 +506,7 @@ public function getClient(): ?Client if (!$this->client instanceof Client) { $headers = [ - 'Authorization' => $this->getAuthorizationHeader() + 'Authorization' => $this->getAuthorizationHeader(), ]; $options = [ @@ -546,7 +546,8 @@ public function setClient(Client $client): self protected function setDefaultOptions(): self { if (null === $this->getBaseUri()) { - $baseUri = $this->getBaseUriFromConstOrEnvVar() ?? self::DEFAULT_BASE_URI; + $baseUri = ConsoleUtils::baseUri() ?? self::DEFAULT_BASE_URI; + Assert::assertBaseUri($baseUri); $this->setBaseUri($baseUri); } @@ -569,35 +570,6 @@ protected function setDefaultOptions(): self return $this; } - /** - * Return the base URI from the PHP const or environment variable "REPORTING_CLOUD_BASE_URI", - * checking that the hostname and sub-domain match the known hostname and sub-domain. - * - * Return null, if the environment variable has not been set or is empty. - * - * @throws InvalidArgumentException - * @return string|null - */ - protected function getBaseUriFromConstOrEnvVar(): ?string - { - $baseUri = ConsoleUtils::baseUri(); - - if (empty($baseUri)) { - return null; - } - - $sdkHost = (string) parse_url(self::DEFAULT_BASE_URI, PHP_URL_HOST); - $envHost = (string) parse_url($baseUri, PHP_URL_HOST); - - if (!StringUtils::endsWith($envHost, $sdkHost)) { - $format = 'Base URI from environment variable "%s" with value "%s" does not end in "%s"'; - $message = sprintf($format, ConsoleUtils::BASE_URI, $baseUri, $sdkHost); - throw new InvalidArgumentException($message); - } - - return $baseUri; - } - /** * Request the URI with options * diff --git a/src/Assert/Assert.php b/src/Assert/Assert.php index 3ac21ce5..bc08af23 100644 --- a/src/Assert/Assert.php +++ b/src/Assert/Assert.php @@ -25,6 +25,7 @@ class Assert extends AbstractAssert use AssertApiKeyTrait; use AssertArrayTrait; use AssertBase64DataTrait; + use AssertBaseUriTrait; use AssertBooleanTrait; use AssertCultureTrait; use AssertDateTimeTrait; diff --git a/src/Assert/AssertBaseUriTrait.php b/src/Assert/AssertBaseUriTrait.php new file mode 100644 index 00000000..7b6f17a0 --- /dev/null +++ b/src/Assert/AssertBaseUriTrait.php @@ -0,0 +1,55 @@ + + + /** + * @param mixed $condition + * @param string $message + */ + abstract public static function assertTrue($condition, string $message = ''): void; + + // + + public function testAssertBaseUri(): void + { + Assert::assertBaseUri('https://phpunit-api.reporting.cloud'); + + $this->assertTrue(true); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Expected base URI to end in "api.reporting.cloud". Got "https://api.example.com" + */ + public function testAssertBaseUriWithInvalidBaseUri(): void + { + Assert::assertBaseUri('https://api.example.com'); + + $this->assertTrue(true); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Expected base URI to end in "api.reporting.cloud". Got "https://api.reporting.cloud.de" + */ + public function testAssertBaseUriInvalidBaseUriKnownHost(): void + { + Assert::assertBaseUri('https://api.reporting.cloud.de'); + + $this->assertTrue(true); + } +} diff --git a/test/Assert/AssertTest.php b/test/Assert/AssertTest.php index 51eba50a..16c1a337 100644 --- a/test/Assert/AssertTest.php +++ b/test/Assert/AssertTest.php @@ -28,6 +28,7 @@ class AssertTest extends TestCase { use AssertApiKeyTestTrait; use AssertBase64DataTestTrait; + use AssertBaseUriTestTrait; use AssertCultureTestTrait; use AssertDateTimeTestTrait; use AssertDocumentDividerTestTrait; diff --git a/test/ReportingCloudTest.php b/test/ReportingCloudTest.php index 6064ba8d..9265f6fc 100644 --- a/test/ReportingCloudTest.php +++ b/test/ReportingCloudTest.php @@ -197,8 +197,7 @@ public function testGetBaseUriFromEnvVarWithInvalidValue(): void $reportingCloud = new ReportingCloud(); } catch (InvalidArgumentException $e) { putenv("{$envVarName}={$baseUri}"); - $expected = 'Base URI from environment variable "REPORTING_CLOUD_BASE_URI" with value '; - $expected .= '"https://www.example.com" does not end in "api.reporting.cloud"'; + $expected = 'Expected base URI to end in "api.reporting.cloud". Got "https://www.example.com"'; $this->assertSame($expected, $e->getMessage()); } unset($reportingCloud);