respect-validation/tests/Exceptions/CheckExceptionsTest.php
Andy Wendt ab65035181 Issue #260: Now using Respect\Validation exceptions only
* `ExceptionInterface`: all Respect\Validation\Exceptions implement through inheritance
* `ValidatorExceptionInterface`: implemented only by AllOfException.  This allows the end users to know when there has been a Validator exception rather than just any of our exceptions.

Fixed formatting issues

Created two new exception types

* Created ValidationExceptionInterface
* Created NestedValidationExceptionInterface which extends ValidationExceptionInterface
    * Renamed from ValidatorExceptionInterface

* ValidationException implements ValidationExceptionInterface and ValidationExceptionTest checks for the implementation.
* AbstractNestedException implements NestedValidationExceptionInterface and AbstractNestedExceptionTest checks for the implementation.

* CheckExceptionsTest now checks all Rule exceptions to make sure they implement ValidationExceptionInterface
* ValidatorTest now contains test that shows that only ValidationExceptionInterface can be used reliably with `::check()`

* Updated documentation for new exception types
* Reworked examples to show how to catch the exception interfaces

Minor changes in readme.md and ExceptionInterfaces

* Removed `import` statements (hahaha)
* Renamed `$e` to `$exception`

* `ValidationExceptionInterface` now extends `ExceptionInterface`.  Changed `ValidationException` to match
2015-01-31 07:59:59 -07:00

98 lines
2.8 KiB
PHP

<?php
namespace Respect\Validation\Exceptions;
use ReflectionClass;
use DirectoryIterator;
class CheckExceptionsTest extends \PHPUnit_Framework_TestCase
{
protected $deprecateds = array(
'Consonants',
'Digits',
'Vowels',
);
private function getAllRuleNames()
{
$rulesDirectory = __DIR__.'/../../library/Rules';
$rulesDirectoryIterator = new DirectoryIterator($rulesDirectory);
$ruleNames = array();
foreach ($rulesDirectoryIterator as $fileInfo) {
if ($fileInfo->isDir()) {
continue;
}
$ruleName = substr($fileInfo->getBasename(), 0, -4);
if (in_array($ruleName, $this->deprecateds)) {
continue;
}
$className = 'Respect\\Validation\\Rules\\'.$ruleName;
$reflectionClass = new ReflectionClass($className);
if ($reflectionClass->isAbstract()
|| $reflectionClass->isInterface()
|| ! $reflectionClass->implementsInterface('Respect\\Validation\\Validatable')) {
continue;
}
$ruleNames[] = $ruleName;
}
return $ruleNames;
}
public function testEveryRuleMustBeItsOwnException()
{
$missingExceptions = array();
foreach ($this->getAllRuleNames() as $ruleName) {
$exceptionClass = $this->buildExceptionClass($ruleName);
if (class_exists($exceptionClass)) {
continue;
}
$missingExceptions[] = $ruleName;
}
$this->assertEmpty($missingExceptions, 'No exceptions for: ' . $this->formatArrayAsString($missingExceptions));
}
public function testEveryRuleExceptionImplementsValidationExceptionInterface()
{
$exceptionsNotImplementingInterface = array();
foreach ($this->getAllRuleNames() as $ruleName) {
$exceptionClass = $this->buildExceptionClass($ruleName);
$exceptionClassMock = $this->getMock($exceptionClass);
if ($exceptionClassMock instanceof ValidationExceptionInterface) {
continue;
}
$exceptionsNotImplementingInterface[] = $ruleName;
}
$this->assertEmpty($exceptionsNotImplementingInterface,
'ValidationExceptionInterface not implemented in: ' .
$this->formatArrayAsString($exceptionsNotImplementingInterface));
}
/**
* @param string $ruleName
* @return string
*/
private function buildExceptionClass($ruleName)
{
$exceptionClass = 'Respect\\Validation\\Exceptions\\' . $ruleName . 'Exception';
return $exceptionClass;
}
/**
* @param array $array
* @return string
*/
private function formatArrayAsString(array $array)
{
return implode(', ', $array);
}
}