Skip to content

Commit

Permalink
Merge pull request #30 from pact-foundation/url-validator
Browse files Browse the repository at this point in the history
refactor(2.0): Add UrlValidator
  • Loading branch information
tienvx authored Apr 16, 2024
2 parents bf27400 + 61b3687 commit 5f3de63
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Attribute/Validator/UrlValidator.php
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;
}
}
84 changes: 84 additions & 0 deletions tests/Unit/Attribute/Validator/UrlValidatorTest.php
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);
}
}

0 comments on commit 5f3de63

Please sign in to comment.