-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for EventSource implementation
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php namespace web\unittest\io; | ||
|
||
use io\streams\{InputStream, MemoryInputStream}; | ||
use test\{Assert, Test, Values}; | ||
use web\io\EventSource; | ||
|
||
class EventSourceTest { | ||
|
||
/** Returns an input stream from the given lines */ | ||
private function stream(array $lines): InputStream { | ||
return new MemoryInputStream(implode("\n", $lines)); | ||
} | ||
|
||
/** @return iterable */ | ||
private function inputs() { | ||
yield [[], []]; | ||
yield [[''], []]; | ||
yield [['data: One'], [[null => 'One']]]; | ||
yield [['', 'data: One'], [[null => 'One']]]; | ||
yield [['data: One', '', 'data: Two'], [[null => 'One'], [null => 'Two']]]; | ||
yield [['event: test', 'data: One'], [['test' => 'One']]]; | ||
yield [['event: test', 'data: One', '', 'data: Two'], [['test' => 'One'], [null => 'Two']]]; | ||
} | ||
|
||
#[Test] | ||
public function can_create() { | ||
new EventSource($this->stream([])); | ||
} | ||
|
||
#[Test, Values(from: 'inputs')] | ||
public function events($lines, $expected) { | ||
$events= new EventSource($this->stream($lines)); | ||
$actual= []; | ||
foreach ($events as $type => $event) { | ||
$actual[]= [$type => $event]; | ||
} | ||
Assert::equals($expected, $actual); | ||
} | ||
} |