respect-validation/tests/unit/Message/StandardFormatter/ArrayProvider.php
Henrique Moody 238f2d506a
Update validation engine
There are a few "problems" with the current engine:

- Allowing each rule to execute assert() and check() means duplication
  in some cases.

- Because we use exceptions to assert/check, we can only invert a
  validation (with Not) if there are errors. That means that we have
  limited granularity control.

- There is a lot of logic in the exceptions. That means that even after
  it throws an exception, something could still happen. We're stable on
  that front, but I want to simplify them. Besides, debugging exception
  code is painful because the stack trace does not go beyond the
  exception.

Apart from that, there are many limitations with templating, and working
that out in the current implementation makes it much harder.

These changes will improve the library in many aspects, but they will
also change the behavior and break backward compatibility. However,
that's a price I'm willing to pay for the improvements we'll have.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-22 16:54:44 +01:00

212 lines
7.6 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Message\StandardFormatter;
use Respect\Validation\Result;
use Respect\Validation\Test\Builders\ResultBuilder;
trait ArrayProvider
{
use ResultCreator;
/** @return array<string, array{0: Result, 1: array<string, mixed>, 2?: array<string, mixed>}> */
public static function provideForArray(): array
{
return [
'without children, without templates' => [
(new ResultBuilder())->id('only')->template('__parent_original__')->build(),
['only' => '__parent_original__'],
],
'without children, with templates' => [
(new ResultBuilder())->id('only')->build(),
['only' => 'Custom template'],
['only' => 'Custom template'],
],
'with single-level children, without templates' => [
self::singleLevelChildrenMessage(),
[
'1st' => '__1st_original__',
'2nd' => '__2nd_original__',
'3rd' => '__3rd_original__',
],
],
'with single-level children, with templates' => [
self::singleLevelChildrenMessage(),
[
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
[
'__self__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
],
'with single-level children, with partial templates' => [
self::singleLevelChildrenMessage(),
[
'1st' => '1st custom',
'2nd' => '__2nd_original__',
'3rd' => '3rd custom',
],
[
'1st' => '1st custom',
'3rd' => '3rd custom',
],
],
'with single-level children, with overwritten template' => [
self::singleLevelChildrenMessage(),
['parent' => 'Parent custom'],
['parent' => 'Parent custom'],
],
'with single-nested child, without templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'1st' => '__1st_original__',
'2nd' => '__2nd_1st_original__',
'3rd' => '__3rd_original__',
],
],
'with single-nested child, with templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'1st' => '1st custom',
'2nd' => '2nd > 1st custom',
'3rd' => '3rd custom',
],
[
'__self__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_1st' => '2nd > 1st custom',
],
'3rd' => '3rd custom',
],
],
'with single-nested child, with partial templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'1st' => '1st custom',
'2nd' => '__2nd_1st_original__',
'3rd' => '3rd custom',
],
[
'__self__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_2nd' => '2nd > 2nd not shown',
],
'3rd' => '3rd custom',
],
],
'with single-nested child, with overwritten templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
[
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
],
'with multi-nested children, without templates' => [
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
[
'1st' => '__1st_original__',
'2nd' => [
'2nd_1st' => '__2nd_1st_original__',
'2nd_2nd' => '__2nd_2nd_original__',
],
'3rd' => '__3rd_original__',
],
],
'with multi-nested children, with templates' => [
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
[
'1st' => '1st custom',
'2nd' => [
'2nd_1st' => '2nd > 1st custom',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
[
'1st' => '1st custom',
'2nd' => [
'2nd_1st' => '2nd > 1st custom',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
],
'with multi-nested children, with partial templates' => [
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
[
'1st' => '1st custom',
'2nd' => [
'2nd_1st' => '__2nd_1st_original__',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
[
'parent' => [
'__self__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
],
],
'with children with the same id, without templates' => [
self::singleLevelChildrenWithSameId(),
[
'child.1' => '__1st_original__',
'child.2' => '__2nd_original__',
'child.3' => '__3rd_original__',
],
],
'with children with the same id, with templates' => [
self::singleLevelChildrenWithSameId(),
[
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '3rd custom',
],
[
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '3rd custom',
],
],
'with children with the same id, with partial templates' => [
self::singleLevelChildrenWithSameId(),
[
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '__3rd_original__',
],
[
'child.1' => '1st custom',
'child.2' => '2nd custom',
],
],
];
}
}