1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Liip/FunctionalTestBundle |
7
|
|
|
* |
8
|
|
|
* (c) Lukas Kahwe Smith <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Liip\FunctionalTestBundle\Test; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\Constraint\Constraint; |
17
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
18
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
19
|
|
|
|
20
|
|
|
class ValidationErrorsConstraint extends Constraint |
21
|
|
|
{ |
22
|
|
|
private $expect; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ValidationErrorsConstraint constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $expect) |
28
|
|
|
{ |
29
|
|
|
$this->expect = $expect; |
30
|
|
|
sort($this->expect); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ConstraintViolationList $other |
35
|
|
|
* @param string $description |
36
|
|
|
* @param bool $returnResult |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function evaluate($other, $description = '', $returnResult = false): ?bool |
41
|
|
|
{ |
42
|
|
|
$actual = []; |
43
|
|
|
|
44
|
|
|
foreach ($other as $error) { |
45
|
|
|
$actual[$error->getPropertyPath()][] = $error->getMessage(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
ksort($actual); |
49
|
|
|
|
50
|
|
|
if (array_keys($actual) === $this->expect) { |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($returnResult) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Generate failure message |
59
|
|
|
$mismatchedKeys = array_merge( |
60
|
|
|
array_diff(array_keys($actual), $this->expect), |
61
|
|
|
array_diff($this->expect, array_keys($actual)) |
62
|
|
|
); |
63
|
|
|
sort($mismatchedKeys); |
64
|
|
|
|
65
|
|
|
$lines = []; |
66
|
|
|
|
67
|
|
|
foreach ($mismatchedKeys as $key) { |
68
|
|
|
if (isset($actual[$key])) { |
69
|
|
|
foreach ($actual[$key] as $unexpectedErrorMessage) { |
70
|
|
|
$lines[] = '+ '.$key.' ('.$unexpectedErrorMessage.')'; |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
$lines[] = '- '.$key; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new ExpectationFailedException($description."\n".implode("\n", $lines)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a string representation of the object. |
82
|
|
|
*/ |
83
|
|
|
public function toString(): string |
84
|
|
|
{ |
85
|
|
|
return 'validation errors match'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|