[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / components / testing_framework / core / Functional / Framework / Constraint / RequestSection / StructureHasRecordConstraint.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 class StructureHasRecordConstraint extends AbstractStructureRecordConstraint
23 {
24 /**
25 * @param ResponseSection $responseSection
26 * @return bool
27 */
28 protected function matchesSection(ResponseSection $responseSection)
29 {
30 $nonMatchingVariants = [];
31 $remainingRecordVariants = [];
32
33 foreach ($responseSection->findStructures($this->recordIdentifier, $this->recordField) as $path => $structure) {
34 if (empty($structure) || !is_array($structure)) {
35 $this->sectionFailures[$responseSection->getIdentifier()] = 'No records found in "' . $path . '"';
36 return false;
37 }
38
39 $remainingRecords = [];
40 $nonMatchingValues = $this->getNonMatchingValues($structure);
41
42 if ($this->strict) {
43 $remainingRecords = $this->getRemainingRecords($structure);
44 }
45
46 if (empty($nonMatchingValues) && (!$this->strict || empty($remainingRecords))) {
47 return true;
48 }
49
50 if (!empty($nonMatchingValues)) {
51 $nonMatchingVariants[$path] = $nonMatchingValues;
52 }
53 if ($this->strict && !empty($remainingRecords)) {
54 $remainingRecordVariants[$path] = $remainingRecords;
55 }
56 }
57
58 $failureMessage = '';
59
60 if (!empty($nonMatchingVariants)) {
61 $failureMessage .= 'Could not assert all values for "' . $this->table . '.' . $this->field . '"' . LF;
62 foreach ($nonMatchingVariants as $path => $nonMatchingValues) {
63 $failureMessage .= ' * Not found in "' . $path . '": ' . implode(', ', $nonMatchingValues) . LF;
64 }
65 }
66
67 if (!empty($remainingRecordVariants)) {
68 $failureMessage .= 'Found remaining records for "' . $this->table . '.' . $this->field . '"' . LF;
69 foreach ($remainingRecordVariants as $path => $remainingRecords) {
70 $failureMessage .= ' * Found in "' . $path . '": ' . implode(', ', array_keys($remainingRecords)) . LF;
71 }
72 }
73
74 $this->sectionFailures[$responseSection->getIdentifier()] = $failureMessage;
75 return false;
76 }
77
78 /**
79 * Returns a string representation of the constraint.
80 *
81 * @return string
82 */
83 public function toString()
84 {
85 return 'structure has record';
86 }
87 }