Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ on:
jobs:
PHPUnit:
name: PHPUnit (PHP ${{ matrix.php }})
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
php:
- 8.4
- 8.3
- 8.2
- 8.1
Expand Down Expand Up @@ -38,7 +39,7 @@ jobs:

PHPUnit-hhvm:
name: PHPUnit (HHVM)
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
continue-on-error: true
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
},
"require": {
"php": ">=5.3",
"clue/multicast-react": "^1.0 || ^0.2",
"clue/multicast-react": "1.x-dev#f1bd5df0309b9f8b3486b09bf37aedfb042addb6",
"react/event-loop": "^1.2",
"react/promise": "^2.0 || ^1.0"
"react/promise": "^3.2 || ^2.7 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
Expand Down
24 changes: 15 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ class Client
* @param ?LoopInterface $loop
* @param ?MulticastFactory $multicast
*/
public function __construct(LoopInterface $loop = null, MulticastFactory $multicast = null)
public function __construct($loop = null, $multicast = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
}
if ($multicast !== null && !$multicast instanceof MulticastFactory) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
}
$this->loop = $loop ?: Loop::get();
$this->multicast = $multicast ?: new MulticastFactory($this->loop);
}
Expand All @@ -46,11 +52,7 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
$socket = $this->multicast->createSender();
// TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.

$timer = $this->loop->addTimer($mx, function() use ($socket, &$deferred) {
$deferred->resolve();
$socket->close();
});

$messages = array();
$loop = $this->loop;
$deferred = new Deferred(function () use ($socket, &$timer, $loop) {
// canceling resulting promise cancels timer and closes socket
Expand All @@ -59,11 +61,15 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
throw new RuntimeException('Cancelled');
});

$timer = $this->loop->addTimer($mx, function() use ($socket, &$deferred, &$messages) {
$deferred->resolve($messages);
$socket->close();
});

$that = $this;
$socket->on('message', function ($data, $remote) use ($deferred, $that) {
$socket->on('message', function ($data, $remote) use (&$messages, $that) {
$message = $that->parseMessage($data, $remote);

$deferred->progress($message);
$messages[] = $message;
});

$socket->send($data, self::ADDRESS);
Expand Down
28 changes: 28 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,32 @@ public function testSearchTimeout()

$promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever());
}

public function testCtorThrowsForInvalidLoop()
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
} else {
// legacy PHPUnit
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
}

new Client('loop');
}

public function testCtorThrowsForInvalidMulticast()
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
} else {
// legacy PHPUnit
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
}

new Client(null, 'multicast');
}
}