[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / components / testing_framework / core / Functional / Framework / Constraint / RequestSection / AbstractRecordConstraint.php
1 <?php
2 namespace TYPO3\CMS\Components\TestingFramework\Core\Functional\Framework\Constraint\RequestSection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Components\TestingFramework\Core\Functional\Framework\Frontend\ResponseSection;
18
19 /**
20 * Model of frontend response
21 */
22 abstract class AbstractRecordConstraint extends \PHPUnit_Framework_Constraint
23 {
24 /**
25 * @var array
26 */
27 protected $sectionFailures = [];
28
29 /**
30 * @var string
31 */
32 protected $table;
33
34 /**
35 * @var string
36 */
37 protected $field;
38
39 /**
40 * @var bool
41 */
42 protected $strict = false;
43
44 /**
45 * @var array
46 */
47 protected $values;
48
49 public function setTable($table)
50 {
51 $this->table = $table;
52 return $this;
53 }
54
55 public function setField($field)
56 {
57 $this->field = $field;
58 return $this;
59 }
60
61 public function setValues(...$values)
62 {
63 $this->values = $values;
64 return $this;
65 }
66
67 public function setStrict($strict)
68 {
69 $this->strict = (bool)$strict;
70 return $this;
71 }
72
73 /**
74 * Evaluates the constraint for parameter $other. Returns true if the
75 * constraint is met, false otherwise.
76 *
77 * @param array|ResponseSection|ResponseSection[] $other ResponseSections to evaluate
78 * @return bool
79 */
80 protected function matches($other)
81 {
82 if (is_array($other)) {
83 $success = null;
84 foreach ($other as $item) {
85 $currentSuccess = $this->matchesSection($item);
86 $success = ($success === null ? $currentSuccess : $success || $currentSuccess);
87 }
88 return !empty($success);
89 } else {
90 return $this->matchesSection($other);
91 }
92 }
93
94 /**
95 * @param ResponseSection $responseSection
96 * @return bool
97 */
98 abstract protected function matchesSection(ResponseSection $responseSection);
99
100 /**
101 * @param array $records
102 * @return array
103 */
104 protected function getNonMatchingValues(array $records)
105 {
106 $values = $this->values;
107
108 foreach ($records as $recordIdentifier => $recordData) {
109 if (strpos($recordIdentifier, $this->table . ':') !== 0) {
110 continue;
111 }
112
113 if (($foundValueIndex = array_search($recordData[$this->field], $values)) !== false) {
114 unset($values[$foundValueIndex]);
115 }
116 }
117
118 return $values;
119 }
120
121 /**
122 * @param array $records
123 * @return array
124 */
125 protected function getRemainingRecords(array $records)
126 {
127 $values = $this->values;
128
129 foreach ($records as $recordIdentifier => $recordData) {
130 if (strpos($recordIdentifier, $this->table . ':') !== 0) {
131 unset($records[$recordIdentifier]);
132 continue;
133 }
134
135 if (($foundValueIndex = array_search($recordData[$this->field], $values)) !== false) {
136 unset($values[$foundValueIndex]);
137 unset($records[$recordIdentifier]);
138 }
139 }
140
141 return $records;
142 }
143
144 /**
145 * Returns the description of the failure
146 *
147 * The beginning of failure messages is "Failed asserting that" in most
148 * cases. This method should return the second part of that sentence.
149 *
150 * @param mixed $other Evaluated value or object.
151 * @return string
152 */
153 protected function failureDescription($other)
154 {
155 return $this->toString();
156 }
157
158 /**
159 * Return additional failure description where needed
160 *
161 * The function can be overridden to provide additional failure
162 * information like a diff
163 *
164 * @param mixed $other Evaluated value or object.
165 * @return string
166 */
167 protected function additionalFailureDescription($other)
168 {
169 $failureDescription = '';
170 foreach ($this->sectionFailures as $sectionIdentifier => $sectionFailure) {
171 $failureDescription .= '* Section "' . $sectionIdentifier . '": ' . $sectionFailure . LF;
172 }
173 return $failureDescription;
174 }
175 }