Skip to content

Commit

Permalink
Merge pull request #35 from M6Web/feature/enhanced-tests
Browse files Browse the repository at this point in the history
Enhanced tests
  • Loading branch information
b-viguier authored Jul 25, 2019
2 parents ced875a + b1b4a1d commit 5cfefb5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- '7.1'
- '7.2'
- '7.3'

before_script:
- phpenv config-rm xdebug.ini
Expand Down
3 changes: 2 additions & 1 deletion examples/01-async-countdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ function asynchronousCountdown(EventLoop $eventLoop, string $name, int $count):
//$eventLoop = new Adapter\Amp\EventLoop();
//$eventLoop = new Adapter\ReactPhp\EventLoop(new React\EventLoop\StreamSelectLoop());

echo "Let's start!\n";

// We can't get directly the result of an asynchronous function,
// but the event loop gives us a promise.
$promiseAlice10 = $eventLoop->async(asynchronousCountdown($eventLoop, 'Alice', 10));
$promiseBob4 = $eventLoop->async(asynchronousCountdown($eventLoop, 'Bob', 4));

echo "Let's start!\n";
// Run the event loop until our goal promise is reached.
$result = $eventLoop->wait(
// We group both promises in one, to run them concurrently.
Expand Down
57 changes: 48 additions & 9 deletions examples/tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class ExamplesTest extends TestCase
{
private const EXAMPLES_DIR = __DIR__.'/../';

public function examplesProvider()
{
$iterator = new \FilesystemIterator(
__DIR__.'/../',
self::EXAMPLES_DIR,
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS
);

Expand All @@ -18,21 +20,58 @@ public function examplesProvider()
continue;
}

yield $name => [$fileinfo->getRealPath()];
foreach ($this->extractExampleCode($fileinfo->getRealPath()) as $eventloopName => $code) {
yield "$name $eventloopName" => [$fileinfo->getRealPath(), $eventloopName, $code];
}
}
}

/**
* @dataProvider examplesProvider
*/
public function testExampleShouldRun($exampleFile)
public function testExampleShouldRun(string $exampleFile, string $eventloopName, string $exampleCode)
{
// Sanitize loop name to create a relevant temporary filename
$eventLoopFileId = preg_replace('/[^a-z0-9]+/', '', strtolower($eventloopName));
$tmpFilePath = tempnam(self::EXAMPLES_DIR, basename($exampleFile, '.php')."-$eventLoopFileId-");

try {
file_put_contents($tmpFilePath, $exampleCode);

$output = [];
$code = null;
exec("php $tmpFilePath", $output, $code);

$this->assertSame(0, $code);
$this->assertStringStartsWith("Let's start!", reset($output));
$this->assertStringEndsWith('Finished!', end($output));
} finally {
unlink($tmpFilePath);
}
}

/**
* Very naive approach to iterate over various eventLoop implementations.
*/
private function extractExampleCode(string $exampleFiles): iterable
{
$output = [];
$code = null;
exec($exampleFile, $output, $code);
$originalContent = file($exampleFiles);

foreach ($originalContent as &$line) {
if (false === strpos($line, '$eventLoop = new ')) {
continue;
}

$this->assertSame(0, $code);
$this->assertStringStartsWith("Let's start!", reset($output));
$this->assertStringEndsWith('Finished!', end($output));
// Extract relevant name
$name = strstr(strstr($line, '(', true), 'Adapter\\');

// Enable current eventLoop
$line = ltrim($line, '/');

yield $name => implode('', $originalContent);

// Disable this eventLoop
$line = "//$line";
}
}
}

0 comments on commit 5cfefb5

Please sign in to comment.