diff --git a/test/phpunit/DomValidationTestCase.php b/test/phpunit/DomValidationTestCase.php new file mode 100644 index 0000000..24b01a6 --- /dev/null +++ b/test/phpunit/DomValidationTestCase.php @@ -0,0 +1,21 @@ +loadHTML($html); + + /** @var DOMElement $domElement */ + $domElement = $document->getElementsByTagName( + "form" + )->item( + 0 + ); + return $domElement; + } +} \ No newline at end of file diff --git a/test/phpunit/FormValidatorTest.php b/test/phpunit/FormValidatorTest.php index 8fea0bb..40acdc4 100644 --- a/test/phpunit/FormValidatorTest.php +++ b/test/phpunit/FormValidatorTest.php @@ -1,28 +1,11 @@ loadHTML($html); - - /** @var DOMElement $domElement */ - $domElement = $document->getElementsByTagName( - "form" - )->item( - 0 - ); - return $domElement; - } +class FormValidatorTest extends DomValidationTestCase { public function testSimpleValidInput() { $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); $validator = new Validator(); @@ -35,168 +18,9 @@ public function testSimpleValidInput() { "password" => "hunter2", ]); } - catch(ValidationException $exception) {} - - self::assertNull($exception); - } - - public function testSimpleMissingRequiredInput() { - $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); - $validator = new Validator(); - - self::expectException(ValidationException::class); - self::expectExceptionMessage("There is 1 invalid field"); - - $validator->validate($form, [ - "username" => "g105b", - ]); - } - - public function testSimpleMissingBothRequiredInputs() { - $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); - $validator = new Validator(); - - self::expectException(ValidationException::class); - self::expectExceptionMessage("There are 2 invalid fields"); - - $validator->validate($form, ["something" => "nothing"]); - } - - public function testSimpleMissingRequiredInputErrorList() { - $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); - $validator = new Validator(); - - try { - $validator->validate($form, ["username" => "g105b"]); - } - catch(ValidationException $exception) { - foreach($validator->getLastErrorList() as $name => $errors) { - self::assertIsArray($errors); - self::assertContains("This field is required", $errors); - self::assertCount(1, $errors); - } - } - } - - public function testSimpleEmptyRequiredInputErrorList() { - $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); - $validator = new Validator(); - - try { - $validator->validate($form, [ - "username" => "g105b", - "password" => "" - ]); - } - catch(ValidationException $exception) { - foreach($validator->getLastErrorList() as $name => $errors) { - self::assertIsArray($errors); - self::assertContains("This field is required", $errors); - self::assertCount(1, $errors); - self::assertEquals("password", $name); - } - } - } - - public function testPattern() { - $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD); - $validator = new Validator(); - - $exception = null; - - try { - $validator->validate($form, [ - "name" => "Jeff Bezos", - "credit-card" => "4921166184521652", - "expiry-year" => 20, - "expiry-month" => 12, - ]); - } - catch(ValidationException $exception) {} - - self::assertNull($exception); - } - - public function testPatternInvalid() { - $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD); - $validator = new Validator(); - - $exception = null; - - try { - $validator->validate($form, [ - "name" => "Jeff Bezos", - "credit-card" => "492116611652", // not enough numbers - ]); - } catch(ValidationException $exception) { - $errorArray = iterator_to_array($validator->getLastErrorList()); - self::assertCount(1, $errorArray); - $creditCardErrorArray = $errorArray["credit-card"]; - self::assertCount(1, $creditCardErrorArray); - self::assertEquals( - "This field does not match the required pattern", - $creditCardErrorArray[0] - ); } - } - - public function testPatternWithMissingRequiredFields() { - $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD_ALL_REQUIRED_FIELDS); - $validator = new Validator(); - - try { - $validator->validate($form, [ - "name" => "Jeff Bezos", - "credit-card" => "4921166184521652", - ]); - } - catch(ValidationException $exception) { - $errorArray = iterator_to_array($validator->getLastErrorList()); - self::assertCount(2, $errorArray); - $expiryMonthErrorArray = $errorArray["expiry-month"]; - $expiryYearErrorArray = $errorArray["expiry-year"]; - self::assertCount(1, $expiryMonthErrorArray); - self::assertCount(1, $expiryYearErrorArray); - self::assertEquals($expiryMonthErrorArray[0], "This field is required"); - self::assertEquals($expiryYearErrorArray[0], "This field is required"); - } - } - - public function testNumberFieldEmpty() { - $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD); - $validator = new Validator(); - - $exception = null; - - try { - $validator->validate($form, [ - "name" => "Jeff Bezos", - "credit-card" => "4921166184521652", - "expiry-month" => "", - "expiry-year" => "", - ]); - } - catch(ValidationException $exception) {} self::assertNull($exception); } - - public function testNumberFieldRequiredButEmpty() { - $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD_ALL_REQUIRED_FIELDS); - $validator = new Validator(); - - try { - $validator->validate($form, [ - "name" => "Jeff Bezos", - "credit-card" => "4921166184521652", - "expiry-month" => "", - "expiry-year" => "", - ]); - } - catch(ValidationException $exception) { - $errorArray = iterator_to_array($validator->getLastErrorList()); - self::assertCount(2, $errorArray); - } - } } \ No newline at end of file diff --git a/test/phpunit/Rule/PatternTest.php b/test/phpunit/Rule/PatternTest.php new file mode 100644 index 0000000..8c749ef --- /dev/null +++ b/test/phpunit/Rule/PatternTest.php @@ -0,0 +1,74 @@ +validate($form, [ + "name" => "Jeff Bezos", + "credit-card" => "4921166184521652", + "expiry-year" => 20, + "expiry-month" => 12, + ]); + } + catch(ValidationException $exception) {} + + self::assertNull($exception); + } + + public function testPatternInvalid() { + $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD); + $validator = new Validator(); + + $exception = null; + + try { + $validator->validate($form, [ + "name" => "Jeff Bezos", + "credit-card" => "492116611652", // not enough numbers + ]); + } + catch(ValidationException $exception) { + $errorArray = iterator_to_array($validator->getLastErrorList()); + self::assertCount(1, $errorArray); + $creditCardErrorArray = $errorArray["credit-card"]; + self::assertCount(1, $creditCardErrorArray); + self::assertEquals( + "This field does not match the required pattern", + $creditCardErrorArray[0] + ); + } + } + + public function testPatternWithMissingRequiredFields() { + $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD_ALL_REQUIRED_FIELDS); + $validator = new Validator(); + + try { + $validator->validate($form, [ + "name" => "Jeff Bezos", + "credit-card" => "4921166184521652", + ]); + } + catch(ValidationException $exception) { + $errorArray = iterator_to_array($validator->getLastErrorList()); + self::assertCount(2, $errorArray); + $expiryMonthErrorArray = $errorArray["expiry-month"]; + $expiryYearErrorArray = $errorArray["expiry-year"]; + self::assertCount(1, $expiryMonthErrorArray); + self::assertCount(1, $expiryYearErrorArray); + self::assertEquals($expiryMonthErrorArray[0], "This field is required"); + self::assertEquals($expiryYearErrorArray[0], "This field is required"); + } + } +} \ No newline at end of file diff --git a/test/phpunit/Rule/RequiredTest.php b/test/phpunit/Rule/RequiredTest.php new file mode 100644 index 0000000..56613e6 --- /dev/null +++ b/test/phpunit/Rule/RequiredTest.php @@ -0,0 +1,85 @@ +validate($form, [ + "username" => "g105b", + ]); + } + + public function testSimpleMissingBothRequiredInputs() { + $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); + $validator = new Validator(); + + self::expectException(ValidationException::class); + self::expectExceptionMessage("There are 2 invalid fields"); + + $validator->validate($form, ["something" => "nothing"]); + } + + public function testSimpleMissingRequiredInputErrorList() { + $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); + $validator = new Validator(); + + try { + $validator->validate($form, ["username" => "g105b"]); + } + catch(ValidationException $exception) { + foreach($validator->getLastErrorList() as $name => $errors) { + self::assertIsArray($errors); + self::assertContains("This field is required", $errors); + self::assertCount(1, $errors); + } + } + } + + public function testSimpleEmptyRequiredInputErrorList() { + $form = self::getFormFromHtml(Helper::HTML_USERNAME_PASSWORD); + $validator = new Validator(); + + try { + $validator->validate($form, [ + "username" => "g105b", + "password" => "" + ]); + } + catch(ValidationException $exception) { + foreach($validator->getLastErrorList() as $name => $errors) { + self::assertIsArray($errors); + self::assertContains("This field is required", $errors); + self::assertCount(1, $errors); + self::assertEquals("password", $name); + } + } + } + + public function testNumberFieldRequiredButEmpty() { + $form = self::getFormFromHtml(Helper::HTML_PATTERN_CREDIT_CARD_ALL_REQUIRED_FIELDS); + $validator = new Validator(); + + try { + $validator->validate($form, [ + "name" => "Jeff Bezos", + "credit-card" => "4921166184521652", + "expiry-month" => "", + "expiry-year" => "", + ]); + } + catch(ValidationException $exception) { + $errorArray = iterator_to_array($validator->getLastErrorList()); + self::assertCount(2, $errorArray); + } + } +} \ No newline at end of file diff --git a/test/phpunit/Rule/TypeNumberTest.php b/test/phpunit/Rule/TypeNumberTest.php new file mode 100644 index 0000000..ddacd93 --- /dev/null +++ b/test/phpunit/Rule/TypeNumberTest.php @@ -0,0 +1,28 @@ +validate($form, [ + "name" => "Jeff Bezos", + "credit-card" => "4921166184521652", + "expiry-month" => "", + "expiry-year" => "", + ]); + } + catch(ValidationException $exception) {} + + self::assertNull($exception); + } +} \ No newline at end of file