TYPO3 CMS  TYPO3_7-6
AbstractRecordConstraint.php
Go to the documentation of this file.
1 <?php
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 
18 
22 abstract class AbstractRecordConstraint extends \PHPUnit_Framework_Constraint
23 {
27  protected $sectionFailures = [];
28 
32  protected $table;
33 
37  protected $field;
38 
42  protected $strict = false;
43 
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()
62  {
63  $values = func_get_args();
64  $this->values = $values;
65  return $this;
66  }
67 
68  public function setStrict($strict)
69  {
70  $this->strict = (bool)$strict;
71  return $this;
72  }
73 
81  protected function matches($other)
82  {
83  if (is_array($other)) {
84  $success = null;
85  foreach ($other as $item) {
86  $currentSuccess = $this->matchesSection($item);
87  $success = ($success === null ? $currentSuccess : $success || $currentSuccess);
88  }
89  return !empty($success);
90  } else {
91  return $this->matchesSection($other);
92  }
93  }
94 
99  abstract protected function matchesSection(ResponseSection $responseSection);
100 
105  protected function getNonMatchingValues(array $records)
106  {
108 
109  foreach ($records as $recordIdentifier => $recordData) {
110  if (strpos($recordIdentifier, $this->table . ':') !== 0) {
111  continue;
112  }
113 
114  if (($foundValueIndex = array_search($recordData[$this->field], $values)) !== false) {
115  unset($values[$foundValueIndex]);
116  }
117  }
118 
119  return $values;
120  }
121 
126  protected function getRemainingRecords(array $records)
127  {
129 
130  foreach ($records as $recordIdentifier => $recordData) {
131  if (strpos($recordIdentifier, $this->table . ':') !== 0) {
132  unset($records[$recordIdentifier]);
133  continue;
134  }
135 
136  if (($foundValueIndex = array_search($recordData[$this->field], $values)) !== false) {
137  unset($values[$foundValueIndex]);
138  unset($records[$recordIdentifier]);
139  }
140  }
141 
142  return $records;
143  }
144 
154  protected function failureDescription($other)
155  {
156  return $this->toString();
157  }
158 
168  protected function additionalFailureDescription($other)
169  {
170  $failureDescription = '';
171  foreach ($this->sectionFailures as $sectionIdentifier => $sectionFailure) {
172  $failureDescription .= '* Section "' . $sectionIdentifier . '": ' . $sectionFailure . LF;
173  }
174  return $failureDescription;
175  }
176 }