|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Constraint; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\Constraint\Constraint; |
|
6
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
7
|
|
|
use SebastianBergmann\Exporter\Exporter; |
|
8
|
|
|
use SilverStripe\Dev\SSListExporter; |
|
9
|
|
|
use SilverStripe\Dev\TestOnly; |
|
10
|
|
|
use SilverStripe\ORM\SS_List; |
|
11
|
|
|
|
|
12
|
|
|
if (!class_exists(Constraint::class)) { |
|
13
|
|
|
return; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constraint for checking if every item in a SS_List matches a given match, |
|
18
|
|
|
* e.g. every Member has isActive set to true |
|
19
|
|
|
*/ |
|
20
|
|
|
class SSListContainsOnlyMatchingItems extends Constraint implements TestOnly |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $match; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ViewableDataContains |
|
29
|
|
|
*/ |
|
30
|
|
|
private $constraint; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var SSListExporter |
|
34
|
|
|
*/ |
|
35
|
|
|
private $exporter; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($match) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->constraint = new ViewableDataContains($match); |
|
40
|
|
|
$this->match = $match; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function exporter(): Exporter |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->exporter === null) { |
|
46
|
|
|
$this->exporter = new SSListExporter; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->exporter; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Evaluates the constraint for parameter $other |
|
54
|
|
|
* |
|
55
|
|
|
* If $returnResult is set to false (the default), an exception is thrown |
|
56
|
|
|
* in case of a failure. null is returned otherwise. |
|
57
|
|
|
* |
|
58
|
|
|
* If $returnResult is true, the result of the evaluation is returned as |
|
59
|
|
|
* a boolean value instead: true in case of success, false in case of a |
|
60
|
|
|
* failure. |
|
61
|
|
|
* |
|
62
|
|
|
* @param SS_List $other Value or object to evaluate. |
|
63
|
|
|
* @param string $description Additional information about the test |
|
64
|
|
|
* @param bool $returnResult Whether to return a result or throw an exception |
|
65
|
|
|
* |
|
66
|
|
|
* @return null|bool |
|
67
|
|
|
* |
|
68
|
|
|
* @throws ExpectationFailedException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool |
|
71
|
|
|
{ |
|
72
|
|
|
$success = true; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($other as $item) { |
|
75
|
|
|
if (!$this->constraint->evaluate($item, '', true)) { |
|
76
|
|
|
$success = false; |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($returnResult) { |
|
82
|
|
|
return $success; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (!$success) { |
|
86
|
|
|
$this->fail($other, $description); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns a string representation of the object. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function toString() : string |
|
98
|
|
|
{ |
|
99
|
|
|
return 'contains only Objects where "' . key($this->match) . '" is "' . current($this->match) . '"'; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|