diff --git a/tests/unit/Rules/Base64Test.php b/tests/unit/Rules/Base64Test.php index 362c6049a..cae654d8d 100644 --- a/tests/unit/Rules/Base64Test.php +++ b/tests/unit/Rules/Base64Test.php @@ -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'], diff --git a/tests/unit/Rules/BaseTest.php b/tests/unit/Rules/BaseTest.php index d8c744f58..24070b5a7 100644 --- a/tests/unit/Rules/BaseTest.php +++ b/tests/unit/Rules/BaseTest.php @@ -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 */ public static function providerForValidInput(): iterable { diff --git a/tests/unit/Rules/BsnTest.php b/tests/unit/Rules/BsnTest.php index 4dfbd066b..da1e47ab5 100644 --- a/tests/unit/Rules/BsnTest.php +++ b/tests/unit/Rules/BsnTest.php @@ -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)] @@ -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'], diff --git a/tests/unit/Rules/ContainsTest.php b/tests/unit/Rules/ContainsTest.php index 591e06ce3..62b6960ad 100644 --- a/tests/unit/Rules/ContainsTest.php +++ b/tests/unit/Rules/ContainsTest.php @@ -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)] @@ -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']], diff --git a/tests/unit/Rules/ExecutableTest.php b/tests/unit/Rules/ExecutableTest.php index a19db036e..4a9ecd1fa 100644 --- a/tests/unit/Rules/ExecutableTest.php +++ b/tests/unit/Rules/ExecutableTest.php @@ -14,6 +14,7 @@ use Respect\Validation\Test\RuleTestCase; use SplFileInfo; use SplFileObject; +use stdClass; #[Group('rule')] #[CoversClass(Executable::class)] @@ -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')], diff --git a/tests/unit/Rules/FilterVarTest.php b/tests/unit/Rules/FilterVarTest.php index e2393b046..1787d0028 100644 --- a/tests/unit/Rules/FilterVarTest.php +++ b/tests/unit/Rules/FilterVarTest.php @@ -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], ]; } } diff --git a/tests/unit/Rules/InTest.php b/tests/unit/Rules/InTest.php index 4e6f40793..1a4564a8b 100644 --- a/tests/unit/Rules/InTest.php +++ b/tests/unit/Rules/InTest.php @@ -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], diff --git a/tests/unit/Rules/IpTest.php b/tests/unit/Rules/IpTest.php index 8aff10d9b..38ff30fcd 100644 --- a/tests/unit/Rules/IpTest.php +++ b/tests/unit/Rules/IpTest.php @@ -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'], diff --git a/tests/unit/Rules/IsbnTest.php b/tests/unit/Rules/IsbnTest.php index a9b0579f6..5019bfb9a 100644 --- a/tests/unit/Rules/IsbnTest.php +++ b/tests/unit/Rules/IsbnTest.php @@ -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)] @@ -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'], diff --git a/tests/unit/Rules/NoWhitespaceTest.php b/tests/unit/Rules/NoWhitespaceTest.php index 751712b1a..c9fdc426f 100644 --- a/tests/unit/Rules/NoWhitespaceTest.php +++ b/tests/unit/Rules/NoWhitespaceTest.php @@ -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)] @@ -37,6 +38,8 @@ public static function providerForInvalidInput(): iterable $rule = new NoWhitespace(); return [ + [$rule, []], + [$rule, new stdClass()], [$rule, ' '], [$rule, 'w poiur'], [$rule, ' '], diff --git a/tests/unit/Rules/PeselTest.php b/tests/unit/Rules/PeselTest.php index 6b9107bcb..fc311a18c 100644 --- a/tests/unit/Rules/PeselTest.php +++ b/tests/unit/Rules/PeselTest.php @@ -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)] @@ -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'], diff --git a/tests/unit/Rules/PhoneTest.php b/tests/unit/Rules/PhoneTest.php index a09b9f586..9de0b1d10 100644 --- a/tests/unit/Rules/PhoneTest.php +++ b/tests/unit/Rules/PhoneTest.php @@ -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)] @@ -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 */ public static function providerForValidInputWithoutCountryCode(): array { @@ -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'], diff --git a/tests/unit/Rules/PolishIdCardTest.php b/tests/unit/Rules/PolishIdCardTest.php index 326f7cb53..8cb3d9b2b 100644 --- a/tests/unit/Rules/PolishIdCardTest.php +++ b/tests/unit/Rules/PolishIdCardTest.php @@ -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)] @@ -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'], diff --git a/tests/unit/Rules/PortugueseNifTest.php b/tests/unit/Rules/PortugueseNifTest.php index 50fdbf2fc..4b5a0ce8e 100644 --- a/tests/unit/Rules/PortugueseNifTest.php +++ b/tests/unit/Rules/PortugueseNifTest.php @@ -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'], diff --git a/tests/unit/Rules/PublicDomainSuffixTest.php b/tests/unit/Rules/PublicDomainSuffixTest.php index ca9150f13..be642c019 100644 --- a/tests/unit/Rules/PublicDomainSuffixTest.php +++ b/tests/unit/Rules/PublicDomainSuffixTest.php @@ -24,6 +24,7 @@ public static function providerForValidInput(): iterable $rule = new PublicDomainSuffix(); return [ + [$rule, ''], [$rule, 'co.uk'], [$rule, 'nom.br'], [$rule, 'WWW.CK'], diff --git a/tests/unit/Rules/StartsWithTest.php b/tests/unit/Rules/StartsWithTest.php index 4030ecafc..8006b8a13 100644 --- a/tests/unit/Rules/StartsWithTest.php +++ b/tests/unit/Rules/StartsWithTest.php @@ -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'], diff --git a/tests/unit/Rules/SubsetTest.php b/tests/unit/Rules/SubsetTest.php index 4da1b3bff..914bea644 100644 --- a/tests/unit/Rules/SubsetTest.php +++ b/tests/unit/Rules/SubsetTest.php @@ -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]], diff --git a/tests/unit/Rules/VersionTest.php b/tests/unit/Rules/VersionTest.php index d93bfa901..83caa1bfc 100644 --- a/tests/unit/Rules/VersionTest.php +++ b/tests/unit/Rules/VersionTest.php @@ -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++'], diff --git a/tests/unit/Rules/VideoUrlTest.php b/tests/unit/Rules/VideoUrlTest.php index ee4a2fa8e..2d1504d1c 100644 --- a/tests/unit/Rules/VideoUrlTest.php +++ b/tests/unit/Rules/VideoUrlTest.php @@ -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 */ public static function providerForValidInput(): iterable { @@ -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'],