generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ergebnis/feature/url
Enhancement: Add Url
- Loading branch information
Showing
8 changed files
with
234 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Exception; | ||
|
||
final class InvalidUrl extends \InvalidArgumentException implements Exception | ||
{ | ||
public static function fromValue(string $value): self | ||
{ | ||
return new self(\sprintf( | ||
'Value "%s" is not a valid URL.', | ||
$value | ||
)); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License; | ||
|
||
final class Url | ||
{ | ||
private $value; | ||
|
||
private function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* | ||
* @throws Exception\InvalidUrl | ||
* | ||
* @return self | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
$trimmed = \trim($value); | ||
|
||
if (false === \filter_var($trimmed, \FILTER_VALIDATE_URL)) { | ||
throw Exception\InvalidUrl::fromValue($value); | ||
} | ||
|
||
return new self($trimmed); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit\Exception; | ||
|
||
use Ergebnis\License\Exception\InvalidUrl; | ||
use Ergebnis\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\Exception\InvalidUrl | ||
*/ | ||
final class InvalidUrlTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
public function testFromValueReturnsInvalidUrl(): void | ||
{ | ||
$value = self::faker()->sentence; | ||
|
||
$exception = InvalidUrl::fromValue($value); | ||
|
||
$expected = \sprintf( | ||
'Value "%s" is not a valid URL.', | ||
$value | ||
); | ||
|
||
self::assertSame($expected, $exception->getMessage()); | ||
} | ||
} |
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,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit; | ||
|
||
use Ergebnis\License\Exception; | ||
use Ergebnis\License\Url; | ||
use Ergebnis\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\Url | ||
* | ||
* @uses \Ergebnis\License\Exception\InvalidUrl | ||
*/ | ||
final class UrlTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
/** | ||
* @dataProvider provideInvalidValue | ||
* | ||
* @param string $value | ||
*/ | ||
public function testFromStringRejectsInvalidValue(string $value): void | ||
{ | ||
$this->expectException(Exception\InvalidUrl::class); | ||
|
||
Url::fromString($value); | ||
} | ||
|
||
public function provideInvalidValue(): \Generator | ||
{ | ||
$values = [ | ||
'string-arbitrary' => self::faker()->sentence, | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
]; | ||
|
||
foreach ($values as $key => $value) { | ||
yield $key => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideValidValue | ||
* | ||
* @param string $value | ||
*/ | ||
public function testFromStringReturnsUrl(string $value): void | ||
{ | ||
$url = Url::fromString($value); | ||
|
||
self::assertSame($value, $url->toString()); | ||
} | ||
|
||
public function provideValidValue(): \Generator | ||
{ | ||
foreach (self::validValues() as $key => $value) { | ||
yield $key => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideUntrimmedValue | ||
* | ||
* @param string $value | ||
*/ | ||
public function testFromStringReturnsUrlWithTrimmedValue(string $value): void | ||
{ | ||
$url = Url::fromString($value); | ||
|
||
self::assertSame(\trim($value), $url->toString()); | ||
} | ||
|
||
public function provideUntrimmedValue(): \Generator | ||
{ | ||
foreach (self::validValues() as $key => $value) { | ||
yield $key => [ | ||
\sprintf( | ||
" %s \n\n", | ||
$value | ||
), | ||
]; | ||
} | ||
} | ||
|
||
private static function validValues(): array | ||
{ | ||
return [ | ||
'string-https' => 'https://github.com/ergebnis/php-cs-fixer-config', | ||
'string-http' => 'https://github.com/ergebnis/php-cs-fixer-config', | ||
]; | ||
} | ||
} |