Skip to content

Commit

Permalink
Refactor tests into individual Rule test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 30, 2019
1 parent c037983 commit 0e8083c
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 177 deletions.
21 changes: 21 additions & 0 deletions test/phpunit/DomValidationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Gt\DomValidation\Test;

use DOMDocument;
use DOMElement;
use PHPUnit\Framework\TestCase;

class DomValidationTestCase extends TestCase {
public static function getFormFromHtml(string $html):DOMElement {
$document = new DOMDocument("1.0", "utf-8");
$document->loadHTML($html);

/** @var DOMElement $domElement */
$domElement = $document->getElementsByTagName(
"form"
)->item(
0
);
return $domElement;
}
}
178 changes: 1 addition & 177 deletions test/phpunit/FormValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
<?php
namespace Gt\DomValidation\Test;

use DOMDocument;
use DOMElement;
use DOMNode;
use Gt\DomValidation\Test\Helper\Helper;
use Gt\DomValidation\ValidationException;
use Gt\DomValidation\Validator;
use PHPUnit\Framework\TestCase;

class FormValidatorTest extends TestCase {
private static function getFormFromHtml(string $html):DOMElement {
$document = new DOMDocument("1.0", "utf-8");
$document->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();
Expand All @@ -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);
}
}
}
74 changes: 74 additions & 0 deletions test/phpunit/Rule/PatternTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace Gt\DomValidation\Test\Rule;

use Gt\DomValidation\Test\DomValidationTestCase;
use Gt\DomValidation\Test\Helper\Helper;
use Gt\DomValidation\ValidationException;
use Gt\DomValidation\Validator;

class PatternTest extends DomValidationTestCase {
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");
}
}
}
85 changes: 85 additions & 0 deletions test/phpunit/Rule/RequiredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Gt\DomValidation\Test\Rule;

use Gt\DomValidation\Test\DomValidationTestCase;
use Gt\DomValidation\Test\Helper\Helper;
use Gt\DomValidation\ValidationException;
use Gt\DomValidation\Validator;

class RequiredTest extends DomValidationTestCase {
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 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);
}
}
}
Loading

0 comments on commit 0e8083c

Please sign in to comment.