forked from tienvx/composer-downloads-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #30 from pact-foundation/url-validator
refactor(2.0): Add UrlValidator
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Attribute\Validator; | ||
|
||
use Composer\Package\PackageInterface; | ||
use LastCall\DownloadsPlugin\Attribute\AttributeManagerInterface; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
|
||
class UrlValidator extends AbstractValidator | ||
{ | ||
public function __construct( | ||
string $id, | ||
PackageInterface $parent, | ||
private AttributeManagerInterface $attributeManager | ||
) { | ||
parent::__construct($id, $parent); | ||
} | ||
|
||
public function validate(mixed $value): string | ||
{ | ||
if (null === $value) { | ||
$this->throwException($this->getAttribute(), 'is required'); | ||
} | ||
|
||
if (!\is_string($value)) { | ||
$this->throwException($this->getAttribute(), sprintf('must be string, "%s" given', get_debug_type($value))); | ||
} | ||
|
||
$url = strtr($value, $this->attributeManager->get(Attribute::VARIABLES)); | ||
if (false === filter_var($url, \FILTER_VALIDATE_URL)) { | ||
$this->throwException($this->getAttribute(), 'is invalid url'); | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
public function getAttribute(): string | ||
{ | ||
return Attribute::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,84 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Tests\Unit\Attribute\Validator; | ||
|
||
use LastCall\DownloadsPlugin\Attribute\Validator\UrlValidator; | ||
use LastCall\DownloadsPlugin\Attribute\Validator\ValidatorInterface; | ||
use LastCall\DownloadsPlugin\Enum\Attribute; | ||
|
||
class UrlValidatorTest extends AbstractValidatorTestCase | ||
{ | ||
public function testNull(): void | ||
{ | ||
$this->parent->expects($this->once())->method('getName')->willReturn($this->parentName); | ||
$this->attributeManager->expects($this->never())->method('get'); | ||
$this->expectUnexpectedValueException('url', 'is required'); | ||
$this->validator->validate(null); | ||
} | ||
|
||
public function getInvalidUrlTypeTests(): array | ||
{ | ||
return [ | ||
[true, 'bool'], | ||
[false, 'bool'], | ||
[123, 'int'], | ||
[12.3, 'float'], | ||
[['key' => 'value'], 'array'], | ||
[(object) ['key' => 'value'], 'stdClass'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidUrlTypeTests | ||
*/ | ||
public function testInvalidUrlType(mixed $invalidUrlType, string $type): void | ||
{ | ||
$this->parent->expects($this->once())->method('getName')->willReturn($this->parentName); | ||
$this->attributeManager->expects($this->never())->method('get'); | ||
$this->expectUnexpectedValueException('url', sprintf('must be string, "%s" given', $type)); | ||
$this->validator->validate($invalidUrlType); | ||
} | ||
|
||
public function getInvalidUrlTests(): array | ||
{ | ||
return [ | ||
[''], | ||
['C:\Programs\PHP\php.ini'], | ||
['/var/www/project/uploads'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidUrlTests | ||
*/ | ||
public function testInvalidUrl(string $invalidUrl): void | ||
{ | ||
$this->attributeManager->expects($this->once())->method('get')->with(Attribute::VARIABLES)->willReturn([]); | ||
$this->parent->expects($this->once())->method('getName')->willReturn($this->parentName); | ||
$this->expectUnexpectedValueException('url', 'is invalid url'); | ||
$this->validator->validate($invalidUrl); | ||
} | ||
|
||
public function getValidateUrlTests(): array | ||
{ | ||
return [ | ||
['http://example/file.rar', [], 'http://example/file.rar'], | ||
['http://example/file-{$version}.rar', ['{$version}' => '1.2.3'], 'http://example/file-1.2.3.rar'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getValidateUrlTests | ||
*/ | ||
public function testValidateUrl(string $url, array $variables, string $expectedUrl): void | ||
{ | ||
$this->attributeManager->expects($this->once())->method('get')->with(Attribute::VARIABLES)->willReturn($variables); | ||
$this->parent->expects($this->never())->method('getName'); | ||
$this->assertSame($expectedUrl, $this->validator->validate($url)); | ||
} | ||
|
||
protected function createValidator(): ValidatorInterface | ||
{ | ||
return new UrlValidator($this->id, $this->parent, $this->attributeManager); | ||
} | ||
} |