15
15
use PhpSchool \PhpWorkshop \Result \Cli \CliResult ;
16
16
use PhpSchool \PhpWorkshop \Result \Failure ;
17
17
use PhpSchool \PhpWorkshop \Result \FailureInterface ;
18
+ use PhpSchool \PhpWorkshop \Result \ResultGroupInterface ;
18
19
use PhpSchool \PhpWorkshop \Result \ResultInterface ;
19
20
use PhpSchool \PhpWorkshop \ResultAggregator ;
20
21
use PhpSchool \PhpWorkshop \Utils \Collection ;
22
+ use PHPUnit \Framework \ExpectationFailedException ;
21
23
use PHPUnit \Framework \TestCase ;
22
24
use Psr \Container \ContainerInterface ;
23
25
use PhpSchool \PhpWorkshop \Input \Input ;
@@ -126,11 +128,21 @@ public function assertResultsHasFailure(string $resultClass, string $reason): vo
126
128
}
127
129
128
130
public function assertOutputWasIncorrect (): void
131
+ {
132
+ $ this ->assertFalse ($ this ->getOutputResult ()->isSuccessful ());
133
+ }
134
+
135
+ public function assertOutputWasCorrect (): void
136
+ {
137
+ $ this ->assertTrue ($ this ->getOutputResult ()->isSuccessful ());
138
+ }
139
+
140
+ private function getOutputResult (): ResultGroupInterface
129
141
{
130
142
$ exerciseType = $ this ->getExercise ()->getType ();
131
143
132
144
if ($ exerciseType ->equals (ExerciseType::CLI ())) {
133
- $ results = ( new Collection ( $ this ->results ->getIterator ()->getArrayCopy () ))
145
+ $ results = collect ( $ this ->results ->getIterator ()->getArrayCopy ())
134
146
->filter (function (ResultInterface $ result ) {
135
147
return $ result instanceof CliResult;
136
148
});
@@ -139,16 +151,33 @@ public function assertOutputWasIncorrect(): void
139
151
}
140
152
141
153
if ($ exerciseType ->equals (ExerciseType::CGI ())) {
142
- $ results = ( new Collection ( $ this ->results ->getIterator ()->getArrayCopy () ))
154
+ $ results = collect ( $ this ->results ->getIterator ()->getArrayCopy ())
143
155
->filter (function (ResultInterface $ result ) {
144
156
return $ result instanceof CgiResult;
145
157
});
146
158
147
159
$ this ->assertCount (1 , $ results );
148
160
}
149
161
150
- $ outputResults = $ results ->values ()->get (0 );
162
+ return $ results ->values ()->get (0 );
163
+ }
164
+
165
+ public function assertResultsHasFailureAndMatches (string $ resultClass , callable $ matcher ): void
166
+ {
167
+ $ failures = collect ($ this ->results ->getIterator ()->getArrayCopy ())
168
+ ->filter (function (ResultInterface $ result ) {
169
+ return $ result instanceof FailureInterface;
170
+ })
171
+ ->filter (function (FailureInterface $ failure ) use ($ resultClass ) {
172
+ return $ failure instanceof $ resultClass ;
173
+ });
174
+
175
+ if ($ failures ->count () === 0 ) {
176
+ throw new ExpectationFailedException ("No failures found for class: ' $ resultClass' " );
177
+ }
151
178
152
- $ this ->assertFalse ($ outputResults ->isSuccessful ());
179
+ $ failures ->each (function ($ failure ) use ($ matcher ) {
180
+ $ this ->assertTrue ($ matcher ($ failure ));
181
+ });
153
182
}
154
183
}
0 commit comments