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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/EventSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/EventSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down