21
21
use PhpSchool \PhpWorkshop \Result \ResultInterface ;
22
22
use PhpSchool \PhpWorkshop \ResultAggregator ;
23
23
use PhpSchool \PhpWorkshop \Utils \Collection ;
24
+ use PhpSchool \PhpWorkshop \Utils \Path ;
24
25
use PhpSchool \PhpWorkshop \Utils \System ;
25
26
use PHPUnit \Framework \ExpectationFailedException ;
26
27
use PHPUnit \Framework \TestCase ;
34
35
35
36
abstract class WorkshopExerciseTest extends TestCase
36
37
{
37
- /**
38
- * @var Application
39
- */
40
- protected $ app ;
38
+ private Application $ app ;
39
+ private ContainerInterface $ container ;
40
+ private ResultAggregator $ results ;
41
41
42
- /**
43
- * @var ContainerInterface
44
- */
45
- protected $ container ;
42
+ private Filesystem $ filesystem ;
43
+ private string $ executionDirectory ;
46
44
47
- /**
48
- * @var ResultAggregator
49
- */
50
- protected $ results ;
45
+ public const SINGLE_FILE_SOLUTION = 'single-file-solution ' ;
46
+ public const DIRECTORY_SOLUTION = 'directory-solution ' ;
51
47
52
48
public function setUp (): void
53
49
{
54
50
$ this ->app = $ this ->getApplication ();
55
51
$ this ->container = $ this ->app ->configure ();
52
+ $ this ->filesystem = new Filesystem ();
53
+ $ this ->executionDirectory = System::randomTempDir ();
56
54
}
57
55
58
56
public function tearDown (): void
59
57
{
60
- // (new Filesystem())-> remove(System::tempDir() );
58
+ $ this -> filesystem -> remove ($ this -> executionDirectory );
61
59
}
62
60
63
61
/**
@@ -73,8 +71,12 @@ protected function getExercise(): ExerciseInterface
73
71
->findByClassName ($ this ->getExerciseClass ());
74
72
}
75
73
76
- public function runExercise (string $ submissionFile ): void
74
+ public function runExercise (string $ submissionFile, string $ type = self :: SINGLE_FILE_SOLUTION ): void
77
75
{
76
+ //we copy the test solution to a random directory
77
+ //so we can properly cleanup. It also saves us from patch crashes.
78
+ $ this ->filesystem ->mkdir ($ this ->executionDirectory );
79
+
78
80
$ exercise = $ this ->getExercise ();
79
81
80
82
$ submissionFileAbsolute = sprintf (
@@ -94,6 +96,14 @@ public function runExercise(string $submissionFile): void
94
96
);
95
97
}
96
98
99
+ if ($ type === self ::SINGLE_FILE_SOLUTION ) {
100
+ $ this ->filesystem ->copy ($ submissionFileAbsolute , Path::join ($ this ->executionDirectory , $ submissionFile ));
101
+ } else {
102
+ $ this ->filesystem ->mirror (dirname ($ submissionFileAbsolute ), $ this ->executionDirectory );
103
+ }
104
+
105
+ $ submissionFileAbsolute = Path::join ($ this ->executionDirectory , basename ($ submissionFile ));
106
+
97
107
if ($ exercise instanceof ComposerExerciseCheck) {
98
108
$ this ->installDeps ($ exercise , dirname ($ submissionFileAbsolute ));
99
109
}
@@ -102,7 +112,7 @@ public function runExercise(string $submissionFile): void
102
112
'program ' => $ submissionFileAbsolute ,
103
113
]);
104
114
105
- $ this ->results = $ this ->container ->get (ExerciseDispatcher::class)
115
+ $ this ->results = $ this ->container ->make (ExerciseDispatcher::class)
106
116
->verify ($ exercise , $ input );
107
117
}
108
118
@@ -237,16 +247,4 @@ public function assertResultsHasFailureAndMatches(string $resultClass, callable
237
247
$ this ->assertTrue ($ matcher ($ failure ));
238
248
});
239
249
}
240
-
241
- public function removeSolutionAsset (string $ file ): void
242
- {
243
- $ path = sprintf (
244
- '%s/test/solutions/%s/%s ' ,
245
- rtrim ($ this ->container ->get ('basePath ' ), '/ ' ),
246
- AbstractExercise::normaliseName ($ this ->getExercise ()->getName ()),
247
- $ file ,
248
- );
249
-
250
- (new Filesystem ())->remove ($ path );
251
- }
252
250
}
0 commit comments