From 6e26ba8837154898fd44ee5fffbc4c3fe9aab27f Mon Sep 17 00:00:00 2001 From: Ismael Elias Date: Fri, 8 Jun 2018 21:34:52 -0300 Subject: [PATCH] Apply contribution guidelines to "PrimeNumber" rule Co-Authored-By: Henrique Moody --- library/Exceptions/PrimeNumberException.php | 10 ++- library/Rules/PrimeNumber.php | 18 ++++- tests/integration/rules/primeNumber.phpt | 37 +++++++++ tests/unit/Rules/PrimeNumberTest.php | 90 +++++++++------------ 4 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 tests/integration/rules/primeNumber.phpt diff --git a/library/Exceptions/PrimeNumberException.php b/library/Exceptions/PrimeNumberException.php index b2ba33df..b7e6b8d5 100644 --- a/library/Exceptions/PrimeNumberException.php +++ b/library/Exceptions/PrimeNumberException.php @@ -13,8 +13,16 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class PrimeNumberException extends ValidationException +/** + * @author Henrique Moody + * @author Ismael Elias + * @author Kleber Hamada Sato + */ +final class PrimeNumberException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be a valid prime number', diff --git a/library/Rules/PrimeNumber.php b/library/Rules/PrimeNumber.php index 280344c1..0c4c994b 100644 --- a/library/Rules/PrimeNumber.php +++ b/library/Rules/PrimeNumber.php @@ -13,8 +13,24 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -class PrimeNumber extends AbstractRule +use function ceil; +use function is_numeric; +use function sqrt; + +/** + * Validates whether the input is a prime number. + * + * @author Alexandre Gomes Gaigalas + * @author Camilo Teixeira de Melo + * @author Henrique Moody + * @author Ismael Elias + * @author Kleber Hamada Sato + */ +final class PrimeNumber extends AbstractRule { + /** + * {@inheritdoc} + */ public function validate($input): bool { if (!is_numeric($input) || $input <= 1) { diff --git a/tests/integration/rules/primeNumber.phpt b/tests/integration/rules/primeNumber.phpt new file mode 100644 index 00000000..edfc38b3 --- /dev/null +++ b/tests/integration/rules/primeNumber.phpt @@ -0,0 +1,37 @@ +--FILE-- +check(10); +} catch (PrimeNumberException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::not(v::primeNumber())->check(3); +} catch (PrimeNumberException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::primeNumber()->assert('Foo'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::primeNumber())->assert('+7'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} +?> +--EXPECTF-- +10 must be a valid prime number +3 must not be a valid prime number +- "Foo" must be a valid prime number +- "+7" must not be a valid prime number diff --git a/tests/unit/Rules/PrimeNumberTest.php b/tests/unit/Rules/PrimeNumberTest.php index cf8e9aea..479c24cd 100644 --- a/tests/unit/Rules/PrimeNumberTest.php +++ b/tests/unit/Rules/PrimeNumberTest.php @@ -13,70 +13,58 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use PHPUnit\Framework\TestCase; +use Respect\Validation\Test\RuleTestCase; /** - * @group rule + * @group rule + * * @covers \Respect\Validation\Rules\PrimeNumber - * @covers \Respect\Validation\Exceptions\PrimeNumberException + * + * @author Gabriel Caruso + * @author Henrique Moody + * @author Ismael Elias + * @author Kleber Hamada Sato */ -class PrimeNumberTest extends TestCase +final class PrimeNumberTest extends RuleTestCase { - protected $object; - - protected function setUp(): void + /* + * {@inheritdoc} + */ + public function providerForValidInput(): array { - $this->object = new PrimeNumber(); - } + $rule = new PrimeNumber(); - /** - * @dataProvider providerForPrimeNumber - */ - public function testPrimeNumber($input): void - { - self::assertTrue($this->object->__invoke($input)); - $this->object->check($input); - $this->object->assert($input); - } - - /** - * @dataProvider providerForNotPrimeNumber - * @expectedException \Respect\Validation\Exceptions\PrimeNumberException - */ - public function testNotPrimeNumber($input): void - { - self::assertFalse($this->object->__invoke($input)); - $this->object->assert($input); - } - - public function providerForPrimeNumber() - { return [ - [3], - [5], - [7], - ['3'], - ['5'], - ['+7'], + [$rule, 3], + [$rule, 5], + [$rule, 7], + [$rule, '3'], + [$rule, '5'], + [$rule, '+7'], ]; } - public function providerForNotPrimeNumber() + /* + * {@inheritdoc} + */ + public function providerForInvalidInput(): array { + $rule = new PrimeNumber(); + return [ - [''], - [null], - [0], - [10], - [25], - [36], - [-1], - ['-1'], - ['25'], - ['0'], - ['a'], - [' '], - ['Foo'], + [$rule, ''], + [$rule, null], + [$rule, 0], + [$rule, 10], + [$rule, 25], + [$rule, 36], + [$rule, -1], + [$rule, '-1'], + [$rule, '25'], + [$rule, '0'], + [$rule, 'a'], + [$rule, ' '], + [$rule, 'Foo'], ]; } }