-
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.
- Loading branch information
Showing
12 changed files
with
3,005 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Dazzle\ChannelZmq\Test; | ||
|
||
class Callback | ||
{ | ||
public function __invoke() | ||
{} | ||
} |
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,160 @@ | ||
<?php | ||
|
||
namespace Dazzle\ChannelZmq\Test; | ||
|
||
use Dazzle\Loop\Model\SelectLoop; | ||
use Dazzle\Loop\Loop; | ||
use Dazzle\Loop\LoopExtendedInterface; | ||
use Dazzle\Loop\LoopInterface; | ||
use Dazzle\ChannelZmq\Test\_Simulation\Event; | ||
use Dazzle\ChannelZmq\Test\_Simulation\Simulation; | ||
use Exception; | ||
|
||
class TModule extends TUnit | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const MSG_EVENT_NAME_ASSERTION_FAILED = 'Expected event name mismatch on %s event.'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
const MSG_EVENT_DATA_ASSERTION_FAILED = 'Expected event data mismatch on %s event.'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
const MSG_EVENT_GET_ASSERTION_FAILED = "Expected event count mismatch after %s events.\nExpected event %s, got event %s."; | ||
|
||
/** | ||
* @var LoopExtendedInterface | ||
*/ | ||
private $loop; | ||
|
||
/** | ||
* @var Simulation | ||
*/ | ||
private $sim; | ||
|
||
/** | ||
* | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->loop = null; | ||
$this->sim = null; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function tearDown() | ||
{ | ||
unset($this->sim); | ||
unset($this->loop); | ||
} | ||
|
||
/** | ||
* @return LoopInterface|null | ||
*/ | ||
public function getLoop() | ||
{ | ||
return $this->loop; | ||
} | ||
|
||
/** | ||
* Run test scenario as simulation. | ||
* | ||
* @param callable(Simulation) $scenario | ||
* @return TModule | ||
*/ | ||
public function simulate(callable $scenario) | ||
{ | ||
try | ||
{ | ||
$this->loop = new Loop(new SelectLoop); | ||
$this->loop->erase(true); | ||
|
||
$this->sim = new Simulation($this->loop); | ||
$this->sim->setScenario($scenario); | ||
$this->sim->begin(); | ||
} | ||
catch (Exception $ex) | ||
{ | ||
$this->fail($ex->getMessage()); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $events | ||
* @param int $flags | ||
* @return TModule | ||
*/ | ||
public function expect($events, $flags = Simulation::EVENTS_COMPARE_IN_ORDER) | ||
{ | ||
$expectedEvents = []; | ||
|
||
foreach ($events as $event) | ||
{ | ||
$data = isset($event[1]) ? $event[1] : []; | ||
$expectedEvents[] = new Event($event[0], $data); | ||
} | ||
|
||
$this->assertEvents( | ||
$this->sim->getExpectations(), | ||
$expectedEvents, | ||
$flags | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param Event[] $actualEvents | ||
* @param Event[] $expectedEvents | ||
* @param int $flags | ||
*/ | ||
public function assertEvents($actualEvents = [], $expectedEvents = [], $flags = Simulation::EVENTS_COMPARE_IN_ORDER) | ||
{ | ||
$count = max(count($actualEvents), count($expectedEvents)); | ||
|
||
if ($flags === Simulation::EVENTS_COMPARE_RANDOMLY) | ||
{ | ||
sort($actualEvents); | ||
sort($expectedEvents); | ||
} | ||
|
||
for ($i=0; $i<$count; $i++) | ||
{ | ||
if (!isset($actualEvents[$i])) | ||
{ | ||
$this->fail( | ||
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, $expectedEvents[$i]->name(), 'null') | ||
); | ||
} | ||
else if (!isset($expectedEvents[$i])) | ||
{ | ||
$this->fail( | ||
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, 'null', $actualEvents[$i]->name()) | ||
); | ||
} | ||
|
||
$actualEvent = $actualEvents[$i]; | ||
$expectedEvent = $expectedEvents[$i]; | ||
|
||
$this->assertSame( | ||
$expectedEvent->name(), | ||
$actualEvent->name(), | ||
sprintf(self::MSG_EVENT_NAME_ASSERTION_FAILED, $i) | ||
); | ||
$this->assertSame( | ||
$expectedEvent->data(), | ||
$actualEvent->data(), | ||
sprintf(self::MSG_EVENT_DATA_ASSERTION_FAILED, $i) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.