@@ -36,9 +36,7 @@ trait TestingFramework
3636 */
3737 protected function importPHPDataSet (string $ filePath ): void
3838 {
39- $ this ->ensureFileExists ($ filePath );
40-
41- $ dataSet = include $ filePath ;
39+ $ dataSet = $ this ->getDataSet ($ filePath );
4240 try {
4341 (new PhpDataSet ())->import ($ dataSet );
4442 } catch (Exception $ e ) {
@@ -52,57 +50,106 @@ protected function importPHPDataSet(string $filePath): void
5250 */
5351 protected function assertPHPDataSet (string $ filePath ): void
5452 {
55- $ this ->ensureFileExists ($ filePath );
53+ if (is_array ($ GLOBALS ['TCA ' ] ?? null ) === false ) {
54+ throw new \RuntimeException ('TYPO3 GLOBALS["TCA"] is not defined. ' , 1760942400 );
55+ }
5656
57- $ dataSet = include $ filePath ;
5857 $ failMessages = [];
59-
60- foreach ($ dataSet as $ tableName => $ expectedRecords ) {
58+ foreach ($ this ->getDataSet ($ filePath ) as $ tableName => $ expectedRecords ) {
6159 $ records = $ this ->getAllRecords ($ tableName , (isset ($ GLOBALS ['TCA ' ][$ tableName ])));
6260
6361 foreach ($ expectedRecords as $ assertion ) {
6462 $ result = $ this ->assertInRecords ($ assertion , $ records );
6563 if ($ result === false ) {
66- // Handle error
67- if (isset ($ assertion ['uid ' ]) && empty ($ records [$ assertion ['uid ' ]])) {
68- $ failMessages [] = 'Record " ' . $ tableName . ': ' . $ assertion ['uid ' ] . '" not found in database ' ;
69- continue ;
70- }
71- if (isset ($ assertion ['uid ' ])) {
72- $ recordIdentifier = $ tableName . ': ' . $ assertion ['uid ' ];
73- $ additionalInformation = $ this ->renderRecords ($ assertion , $ records [$ assertion ['uid ' ]]);
74- } else {
75- $ recordIdentifier = $ tableName ;
76- $ additionalInformation = $ this ->arrayToString ($ assertion );
77- }
78-
79- $ failMessages [] = 'Assertion in data-set failed for " ' . $ recordIdentifier . '": ' . PHP_EOL . $ additionalInformation ;
64+ $ failMessages [] = $ this ->getAssertionErrorMessageForNoneMatchingRecord ($ assertion , $ records , $ tableName );
8065 continue ;
8166 }
8267
83- // Unset asserted record
68+ // Unset already asserted record to only keep unexpected records.
8469 unset($ records [$ result ]);
70+
8571 // Increase assertion counter
86- self ::assertTrue ($ result !== false );
72+ self ::assertTrue (true );
8773 }
8874
89- if (!empty ($ records )) {
90- foreach ($ records as $ record ) {
91- $ recordIdentifier = $ tableName . ': ' . ($ record ['uid ' ] ?? '' );
92- $ failMessages [] = 'Not asserted record found for " ' . $ recordIdentifier . '". ' ;
75+ foreach ($ records as $ record ) {
76+ if (is_array ($ record ) === false ) {
77+ throw new \RuntimeException ('Something went horribly wrong while fetching records, record was not an array. ' , 1760943536 );
9378 }
79+
80+ $ failMessages [] = $ this ->getAssertionErrorMessageForUnexpectedRecord ($ record , $ tableName );
9481 }
9582 }
9683
84+ $ failMessages = array_filter ($ failMessages );
85+
9786 if (!empty ($ failMessages )) {
9887 self ::fail (implode (PHP_EOL , $ failMessages ));
9988 }
10089 }
10190
91+ /**
92+ * @return array<string, array<string, string>[]>
93+ */
94+ private function getDataSet (string $ filePath ): array
95+ {
96+ $ this ->ensureFileExists ($ filePath );
97+
98+ $ dataSet = require $ filePath ;
99+ if (is_array ($ dataSet ) === false ) {
100+ throw new \RuntimeException ('Given file did not return an array: ' . $ filePath , 1760942255 );
101+ }
102+
103+ return $ dataSet ;
104+ }
105+
102106 private function ensureFileExists (string $ filePath ): void
103107 {
104108 if (file_exists ($ filePath ) === false ) {
105109 throw new InvalidArgumentException ('The requested PHP data-set file " ' . $ filePath . '" does not exist. ' , 1681207108 );
106110 }
107111 }
112+
113+ /**
114+ * @param array{uid: int|string|null} $assertion
115+ * @param array<string|int, mixed[]> $records
116+ */
117+ private function getAssertionErrorMessageForNoneMatchingRecord (array $ assertion , array $ records , string $ tableName ): string
118+ {
119+ // Handle error
120+ if (isset ($ assertion ['uid ' ]) && empty ($ records [$ assertion ['uid ' ]])) {
121+ return 'Record " ' . $ tableName . ': ' . $ assertion ['uid ' ] . '" not found in database ' ;
122+ }
123+
124+ if (isset ($ assertion ['uid ' ])) {
125+ $ record = $ records [$ assertion ['uid ' ]] ?? null ;
126+ if (is_array ($ record ) === false ) {
127+ return 'Assertion in data-set failed for " ' . $ tableName . ': ' . $ assertion ['uid ' ] . '": Uid missing in database ' . PHP_EOL ;
128+ }
129+
130+ return 'Assertion in data-set failed for " ' . $ tableName . ': ' . $ assertion ['uid ' ] . '": ' . PHP_EOL . $ this ->renderRecords ($ assertion , $ record );
131+ }
132+
133+ return 'Assertion in data-set failed for " ' . $ tableName . '": ' . PHP_EOL . $ this ->arrayToString ($ assertion );
134+ }
135+
136+ /**
137+ * @param mixed[] $record
138+ */
139+ private function getAssertionErrorMessageForUnexpectedRecord (array $ record , string $ tableName ): string
140+ {
141+ if (is_numeric ($ record ['uid ' ] ?? null )) {
142+ return sprintf (
143+ 'Not asserted record with uid "%s" found for table "%s". ' ,
144+ $ record ['uid ' ],
145+ $ tableName
146+ );
147+ }
148+
149+ return sprintf (
150+ 'Not asserted record found for table "%s": %s ' ,
151+ $ tableName ,
152+ PHP_EOL . var_export ($ record , true )
153+ );
154+ }
108155}
0 commit comments