Skip to content

Commit 5cfefb5

Browse files
authored
Merge pull request #35 from M6Web/feature/enhanced-tests
Enhanced tests
2 parents ced875a + b1b4a1d commit 5cfefb5

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: php
22
php:
33
- '7.1'
44
- '7.2'
5+
- '7.3'
56

67
before_script:
78
- phpenv config-rm xdebug.ini

examples/01-async-countdown.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ function asynchronousCountdown(EventLoop $eventLoop, string $name, int $count):
2626
//$eventLoop = new Adapter\Amp\EventLoop();
2727
//$eventLoop = new Adapter\ReactPhp\EventLoop(new React\EventLoop\StreamSelectLoop());
2828

29+
echo "Let's start!\n";
30+
2931
// We can't get directly the result of an asynchronous function,
3032
// but the event loop gives us a promise.
3133
$promiseAlice10 = $eventLoop->async(asynchronousCountdown($eventLoop, 'Alice', 10));
3234
$promiseBob4 = $eventLoop->async(asynchronousCountdown($eventLoop, 'Bob', 4));
3335

34-
echo "Let's start!\n";
3536
// Run the event loop until our goal promise is reached.
3637
$result = $eventLoop->wait(
3738
// We group both promises in one, to run them concurrently.

examples/tests/ExamplesTest.php

+48-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
class ExamplesTest extends TestCase
88
{
9+
private const EXAMPLES_DIR = __DIR__.'/../';
10+
911
public function examplesProvider()
1012
{
1113
$iterator = new \FilesystemIterator(
12-
__DIR__.'/../',
14+
self::EXAMPLES_DIR,
1315
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS
1416
);
1517

@@ -18,21 +20,58 @@ public function examplesProvider()
1820
continue;
1921
}
2022

21-
yield $name => [$fileinfo->getRealPath()];
23+
foreach ($this->extractExampleCode($fileinfo->getRealPath()) as $eventloopName => $code) {
24+
yield "$name $eventloopName" => [$fileinfo->getRealPath(), $eventloopName, $code];
25+
}
2226
}
2327
}
2428

2529
/**
2630
* @dataProvider examplesProvider
2731
*/
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
2957
{
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+
}
3364

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+
}
3776
}
3877
}

0 commit comments

Comments
 (0)