From 3c03e64ed04c4a549eb76fa26383f7c581ee00cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 29 Jul 2018 18:10:38 +0200 Subject: [PATCH] Forward compatibility with stable EventLoop 1.0 and 0.5 --- composer.json | 6 +++--- src/Client.php | 5 +++-- tests/ClientTest.php | 10 +++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index fcbdd74..ede20d6 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ }, "require": { "php": ">=5.3", - "react/event-loop": "~0.4.0|~0.3.0", - "react/promise": "~2.0|~1.0", - "clue/multicast-react": "~0.2.0" + "clue/multicast-react": "^1.0 || ^0.2", + "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/promise": "^2.0 || ^1.0" }, "require-dev": { "phpunit/phpunit": "^6.0 || ^5.7 || ^4.8.35" diff --git a/src/Client.php b/src/Client.php index 9455af1..db3e8af 100644 --- a/src/Client.php +++ b/src/Client.php @@ -40,9 +40,10 @@ public function search($searchTarget = 'ssdp:all', $mx = 2) $socket->close(); }); - $deferred = new Deferred(function () use ($socket, &$timer) { + $loop = $this->loop; + $deferred = new Deferred(function () use ($socket, &$timer, $loop) { // canceling resulting promise cancels timer and closes socket - $timer->cancel(); + $loop->cancelTimer($timer); $socket->close(); throw new RuntimeException('Cancelled'); }); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index d53878f..cfca429 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -23,8 +23,13 @@ public function testSearchCancel() $socket = $this->getMockBuilder('React\Datagram\SocketInterface')->getMock(); $socket->expects($this->once())->method('send'); - $timer = $this->getMockBuilder('React\EventLoop\Timer\TimerInterface')->getMock(); - $loop->expects($this->once())->method('addTimer')->will($this->returnValue($timer)); + // prefer newer EventLoop 1.0/0.5+ TimerInterface or fall back to legacy namespace + $timer = $this->getMockBuilder( + interface_exists('React\EventLoop\TimerInterface') ? 'React\EventLoop\TimerInterface' : 'React\EventLoop\Timer\TimerInterface' + )->getMock(); + + $loop->expects($this->once())->method('addTimer')->willReturn($timer); + $loop->expects($this->once())->method('cancelTimer')->with($timer); $multicast->expects($this->once())->method('createSender')->will($this->returnValue($socket)); @@ -37,7 +42,6 @@ public function testSearchCancel() } $socket->expects($this->once())->method('close'); - $timer->expects($this->once())->method('cancel'); $promise->cancel();