6
6
7
7
class ExamplesTest extends TestCase
8
8
{
9
+ private const EXAMPLES_DIR = __DIR__ .'/../ ' ;
10
+
9
11
public function examplesProvider ()
10
12
{
11
13
$ iterator = new \FilesystemIterator (
12
- __DIR__ . ' /../ ' ,
14
+ self :: EXAMPLES_DIR ,
13
15
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS
14
16
);
15
17
@@ -18,21 +20,58 @@ public function examplesProvider()
18
20
continue ;
19
21
}
20
22
21
- yield $ name => [$ fileinfo ->getRealPath ()];
23
+ foreach ($ this ->extractExampleCode ($ fileinfo ->getRealPath ()) as $ eventloopName => $ code ) {
24
+ yield "$ name $ eventloopName " => [$ fileinfo ->getRealPath (), $ eventloopName , $ code ];
25
+ }
22
26
}
23
27
}
24
28
25
29
/**
26
30
* @dataProvider examplesProvider
27
31
*/
28
- public function testExampleShouldRun ($ exampleFile )
32
+ public function testExampleShouldRun (string $ exampleFile , string $ eventloopName , string $ exampleCode )
33
+ {
34
+ // Sanitize loop name to create a relevant temporary filename
35
+ $ eventLoopFileId = preg_replace ('/[^a-z0-9]+/ ' , '' , strtolower ($ eventloopName ));
36
+ $ tmpFilePath = tempnam (self ::EXAMPLES_DIR , basename ($ exampleFile , '.php ' )."- $ eventLoopFileId- " );
37
+
38
+ try {
39
+ file_put_contents ($ tmpFilePath , $ exampleCode );
40
+
41
+ $ output = [];
42
+ $ code = null ;
43
+ exec ("php $ tmpFilePath " , $ output , $ code );
44
+
45
+ $ this ->assertSame (0 , $ code );
46
+ $ this ->assertStringStartsWith ("Let's start! " , reset ($ output ));
47
+ $ this ->assertStringEndsWith ('Finished! ' , end ($ output ));
48
+ } finally {
49
+ unlink ($ tmpFilePath );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Very naive approach to iterate over various eventLoop implementations.
55
+ */
56
+ private function extractExampleCode (string $ exampleFiles ): iterable
29
57
{
30
- $ output = [];
31
- $ code = null ;
32
- exec ($ exampleFile , $ output , $ code );
58
+ $ originalContent = file ($ exampleFiles );
59
+
60
+ foreach ($ originalContent as &$ line ) {
61
+ if (false === strpos ($ line , '$eventLoop = new ' )) {
62
+ continue ;
63
+ }
33
64
34
- $ this ->assertSame (0 , $ code );
35
- $ this ->assertStringStartsWith ("Let's start! " , reset ($ output ));
36
- $ this ->assertStringEndsWith ('Finished! ' , end ($ output ));
65
+ // Extract relevant name
66
+ $ name = strstr (strstr ($ line , '( ' , true ), 'Adapter \\' );
67
+
68
+ // Enable current eventLoop
69
+ $ line = ltrim ($ line , '/ ' );
70
+
71
+ yield $ name => implode ('' , $ originalContent );
72
+
73
+ // Disable this eventLoop
74
+ $ line = "// $ line " ;
75
+ }
37
76
}
38
77
}
0 commit comments