diff --git a/composer.json b/composer.json index 4b12f2c..e42ac09 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,8 @@ "php": ">=5.4", "evenement/evenement": "^3.0 || ^2.0", "react/event-loop": "^1.2", - "react/http": "^1.6", - "react/promise": "^3 || ^2.10 || ^1.2.1" + "react/http": "^1.11", + "react/promise": "^3.2 || ^2.10 || ^1.2.1" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36" diff --git a/src/EventSource.php b/src/EventSource.php index 8c70271..8e29182 100644 --- a/src/EventSource.php +++ b/src/EventSource.php @@ -150,13 +150,20 @@ class EventSource extends EventEmitter * @param ?LoopInterface $loop * @throws \InvalidArgumentException for invalid URL */ - public function __construct($url, Browser $browser = null, LoopInterface $loop = null) + public function __construct($url, $browser = null, $loop = null) { $parts = parse_url($url); if (!isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('http', 'https'))) { throw new \InvalidArgumentException(); } + if ($browser !== null && !$browser instanceof Browser) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #2 ($browser) expected null|React\Http\Browser'); + } + if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface'); + } + $this->loop = $loop ?: Loop::get(); if ($browser === null) { $browser = new Browser(null, $this->loop); diff --git a/tests/EventSourceTest.php b/tests/EventSourceTest.php index 5d891b0..c9d3025 100644 --- a/tests/EventSourceTest.php +++ b/tests/EventSourceTest.php @@ -30,6 +30,18 @@ public function testConstructorThrowsIfUriArgumentIncludesInvalidScheme() new EventSource('ftp://example.com'); } + public function testCtorThrowsForInvalidBrowser() + { + $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($browser) expected null|React\Http\Browser'); + new EventSource('http://example.com', 'browser'); + } + + public function testCtorThrowsForInvalidLoop() + { + $this->setExpectedException('InvalidArgumentException', 'Argument #3 ($loop) expected null|React\EventLoop\LoopInterface'); + new EventSource('http://example.com', null, 'loop'); + } + public function testConstructWithoutBrowserAndLoopAssignsBrowserAndLoopAutomatically() { $es = new EventSource('http://example.com');