This repository has been archived by the owner on Sep 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved validation of ENV base URI to Assert component
- Loading branch information
1 parent
281b0d0
commit aa28aa2
Showing
7 changed files
with
183 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters