-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests into individual Rule test cases
- Loading branch information
Showing
5 changed files
with
209 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.