From 719f12a424b34c171b1afee4e576e1da6aadae82 Mon Sep 17 00:00:00 2001 From: Danilo Correa Date: Sun, 21 Apr 2024 13:57:44 -0300 Subject: [PATCH] Increase code coverage of some rules We were not thoroughly testing quite a few rules, especially the constructor of some of them. This commit increases the code coverage, ensuring almost every single line in the "Rules" namespace is covered. --- tests/unit/Rules/Base64Test.php | 5 +++++ tests/unit/Rules/BaseTest.php | 11 +++++++++++ tests/unit/Rules/BsnTest.php | 4 ++++ tests/unit/Rules/ContainsTest.php | 8 ++++++++ tests/unit/Rules/ExecutableTest.php | 4 ++++ tests/unit/Rules/FilterVarTest.php | 2 ++ tests/unit/Rules/InTest.php | 1 + tests/unit/Rules/IpTest.php | 1 + tests/unit/Rules/IsbnTest.php | 4 ++++ tests/unit/Rules/NoWhitespaceTest.php | 3 +++ tests/unit/Rules/PeselTest.php | 4 ++++ tests/unit/Rules/PhoneTest.php | 13 +++++++++++++ tests/unit/Rules/PolishIdCardTest.php | 5 +++++ tests/unit/Rules/PortugueseNifTest.php | 3 +++ tests/unit/Rules/PublicDomainSuffixTest.php | 1 + tests/unit/Rules/StartsWithTest.php | 2 ++ tests/unit/Rules/SubsetTest.php | 1 + tests/unit/Rules/VersionTest.php | 2 ++ tests/unit/Rules/VideoUrlTest.php | 12 ++++++++++++ 19 files changed, 86 insertions(+) diff --git a/tests/unit/Rules/Base64Test.php b/tests/unit/Rules/Base64Test.php index 362c6049..cae654d8 100644 --- a/tests/unit/Rules/Base64Test.php +++ b/tests/unit/Rules/Base64Test.php @@ -55,6 +55,11 @@ final class Base64Test extends RuleTestCase $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 d8c744f5..dc064b88 100644 --- a/tests/unit/Rules/BaseTest.php +++ b/tests/unit/Rules/BaseTest.php @@ -11,12 +11,23 @@ namespace Respect\Validation\Rules; 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); + } + /** @return iterable */ public static function providerForValidInput(): iterable { diff --git a/tests/unit/Rules/BsnTest.php b/tests/unit/Rules/BsnTest.php index 4dfbd066..da1e47ab 100644 --- a/tests/unit/Rules/BsnTest.php +++ b/tests/unit/Rules/BsnTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class BsnTest extends RuleTestCase $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 591e06ce..62b6960a 100644 --- a/tests/unit/Rules/ContainsTest.php +++ b/tests/unit/Rules/ContainsTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class ContainsTest extends RuleTestCase 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 a19db036..4a9ecd1f 100644 --- a/tests/unit/Rules/ExecutableTest.php +++ b/tests/unit/Rules/ExecutableTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\Attributes\Group; use Respect\Validation\Test\RuleTestCase; use SplFileInfo; use SplFileObject; +use stdClass; #[Group('rule')] #[CoversClass(Executable::class)] @@ -37,6 +38,9 @@ final class ExecutableTest extends RuleTestCase $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 e2393b04..1787d002 100644 --- a/tests/unit/Rules/FilterVarTest.php +++ b/tests/unit/Rules/FilterVarTest.php @@ -60,6 +60,8 @@ final class FilterVarTest extends RuleTestCase [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 4e6f4079..1a4564a8 100644 --- a/tests/unit/Rules/InTest.php +++ b/tests/unit/Rules/InTest.php @@ -38,6 +38,7 @@ final class InTest extends RuleTestCase 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 8aff10d9..38ff30fc 100644 --- a/tests/unit/Rules/IpTest.php +++ b/tests/unit/Rules/IpTest.php @@ -42,6 +42,7 @@ final class IpTest extends RuleTestCase 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 a9b0579f..5019bfb9 100644 --- a/tests/unit/Rules/IsbnTest.php +++ b/tests/unit/Rules/IsbnTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class IsbnTest extends RuleTestCase $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 751712b1..c9fdc426 100644 --- a/tests/unit/Rules/NoWhitespaceTest.php +++ b/tests/unit/Rules/NoWhitespaceTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class NoWhitespaceTest extends RuleTestCase $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 6b9107bc..fc311a18 100644 --- a/tests/unit/Rules/PeselTest.php +++ b/tests/unit/Rules/PeselTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class PeselTest extends RuleTestCase $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 a09b9f58..e2d5ca29 100644 --- a/tests/unit/Rules/PhoneTest.php +++ b/tests/unit/Rules/PhoneTest.php @@ -13,7 +13,9 @@ use PHPUnit\Framework\Attributes\CoversClass; 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 @@ final class PhoneTest extends TestCase self::assertInvalidInput(new Phone($countryCode), $input); } + #[Test] + public function itShouldThrowsExceptionWhenCountryCodeIsNotValid(): void + { + $this->expectException(InvalidRuleConstructorException::class); + $this->expectExceptionMessage('Invalid country code BRR'); + + new Phone('BRR'); + } + /** @return array */ public static function providerForValidInputWithoutCountryCode(): array { @@ -67,6 +78,8 @@ final class PhoneTest extends TestCase 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 326f7cb5..8cb3d9b2 100644 --- a/tests/unit/Rules/PolishIdCardTest.php +++ b/tests/unit/Rules/PolishIdCardTest.php @@ -12,6 +12,7 @@ namespace Respect\Validation\Rules; 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 @@ final class PolishIdCardTest extends RuleTestCase $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 50fdbf2f..4b5a0ce8 100644 --- a/tests/unit/Rules/PortugueseNifTest.php +++ b/tests/unit/Rules/PortugueseNifTest.php @@ -55,6 +55,9 @@ final class PortugueseNifTest extends RuleTestCase 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 ca9150f1..be642c01 100644 --- a/tests/unit/Rules/PublicDomainSuffixTest.php +++ b/tests/unit/Rules/PublicDomainSuffixTest.php @@ -24,6 +24,7 @@ final class PublicDomainSuffixTest extends RuleTestCase $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 4030ecaf..8006b8a1 100644 --- a/tests/unit/Rules/StartsWithTest.php +++ b/tests/unit/Rules/StartsWithTest.php @@ -34,6 +34,8 @@ final class StartsWithTest extends RuleTestCase 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 4da1b3bf..914bea64 100644 --- a/tests/unit/Rules/SubsetTest.php +++ b/tests/unit/Rules/SubsetTest.php @@ -35,6 +35,7 @@ final class SubsetTest extends RuleTestCase 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 d93bfa90..83caa1bf 100644 --- a/tests/unit/Rules/VersionTest.php +++ b/tests/unit/Rules/VersionTest.php @@ -41,6 +41,8 @@ final class VersionTest extends RuleTestCase $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 ee4a2fa8..0840521d 100644 --- a/tests/unit/Rules/VideoUrlTest.php +++ b/tests/unit/Rules/VideoUrlTest.php @@ -11,12 +11,23 @@ namespace Respect\Validation\Rules; 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'); + } + /** @return iterable */ public static function providerForValidInput(): iterable { @@ -40,6 +51,7 @@ final class VideoUrlTest extends RuleTestCase 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'],