diff --git a/composer.json b/composer.json index 72b3fdc..f553628 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "autoload-dev": { "psr-4": { - "Interop\\Async\\Loop\\": "test" + "Interop\\Async\\Loop\\Test\\": "test" } } } diff --git a/src/Loop.php b/src/Loop.php index e5c2c29..682f114 100644 --- a/src/Loop.php +++ b/src/Loop.php @@ -72,8 +72,7 @@ final class Loop self::$level++; try { - $callback(); - + self::$driver->defer($callback); self::$driver->run(); } finally { self::$driver = $previousDriver; diff --git a/test/DummyDriver.php b/test/DummyDriver.php new file mode 100644 index 0000000..da68260 --- /dev/null +++ b/test/DummyDriver.php @@ -0,0 +1,46 @@ +defers)) { + try { + $defer(self::$id++, $data); + } catch (Exception $e) { + if ($handler = $this->handler) { + $handler($e); + } else { + throw $e; + } + } + } + } + + public function defer(callable $callback, $data = null) { + $this->defers[] = [$callback, $data]; + } + + public function setErrorHandler(callable $callback = null) { + $this->handler = $callback; + } + + public function stop() {} + public function delay($delay, callable $callback, $data = null) { return self::$id++; } + public function repeat($interval, callable $callback, $data = null) { return self::$id++; } + public function onReadable($stream, callable $callback, $data = null) { return self::$id++; } + public function onWritable($stream, callable $callback, $data = null) { return self::$id++; } + public function onSignal($signo, callable $callback, $data = null) { return self::$id++; } + public function enable($watcherId) {} + public function disable($watcherId) {} + public function cancel($watcherId) {} + public function reference($watcherId) {} + public function unreference($watcherId) {} + public function info() {} + public function getHandle() {} +} diff --git a/test/LoopTest.php b/test/LoopTest.php index f4e38a2..363ffb0 100644 --- a/test/LoopTest.php +++ b/test/LoopTest.php @@ -1,6 +1,6 @@ getMockBuilder(Driver::class)->getMock(); + $driver = new DummyDriver; - $factory = $this->getMockBuilder(DriverFactory::class)->getMock(); + $factory = $this->getMockBuilder(Loop\DriverFactory::class)->getMock(); $factory->method("create")->willReturn($driver); Loop::setFactory($factory); @@ -30,8 +30,8 @@ class LoopTest extends \PHPUnit_Framework_TestCase /** @test */ public function executeStackReturnsScopedDriver() { - $driver1 = $this->getMockBuilder(Driver::class)->getMock(); - $driver2 = $this->getMockBuilder(Driver::class)->getMock(); + $driver1 = new DummyDriver; + $driver2 = new DummyDriver; Loop::execute(function () use ($driver1, $driver2) { $this->assertSame($driver1, Loop::get());