Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
Moved validation of ENV base URI to Assert component
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmaron committed Apr 30, 2019
1 parent 281b0d0 commit aa28aa2
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 52 deletions.
62 changes: 17 additions & 45 deletions src/AbstractReportingCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';

// </editor-fold>

Expand Down Expand Up @@ -506,7 +506,7 @@ public function getClient(): ?Client
if (!$this->client instanceof Client) {

$headers = [
'Authorization' => $this->getAuthorizationHeader()
'Authorization' => $this->getAuthorizationHeader(),
];

$options = [
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/Assert/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Assert extends AbstractAssert
use AssertApiKeyTrait;
use AssertArrayTrait;
use AssertBase64DataTrait;
use AssertBaseUriTrait;
use AssertBooleanTrait;
use AssertCultureTrait;
use AssertDateTimeTrait;
Expand Down
55 changes: 55 additions & 0 deletions src/Assert/AssertBaseUriTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
* @copyright © 2019 Text Control GmbH
*/

namespace TxTextControl\ReportingCloud\Assert;

use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
use TxTextControl\ReportingCloud\ReportingCloud;
use TxTextControl\ReportingCloud\Stdlib\StringUtils;

/**
* Trait AssertBaseUriTrait
*
* @package TxTextControl\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
trait AssertBaseUriTrait
{
/**
* @param mixed $value
*
* @return string
*/
abstract protected static function valueToString($value): string;

/**
* Check value is a known base URI
*
* @param mixed $value
* @param string $message
*/
public static function assertBaseUri($value, string $message = ''): void
{
$baseUri = ReportingCloud::DEFAULT_BASE_URI;

$host1 = (string) parse_url($baseUri, PHP_URL_HOST);
$host2 = (string) parse_url($value, PHP_URL_HOST);

if (!StringUtils::endsWith($host2, $host1)) {
$format = $message ?: 'Expected base URI to end in %2$s. Got %1$s';
$message = sprintf($format, self::valueToString($value), self::valueToString($host1));
throw new InvalidArgumentException($message);
}
}
}
47 changes: 42 additions & 5 deletions src/Stdlib/ConsoleUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,57 @@ public static function writeLn(string $format = '', ...$args): void
*
* @return string|null
*/
protected static function getValueFromConstOrEnvVar($key): ?string
private static function getValueFromConstOrEnvVar($key): ?string
{
$ret = self::getValueFromConst($key);

if (null !== $ret) {
return $ret;
}

$ret = self::getValueFromEnvVar($key);

if (null !== $ret) {
return $ret;
}

return null;
}

/**
* Return a value from a PHP constant
*
* @param $key
*
* @return string|null
*/
private static function getValueFromConst($key): ?string
{
if (defined($key)) {
$ret = (string) constant($key);
$ret = trim($ret);

return $ret;
if (!empty($ret)) {
return $ret;
}
}

return null;
}
/**
* Return a value from an environmental variable
*
* @param $key
*
* @return string|null
*/
private static function getValueFromEnvVar($key): ?string
{
if (getenv($key)) {
$ret = (string) getenv($key);
$ret = trim($ret);

return $ret;
if (!empty($ret)) {
return $ret;
}
}

return null;
Expand Down
66 changes: 66 additions & 0 deletions test/Assert/AssertBaseUriTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
* @copyright © 2019 Text Control GmbH
*/

namespace TxTextControlTest\ReportingCloud\Assert;

use TxTextControl\ReportingCloud\Assert\Assert;
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;

/**
* Trait AssertBaseUriTestTrait
*
* @package TxTextControlTest\ReportingCloud
* @author Jonathan Maron (@JonathanMaron)
*/
trait AssertBaseUriTestTrait
{
// <editor-fold desc="Abstract methods">

/**
* @param mixed $condition
* @param string $message
*/
abstract public static function assertTrue($condition, string $message = ''): void;

// </editor-fold>

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);
}
}
1 change: 1 addition & 0 deletions test/Assert/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AssertTest extends TestCase
{
use AssertApiKeyTestTrait;
use AssertBase64DataTestTrait;
use AssertBaseUriTestTrait;
use AssertCultureTestTrait;
use AssertDateTimeTestTrait;
use AssertDocumentDividerTestTrait;
Expand Down
3 changes: 1 addition & 2 deletions test/ReportingCloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit aa28aa2

Please sign in to comment.