Skip to content
Merged
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
8 changes: 7 additions & 1 deletion 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 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');
}
}