Skip to content

Commit

Permalink
Increase test coverage of the following Rules
Browse files Browse the repository at this point in the history
- Base64
- Base
- Bsn
- Contains
- Executable
- FilterVar
- In
- Ip
- Isbn
- NoWhiteSpace
- Pesel
- Phone
- PolishIdCard
- PortugueseNif
- PublicDomainSuffix
- StartsWith
- Subset
- Version
- VideoUrl
  • Loading branch information
dcorrea777 committed Apr 21, 2024
1 parent 2ae1df1 commit 5084aae
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/unit/Rules/Base64Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static function providerForInvalidInput(): iterable
$rule = new Base64();

return [
[$rule, []],
[$rule, 1.2],
[$rule, false],
[$rule, 123],
[$rule, null],
[$rule, ''],
[$rule, 'hello!'],
[$rule, '=c3VyZS4'],
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/Rules/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;

#[Group('rule')]
#[CoversClass(Base::class)]
final class BaseTest extends RuleTestCase
{
#[Test]
public function itShouldThrowsExceptionWhenBaseIsNotValid(): void
{
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('a base between 1 and 62 is required');

(new Base(63))->evaluate('011010001');
}

/** @return iterable<array{Base, mixed}> */
public static function providerForValidInput(): iterable
{
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Rules/BsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(Bsn::class)]
Expand Down Expand Up @@ -42,6 +43,9 @@ public static function providerForInvalidInput(): iterable
$rule = new Bsn();

return [
[$rule, []],
[$rule, new stdClass()],
[$rule, null],
[$rule, '1234567890'],
[$rule, '0987654321'],
[$rule, '13579024'],
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Rules/ContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(Contains::class)]
Expand Down Expand Up @@ -40,10 +41,17 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
[new Contains('', false), 'abc'],
[new Contains(null, false), null],
[new Contains(null, false), []],
[new Contains(new stdClass(), false), new stdClass()],
[new Contains('foo', false), ''],
[new Contains('bat', false), ['bar', 'foo']],
[new Contains('foo', false), 'barfaabaz'],
[new Contains('foo', false), 'faabarbaz'],
[new Contains(null, true), null],
[new Contains(null, true), []],
[new Contains(new stdClass(), true), new stdClass()],
[new Contains('foo', true), ''],
[new Contains('bat', true), ['BAT', 'foo']],
[new Contains('bat', true), ['BaT', 'Batata']],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Rules/ExecutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Respect\Validation\Test\RuleTestCase;
use SplFileInfo;
use SplFileObject;
use stdClass;

#[Group('rule')]
#[CoversClass(Executable::class)]
Expand All @@ -37,6 +38,9 @@ public static function providerForInvalidInput(): iterable
$rule = new Executable();

return [
[$rule, []],
[$rule, new stdClass()],
[$rule, null],
[$rule, 'tests/fixtures/valid-image.gif'],
[$rule, new SplFileInfo('tests/fixtures/valid-image.jpg')],
[$rule, new SplFileObject('tests/fixtures/valid-image.png')],
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Rules/FilterVarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static function providerForInvalidInput(): iterable
[new FilterVar(FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED), 'http://example.com'],
[new FilterVar(FILTER_VALIDATE_DOMAIN), '.com'],
[new FilterVar(FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME), '@local'],
[new FilterVar(FILTER_VALIDATE_INT, []), 1.4],
[new FilterVar(FILTER_VALIDATE_INT, 2), 1.4],
];
}
}
1 change: 1 addition & 0 deletions tests/unit/Rules/InTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
[new In('0', true), 'abc'],
[new In('0'), null],
[new In(0, true), null],
[new In('', true), null],
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Rules/IpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function providerForInvalidRanges(): array
return [
['192.168'],
['asd'],
['-'],
['192.168.0.0-192.168.0.256'],
['192.168.0.0-192.168.0.1/4'],
['192.168.0/1'],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Rules/IsbnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(Isbn::class)]
Expand Down Expand Up @@ -39,6 +40,9 @@ public static function providerForInvalidInput(): iterable
$sut = new Isbn();

return [
[$sut, []],
[$sut, null],
[$sut, new stdClass()],
[$sut, 'ISBN 11978-0-596-52068-7'],
[$sut, 'ISBN-12: 978-0-596-52068-7'],
[$sut, '978 10 596 52068 7'],
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Rules/NoWhitespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(NoWhitespace::class)]
Expand All @@ -37,6 +38,8 @@ public static function providerForInvalidInput(): iterable
$rule = new NoWhitespace();

return [
[$rule, []],
[$rule, new stdClass()],
[$rule, ' '],
[$rule, 'w poiur'],
[$rule, ' '],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Rules/PeselTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(Pesel::class)]
Expand Down Expand Up @@ -40,6 +41,9 @@ public static function providerForInvalidInput(): iterable
$rule = new Pesel();

return [
[$rule, null],
[$rule, []],
[$rule, new stdClass()],
[$rule, '1'],
[$rule, '22'],
[$rule, 'PESEL'],
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Rules/PhoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\TestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(Phone::class)]
Expand Down Expand Up @@ -47,6 +49,15 @@ public function shouldValidateInvalidInputWithCountryCode(string $countryCode, m
self::assertInvalidInput(new Phone($countryCode), $input);
}

#[Test]
public function itShouldThrowsExceptionWhenCountryCodeIsNotValid(): void
{
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('Invalid country code BRR');

(new Phone('BRR'))->evaluate('+1 11 91111 1111');
}

/** @return array<array{mixed}> */
public static function providerForValidInputWithoutCountryCode(): array
{
Expand All @@ -67,6 +78,8 @@ public static function providerForValidInputWithoutCountryCode(): array
public static function providerForInvalidInputWithoutCountryCode(): array
{
return [
[null],
[new stdClass()],
['+1-650-253-00-0'],
['33(020) 7777 7777'],
['33(020)7777 7777'],
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/Rules/PolishIdCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\RuleTestCase;
use stdClass;

#[Group('rule')]
#[CoversClass(PolishIdCard::class)]
Expand All @@ -35,6 +36,10 @@ public static function providerForInvalidInput(): iterable
$rule = new PolishIdCard();

return [
[$rule, null],
[$rule, new stdClass()],
[$rule, []],
[$rule, '999205411'],
[$rule, 'AAAAAAAAA'],
[$rule, 'APH 505567'],
[$rule, 'AYE205411'],
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/Rules/PortugueseNifTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static function providerForInvalidInput(): iterable

return [
// Check digit is wrong
[$rule, '429468882'],
[$rule, '739468882'],
[$rule, '939468882'],
[$rule, '129468882'],
[$rule, '220005245'],
[$rule, '389684008'],
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Rules/PublicDomainSuffixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function providerForValidInput(): iterable
$rule = new PublicDomainSuffix();

return [
[$rule, ''],
[$rule, 'co.uk'],
[$rule, 'nom.br'],
[$rule, 'WWW.CK'],
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Rules/StartsWithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
[new StartsWith(123), 123],
[new StartsWith(123, true), 123],
[new StartsWith('foo'), ''],
[new StartsWith('bat'), ['foo', 'bar']],
[new StartsWith('foo'), 'barfaabaz'],
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Rules/SubsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
[new Subset([]), '1'],
[new Subset([]), [1]],
[new Subset([1]), [2]],
[new Subset([1, 2]), [1, 2, 3]],
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Rules/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static function providerForInvalidInput(): iterable
$sut = new Version();

return [
'int' => [$sut, 1],
'float' => [$sut, 1.2],
'empty' => [$sut, ''],
'1.3.7--' => [$sut, '1.3.7--'],
'1.3.7++' => [$sut, '1.3.7++'],
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/Rules/VideoUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase;

#[Group('rule')]
#[CoversClass(VideoUrl::class)]
final class VideoUrlTest extends RuleTestCase
{
#[Test]
public function itShouldThrowsExceptionWhenVideoUrlIsNotValid(): void
{
$this->expectException(InvalidRuleConstructorException::class);
$this->expectExceptionMessage('"tiktok" is not a recognized video service.');

(new VideoUrl('tiktok'))->evaluate('https://tiktok.com/video/71787467');
}

/** @return iterable<array{VideoUrl, mixed}> */
public static function providerForValidInput(): iterable
{
Expand All @@ -40,6 +51,7 @@ public static function providerForValidInput(): iterable
public static function providerForInvalidInput(): iterable
{
return [
'vimeo service with invalid URL' => [new VideoUrl('vimeo'), 1],
'vimeo service with youtube url' => [new VideoUrl('vimeo'), 'https://www.youtube.com/watch?v=netHLn9TScY'],
'youtube service with vimeo url' => [new VideoUrl('youtube'), 'https://vimeo.com/71787467'],
'no service with example.com url' => [new VideoUrl(), 'example.com'],
Expand Down

0 comments on commit 5084aae

Please sign in to comment.