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 e16f131 commit 6663f2c
Show file tree
Hide file tree
Showing 17 changed files with 2,811 additions and 10 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
"dazzle-php/event": "0.5.*",
"dazzle-php/loop": "0.5.*",
"dazzle-php/socket": "0.5.*",
"dazzle-php/throwable": "0.5.*"
"dazzle-php/throwable": "0.5.*",
"dazzle-php/util": "0.5.*"
},
"require-dev": {
"phpunit/phpunit": ">=4.8.0 <5.4.0"
},
"autoload": {
"psr-4": {
"Dazzle\\Channel\\Socket\\": "src/Channel-Socket",
"Dazzle\\Channel\\Socket\\Test\\": "test"
"Dazzle\\ChannelSocket\\": "src/Channel-Socket",
"Dazzle\\ChannelSocket\\Test\\": "test"
}
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion src/Channel-Socket/Buffer/Buffer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Dazzle\Channel\Socket\Socket\Buffer;
namespace Dazzle\ChannelSocket\Buffer;

/**
* @codeCoverageIgnore
Expand Down
2 changes: 1 addition & 1 deletion src/Channel-Socket/Connection/Connection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Dazzle\Channel\Socket\Socket\Connection;
namespace Dazzle\ChannelSocket\Connection;

use Dazzle\Socket\SocketInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/Channel-Socket/Connection/ConnectionPool.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Dazzle\Channel\Socket\Socket\Connection;
namespace Dazzle\ChannelSocket\Connection;

/**
* @codeCoverageIgnore
Expand Down
8 changes: 4 additions & 4 deletions src/Channel-Socket/Socket.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Dazzle\Channel\Socket\Socket;
namespace Dazzle\ChannelSocket;

use Dazzle\Channel\Socket\Socket\Buffer\Buffer;
use Dazzle\Channel\Socket\Socket\Connection\Connection;
use Dazzle\Channel\Socket\Socket\Connection\ConnectionPool;
use Dazzle\ChannelSocket\Buffer\Buffer;
use Dazzle\ChannelSocket\Connection\Connection;
use Dazzle\ChannelSocket\Connection\ConnectionPool;
use Dazzle\Channel\Model\ModelInterface;
use Dazzle\Channel\Channel;
use Dazzle\Event\BaseEventEmitter;
Expand Down
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\ChannelSocket\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\ChannelSocket\Test;

use Dazzle\Loop\Model\SelectLoop;
use Dazzle\Loop\Loop;
use Dazzle\Loop\LoopExtendedInterface;
use Dazzle\Loop\LoopInterface;
use Dazzle\ChannelSocket\Test\_Simulation\Event;
use Dazzle\ChannelSocket\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 6663f2c

Please sign in to comment.