Skip to content

Commit

Permalink
Merge pull request #19 from ergebnis/feature/url
Browse files Browse the repository at this point in the history
Enhancement: Add Url
  • Loading branch information
localheinz authored Jan 9, 2020
2 parents bb8e8eb + fc17963 commit 5a3e779
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ $range = License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
);
$url = License\Url::fromString('https://github.com/ergebnis/license');

$header = <<<EOF
Copyright (c) {$range->toString()} {$holder->toString()}
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
@see {$url->toString()}
EOF;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header));
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ For a full diff see [`675601b...master`][675601b...master].
* Added `Holder` ([#7]), by [@localheinz]
* Added `Year` ([#10]), by [@localheinz]
* Added `Range` ([#12]), by [@localheinz]
* Added `Url` ([#19]), by [@localheinz]

[675601b...master]: https://github.com/ergebnis/license/compare/675601b...master

[#7]: https://github.com/ergebnis/license/pull/7
[#10]: https://github.com/ergebnis/license/pull/10
[#12]: https://github.com/ergebnis/license/pull/12
[#19]: https://github.com/ergebnis/license/pull/18

[@localheinz]: https://github.com/localheinz
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
],
"require": {
"php": "^7.1"
"php": "^7.1",
"ext-filter": "*"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.2.0",
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/Exception/InvalidUrl.php
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
));
}
}
47 changes: 47 additions & 0 deletions src/Url.php
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;
}
}
42 changes: 42 additions & 0 deletions test/Unit/Exception/InvalidUrlTest.php
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());
}
}
111 changes: 111 additions & 0 deletions test/Unit/UrlTest.php
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',
];
}
}

0 comments on commit 5a3e779

Please sign in to comment.