Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khelle committed Jun 25, 2017
1 parent bb14b16 commit 0c86b11
Show file tree
Hide file tree
Showing 12 changed files with 3,005 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dazzle\ChannelZmq\Test;

class Callback
{
public function __invoke()
{}
}
160 changes: 160 additions & 0 deletions test/TModule.php
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)
);
}
}
}
Loading

0 comments on commit 0c86b11

Please sign in to comment.