Last active
September 15, 2016 10:31
-
-
Save mkusher/3eae07a880c0e46ff21619a1e30c3aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Mkusher\Co; | |
use React\EventLoop\Factory; | |
use React\Promise; | |
use Webmozart\Assert\Assert; | |
require dirname(__DIR__) . '/vendor/autoload.php'; | |
$loop = Factory::create(); | |
it("should wait for all promises and return result", Co\await(function() { | |
$wait1 = yield Promise\resolve("123"); | |
$wait2 = yield Promise\resolve("456"); | |
return $wait1 . $wait2; | |
})->then(function($result) { | |
Assert::same($result, "123456"); | |
})); | |
it("should catch rejected promises", Co\await(function() { | |
try { | |
$wait1 = yield Promise\reject(new \Exception("Some error")); | |
} catch (\Exception $e) { | |
return $e->getMessage(); | |
} | |
return "Not caught"; | |
})->then(function($result) { | |
Assert::same($result, "Some error"); | |
})); | |
it("should return non-promises as is", Co\await(function() { | |
$wait1 = yield "123"; | |
$wait2 = yield Promise\resolve("456"); | |
return $wait1 . $wait2; | |
})->then(function($result) { | |
Assert::same($result, "123456"); | |
})); | |
it("should return null if no return", Co\await(function() { | |
$wait1 = yield Promise\resolve("123"); | |
yield $wait1 . "456"; | |
})->then(function($result) { | |
Assert::same($result, null); | |
})); | |
it("should wait for nested generators", Co\await(function() { | |
$nestedCoroutine = function() { | |
$wait1 = yield Promise\resolve("123"); | |
$wait2 = yield Promise\resolve("456"); | |
return $wait1 . $wait2; | |
}; | |
$message1 = yield $nestedCoroutine(); | |
$message2 = yield $nestedCoroutine(); | |
return $message1 . $message2; | |
})->then(function($result) { | |
Assert::same($result, "123456123456"); | |
})); | |
it("should pass exception to nested coroutine", Co\await(function() { | |
$nestedCoroutine = function() { | |
try { | |
yield Promise\reject(new \Exception("Some error")); | |
} catch (\Exception $e) { | |
return $e->getMessage(); | |
} | |
return "Uncaught"; | |
}; | |
$message = yield $nestedCoroutine(); | |
return $message; | |
})->then(function($result) { | |
Assert::same($result, "Some error"); | |
})); | |
$loop->run(); | |
printf("%s %s successful\n", $okSpecs, $okSpecs === 1 ? "is": "are"); | |
$okSpecs = 0; | |
function ok() { | |
global $okSpecs; | |
++$okSpecs; | |
} | |
function it($name, $specPromise) { | |
$specPromise->done(function() { | |
ok(); | |
}, function($error) use ($name) { | |
printf("[x] Spec \"it %s\" errored with:\n", $name); | |
throw $error; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment