Add "respect/stringifier" as dependency

Due to this change it was possible to identify that NotEmptyException,
and NotOptionalException where not working as they should. A fix was
made along with this commit.
This commit is contained in:
Henrique Moody 2018-01-01 14:59:31 +01:00
parent ef975629f3
commit 2d7e2ea48f
No known key found for this signature in database
GPG key ID: 221E9281655813A6
42 changed files with 69 additions and 261 deletions

View file

@ -13,6 +13,7 @@
],
"require": {
"php": ">=7.1",
"respect/stringifier": "^0.2.0",
"symfony/polyfill-mbstring": "^1.2"
},
"require-dev": {

View file

@ -31,6 +31,6 @@ class NotBlankException extends ValidationException
public function chooseTemplate()
{
return '' == $this->getName() ? static::STANDARD : static::NAMED;
return $this->hasName() ? static::NAMED : static::STANDARD;
}
}

View file

@ -30,6 +30,6 @@ class NotEmptyException extends ValidationException
public function chooseTemplate()
{
return '' == $this->getName() ? static::STANDARD : static::NAMED;
return $this->hasName() ? static::NAMED : static::STANDARD;
}
}

View file

@ -31,6 +31,6 @@ class NotOptionalException extends ValidationException
public function chooseTemplate()
{
return '' == $this->getName() ? static::STANDARD : static::NAMED;
return $this->hasName() ? static::NAMED : static::STANDARD;
}
}

View file

@ -31,6 +31,6 @@ class OptionalException extends ValidationException
public function chooseTemplate()
{
return '' == $this->getName() ? static::STANDARD : static::NAMED;
return $this->hasName() ? static::NAMED : static::STANDARD;
}
}

View file

@ -13,10 +13,10 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use DateTime;
use Exception;
use function in_array;
use function is_numeric;
use function Respect\Stringifier\stringify;
use InvalidArgumentException;
use Traversable;
class ValidationException extends InvalidArgumentException implements ExceptionInterface
{
@ -32,21 +32,6 @@ class ValidationException extends InvalidArgumentException implements ExceptionI
],
];
/**
* @var int
*/
private static $maxDepthStringify = 5;
/**
* @var int
*/
private static $maxCountStringify = 10;
/**
* @var string
*/
private static $maxReplacementStringify = '...';
protected $id = 'validation';
protected $mode = self::MODE_DEFAULT;
protected $name = '';
@ -76,118 +61,12 @@ class ValidationException extends InvalidArgumentException implements ExceptionI
/**
* @param mixed $value
* @param int $depth
*
* @return string
*/
public static function stringify($value, $depth = 1)
public static function stringify($value)
{
if ($depth >= self::$maxDepthStringify) {
return self::$maxReplacementStringify;
}
if (is_array($value)) {
return static::stringifyArray($value, $depth);
}
if (is_object($value)) {
return static::stringifyObject($value, $depth);
}
if (is_resource($value)) {
return sprintf('`[resource] (%s)`', get_resource_type($value));
}
if (is_float($value)) {
if (is_infinite($value)) {
return ($value > 0 ? '' : '-').'INF';
}
if (is_nan($value)) {
return 'NaN';
}
}
return @json_encode($value, (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) ?: $value;
}
/**
* @param array $value
* @param int $depth
*
* @return string
*/
public static function stringifyArray(array $value, $depth = 1)
{
$nextDepth = ($depth + 1);
if ($nextDepth >= self::$maxDepthStringify) {
return self::$maxReplacementStringify;
}
if (empty($value)) {
return '{ }';
}
$total = count($value);
$string = '';
$current = 0;
foreach ($value as $childKey => $childValue) {
if ($current++ >= self::$maxCountStringify) {
$string .= self::$maxReplacementStringify;
break;
}
if (!is_int($childKey)) {
$string .= sprintf('%s: ', static::stringify($childKey, $nextDepth));
}
$string .= static::stringify($childValue, $nextDepth);
if ($current !== $total) {
$string .= ', ';
}
}
return sprintf('{ %s }', $string);
}
/**
* @param mixed $value
* @param int $depth
*
* @return string
*/
public static function stringifyObject($value, $depth = 2)
{
$nextDepth = $depth + 1;
if ($value instanceof DateTime) {
return sprintf('"%s"', $value->format('Y-m-d H:i:s'));
}
$class = get_class($value);
if ($value instanceof Traversable) {
return sprintf('`[traversable] (%s: %s)`', $class, static::stringify(iterator_to_array($value), $nextDepth));
}
if ($value instanceof Exception) {
$properties = [
'message' => $value->getMessage(),
'code' => $value->getCode(),
'file' => $value->getFile().':'.$value->getLine(),
];
return sprintf('`[exception] (%s: %s)`', $class, static::stringify($properties, $nextDepth));
}
if (method_exists($value, '__toString')) {
return static::stringify($value->__toString(), $nextDepth);
}
$properties = static::stringify(get_object_vars($value), $nextDepth);
return sprintf('`[object] (%s: %s)`', $class, str_replace('`', '', $properties));
return stringify($value);
}
public function __toString()
@ -214,6 +93,16 @@ class ValidationException extends InvalidArgumentException implements ExceptionI
return $this->name;
}
protected function hasName(): bool
{
$name = $this->getName();
if (is_numeric($name)) {
return (bool) (float) $name;
}
return !in_array($name, ['`FALSE`', '`NULL`', '`{ }`', '""', '']);
}
public function getId()
{
return $this->id;

View file

@ -16,6 +16,9 @@ namespace Respect\Validation\Rules;
use DateTime as DateTimeMutable;
use Respect\Validation\Exceptions\ComponentException;
/**
* @todo Do not extend AllOf
*/
class Age extends AllOf
{
public $minAge;

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
null must be a BSN
`NULL` must be a BSN

View file

@ -14,4 +14,4 @@ try {
?>
--EXPECTF--
"17 years ago" must be less than or equal to "%d-%d-%d %d:%d:%d"
"17 years ago" must be less than or equal to `[date-time] (DateTime: "%s")`

View file

@ -14,4 +14,4 @@ try {
?>
--EXPECTF--
- "17 years ago" must be less than or equal to "%d-%d-%d %d:%d:%d"
- "17 years ago" must be less than or equal to `[date-time] (DateTime: "%s")`

View file

@ -14,4 +14,4 @@ try {
?>
--EXPECTF--
- "9 years ago" must be less than or equal to "%d-%d-%d %d:%d:%d"
- "9 years ago" must be less than or equal to `[date-time] (DateTime: "%s")`

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
{ } must not be of the type array
`{ }` must not be of the type array

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
- { 1, 2, 3 } must not be of the type array
- `{ 1, 2, 3 }` must not be of the type array

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
{ 42 } must not be an array
`{ 42 }` must not be an array

View file

@ -1,5 +1,5 @@
--FILE--
<?php
<?php
require 'vendor/autoload.php';
@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
true must not be a boolean
`TRUE` must not be a boolean

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
- true must not be a boolean
- `TRUE` must not be a boolean

View file

@ -37,8 +37,8 @@ try {
?>
--EXPECTF--
{ } must be a callable
`{ }` must be a callable
"oneInexistentFunction" must be a callable
100 must be a callable
null must be a callable
"" must be a callable
`NULL` must be a callable
"" must be a callable

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
{ } must not be countable
`{ }` must not be countable

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"497511659092062" must be a valid IMEI
- { } must be a valid IMEI
- `{ }` must be a valid IMEI

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
{ 2, 3 } must not be iterable
`{ 2, 3 }` must not be iterable

View file

@ -15,5 +15,6 @@ try {
} catch (AllOfException $e) {
echo $e->getMainMessage();
}
?>
--EXPECTF--
All of the required rules must pass for { "password": "shazam", "password_confirmation": "batman" }
All of the required rules must pass for `{ "password": "shazam", "password_confirmation": "batman" }`

View file

@ -14,5 +14,6 @@ try {
} catch (Exception $e) {
echo $e->getMainMessage();
}
?>
--EXPECTF--
Key { "password": "123", "invalid_passwords": { "123", "secreta" } } must not be present
Key `{ "password": "123", "invalid_passwords": { "123", "secreta" } }` must not be present

View file

@ -15,5 +15,6 @@ try {
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- Key { "password": "123", "invalid_passwords": { "123", "secreta" } } must not be present
- Key `{ "password": "123", "invalid_passwords": { "123", "secreta" } }` must not be present

View file

@ -19,5 +19,5 @@ try {
}
?>
--EXPECTF--
null must not be blank
- "" must not be blank
The value must not be blank
- The value must not be blank

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must be blank
- { 1 } must be blank
- `{ 1 }` must be blank

View file

@ -19,5 +19,5 @@ try {
}
?>
--EXPECTF--
null must not be empty
- "" must not be empty
The value must not be empty
- The value must not be empty

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must be empty
- { 1 } must be empty
- `{ 1 }` must be empty

View file

@ -19,5 +19,5 @@ try {
}
?>
--EXPECTF--
null must not be optional
- "" must not be optional
The value must not be optional
- The value must not be optional

View file

@ -26,5 +26,5 @@ try {
?>
--EXPECTF--
The value must be optional
- { } must be optional
- The value must be optional
- Field must be optional

View file

@ -19,5 +19,5 @@ try {
}
?>
--EXPECTF--
null must not be null
- null must not be null
`NULL` must not be null
- `NULL` must not be null

View file

@ -14,4 +14,4 @@ try {
?>
--EXPECTF--
NaN must be a number
`NaN` must be a number

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
- NaN must be a number
- `NaN` must be a number

View file

@ -25,5 +25,5 @@ try {
?>
--EXPECTF--
"" must be an object
true must be an object
`TRUE` must be an object
0 must be an object

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
null must not be optional
The value must not be optional

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
{ 1, 2, 2, 3 } must not contain duplicates
`{ 1, 2, 2, 3 }` must not contain duplicates

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
{ 1, 2, 3, 1 } must not contain duplicates
`{ 1, 2, 3, 1 }` must not contain duplicates

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
- null is not considered as "Yes"
- `NULL` is not considered as "Yes"

View file

@ -13,12 +13,7 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
use ArrayIterator;
use DateTime;
use Exception;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
use stdClass;
class ValidationExceptionTest extends TestCase
{
@ -39,17 +34,6 @@ class ValidationExceptionTest extends TestCase
);
}
/**
* @dataProvider providerForStringify
*/
public function testStringifyShouldConvertStringsProperly($input, $result): void
{
self::assertStringMatchesFormat(
$result,
ValidationException::stringify($input)
);
}
public function testGetMainMessageShouldApplyTemplatePlaceholders(): void
{
$sampleValidationException = new ValidationException();
@ -69,78 +53,6 @@ class ValidationExceptionTest extends TestCase
self::assertEquals('foo', $x->getTemplate());
}
public function providerForStringify()
{
$object1 = new SplFileInfo('stringify.phpt'); // __toString()
$object2 = new DateTime('1988-09-09 23:59:59');
$object3 = new stdClass();
$object4 = new stdClass();
$object4->foo = 1;
$object4->bar = false;
$object5 = new stdClass();
$objectRecursive = $object5;
for ($i = 0; $i < 10; ++$i) {
$objectRecursive->name = new stdClass();
$objectRecursive = $objectRecursive->name;
}
$exception = new Exception('My message');
$iterator1 = new ArrayIterator([1, 2, 3]);
$iterator2 = new ArrayIterator(['a' => 1, 'b' => 2, 'c' => 3]);
return [
['', '""'],
['foo', '"foo"'],
[INF, 'INF'],
[-INF, '-INF'],
[acos(4), 'NaN'],
[123, '123'],
[123.456, '123.456'],
[[], '{ }'],
[[false], '{ false }'],
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], '{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }'],
[range(1, 80), '{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... }'],
[
['foo' => true, 'bar' => ['baz' => 123, 'qux' => [1, 2, 3]]],
'{ "foo": true, "bar": { "baz": 123, "qux": { 1, 2, 3 } } }',
],
[
['foo' => true, 'bar' => ['baz' => 123, 'qux' => ['norf' => [1, 2, 3]]]],
'{ "foo": true, "bar": { "baz": 123, "qux": { "norf": ... } } }',
],
[[[], 'foo'], '{ { }, "foo" }'],
[[[1], 'foo'], '{ { 1 }, "foo" }'],
[[1, [2, [3]]], '{ 1, { 2, { 3 } } }'],
[[1, [2, [3, [4]]]], '{ 1, { 2, { 3, ... } } }'],
[[1, [2, [3, [4, [5]]]]], '{ 1, { 2, { 3, ... } } }'],
[['foo', 'bar'], '{ "foo", "bar" }'],
[['foo', -1], '{ "foo", -1 }'],
[$object1, '"stringify.phpt"'],
[$object2, sprintf('"%s"', $object2->format('Y-m-d H:i:s'))],
[$object3, '`[object] (stdClass: { })`'],
[$object4, '`[object] (stdClass: { "foo": 1, "bar": false })`'],
[$object5, '`[object] (stdClass: { "name": [object] (stdClass: ...) })`'],
[
$exception,
'`[exception] (Exception: { "message": "My message", "code": 0, "file": "%s:%d" })`',
],
[$iterator1, '`[traversable] (ArrayIterator: { 1, 2, 3 })`'],
[$iterator2, '`[traversable] (ArrayIterator: { "a": 1, "b": 2, "c": 3 })`'],
[stream_context_create(), '`[resource] (stream-context)`'],
[tmpfile(), '`[resource] (stream)`'],
[xml_parser_create(), '`[resource] (xml)`'],
[
[$object4, [42, 43], true, null, tmpfile()],
'{ `[object] (stdClass: { "foo": 1, "bar": false })`, { 42, 43 }, true, null, `[resource] (stream)` }',
],
];
}
public function providerForFormat()
{
return [

View file

@ -47,7 +47,7 @@ class FiniteTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\FiniteException
* @expectedExceptionMessage INF must be a finite number
* @expectedExceptionMessage `INF` must be a finite number
*/
public function testShouldThrowFiniteExceptionWhenChecking(): void
{

View file

@ -46,7 +46,7 @@ class InTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\InException
* @expectedExceptionMessage "x" must be in { "foo", "bar" }
* @expectedExceptionMessage "x" must be in `{ "foo", "bar" }`
*/
public function testInCheckExceptionMessageWithArray(): void
{

View file

@ -147,7 +147,7 @@ class KeySetTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\KeySetException
* @expectedExceptionMessage Must have keys { "foo", "bar" }
* @expectedExceptionMessage Must have keys `{ "foo", "bar" }`
*/
public function testShouldCheckKeys(): void
{
@ -162,7 +162,7 @@ class KeySetTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\KeySetException
* @expectedExceptionMessage Must have keys { "foo", "bar" }
* @expectedExceptionMessage Must have keys `{ "foo", "bar" }`
*/
public function testShouldAssertKeys(): void
{
@ -177,7 +177,7 @@ class KeySetTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\KeySetException
* @expectedExceptionMessage Must have keys { "name" }
* @expectedExceptionMessage Must have keys `{ "name" }`
* @dataProvider providerForInvalidArguments
*/
public function testShouldThrowExceptionInCaseArgumentIsAnythingOtherThanArray($input): void

View file

@ -47,7 +47,7 @@ class ScalarValTest extends TestCase
/**
* @expectedException \Respect\Validation\Exceptions\ScalarValException
* @expectedExceptionMessage null must be a scalar value
* @expectedExceptionMessage `NULL` must be a scalar value
*/
public function testShouldThrowScalarExceptionWhenChecking(): void
{