From ea67e113b2942d72ba357e090f14ffa19e2deb8a Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 15 Mar 2017 08:40:58 +0100 Subject: [PATCH] Add test for loop accessor --- lib/Loop.php | 2 ++ test/Loop/DriverStateTest.php | 10 ++++++++++ test/LoopTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/lib/Loop.php b/lib/Loop.php index 428eea9..aa1f457 100644 --- a/lib/Loop.php +++ b/lib/Loop.php @@ -358,4 +358,6 @@ final class Loop { // Default factory, don't move this a file loaded by the composer "files" autoload mechanism, otherwise custom // implementations might have issues setting a default loop, because it's overridden by us then. +// @codeCoverageIgnoreStart Loop::set((new DriverFactory)->create()); +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/test/Loop/DriverStateTest.php b/test/Loop/DriverStateTest.php index a3a5383..f5b08a7 100644 --- a/test/Loop/DriverStateTest.php +++ b/test/Loop/DriverStateTest.php @@ -2,6 +2,7 @@ namespace Amp\Test\Loop; +use Amp\Loop; use Amp\Loop\Driver; use PHPUnit\Framework\TestCase; @@ -27,6 +28,15 @@ class DriverStateTest extends TestCase { $this->assertSame($value, $this->loop->getState("foobar")); } + /** + * @test + * @dataProvider provideValues + */ + public function getsPreviouslySetValueViaAccessor($value) { + Loop::setState("foobar", $value); + $this->assertSame($value, Loop::getState("foobar")); + } + public function provideValues() { return [ ["string"], diff --git a/test/LoopTest.php b/test/LoopTest.php index e1e7d7e..90dc162 100644 --- a/test/LoopTest.php +++ b/test/LoopTest.php @@ -17,4 +17,33 @@ class LoopTest extends TestCase { Loop::repeat(-1, function () {}); } + + public function testOnReadable() { + Loop::run(function () { + $ends = stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); + fwrite($ends[0], "trigger readability watcher"); + + Loop::onReadable($ends[1], function () { + $this->assertTrue(true); + Loop::stop(); + }); + }); + } + + public function testOnWritable() { + Loop::run(function () { + Loop::onWritable(STDOUT, function () { + $this->assertTrue(true); + Loop::stop(); + }); + }); + } + + public function testGet() { + $this->assertInstanceOf(Loop\Driver::class, Loop::get()); + } + + public function testGetInto() { + $this->assertSame(Loop::get()->getInfo(), Loop::getInfo()); + } } \ No newline at end of file