Skip to content

Commit

Permalink
Merge pull request #2 from elephfront/tests-messager
Browse files Browse the repository at this point in the history
Add tests for the LiveReloadMessager
  • Loading branch information
HavokInspiration authored Aug 8, 2017
2 parents 9462aec + fbdbf62 commit 399453d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/LiveReloadMessager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function onOpen(ConnectionInterface $connection)
{
$this->clients->attach($connection);

echo 'New connection to the LiveReload server' . "\n";
$this->output('New connection to the LiveReload server');
}

/**
Expand All @@ -64,12 +64,12 @@ public function onOpen(ConnectionInterface $connection)
public function onMessage(ConnectionInterface $connection, $message)
{
$targetCount = count($this->clients) - 1;
echo sprintf(
$this->output(sprintf(
'Sending message "%s" to %d other connection%s',
$message,
$targetCount,
$targetCount == 1 ? '' : 's'
) . "\n";
));

foreach ($this->clients as $client) {
if ($connection !== $client) {
Expand All @@ -89,7 +89,7 @@ public function onClose(ConnectionInterface $connection)
{
$this->clients->detach($connection);

echo 'Connection has disconnected' . "\n";
$this->output('Connection has disconnected');
}

/**
Expand All @@ -102,8 +102,19 @@ public function onClose(ConnectionInterface $connection)
*/
public function onError(ConnectionInterface $connection, \Exception $exception)
{
echo sprintf('An error has occurred: `%s`', $exception->getMessage()) . "\n";
$this->output(sprintf('An error has occurred: `%s`', $exception->getMessage()));

$connection->close();
}

/**
* Outputs a message.
*
* @param string $message The message to output
* @return void
*/
protected function output($message)
{
echo $message . "\n";
}
}
119 changes: 119 additions & 0 deletions tests/TestCase/Robo/Task/LiveReloadMessagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace Elephfront\RoboLiveReload\Tests;

use Elephfront\RoboLiveReload\LiveReloadMessager;
use PHPUnit\Framework\TestCase;
use Ratchet\Server\IoConnection;

class LiveReloadMessagerTest extends TestCase
{

/**
* Test the LiveReloadMessager::onOpen method
*
* @return void
*/
public function testOnOpen()
{
$messager = $this
->getMockBuilder(LiveReloadMessager::class)
->setMethods(['output'])
->getMock();

$this->sock = $this
->getMockBuilder('\\React\\Socket\\ConnectionInterface')
->getMock();

$messager
->expects($this->once())
->method('output')
->with('New connection to the LiveReload server');

$messager->onOpen(new IoConnection($this->sock));
}

/**
* Test the LiveReloadMessager::onMessage method
*
* @return void
*/
public function testOnMessage()
{
$messager = $this
->getMockBuilder(LiveReloadMessager::class)
->setMethods(['output'])
->getMock();

$this->sock = $this
->getMockBuilder('\\React\\Socket\\ConnectionInterface')
->getMock();

$messager
->expects($this->at(2))
->method('output')
->with('Sending message "Hello" to 1 other connection');

$connection = $this
->getMockBuilder('\\Ratchet\\Server\\IoConnection')
->setConstructorArgs([$this->sock])
->setMethods(['send'])
->getMock();

$connection
->expects($this->once())
->method('send')
->with('Hello');

$messager->onOpen(new IoConnection($this->sock));
$messager->onOpen($connection);
$messager->onMessage(new IoConnection($this->sock), 'Hello');
}

/**
* Test the LiveReloadMessager::onClose method
*
* @return void
*/
public function testOnClose()
{
$messager = $this
->getMockBuilder(LiveReloadMessager::class)
->setMethods(['output'])
->getMock();

$this->sock = $this
->getMockBuilder('\\React\\Socket\\ConnectionInterface')
->getMock();

$messager
->expects($this->once())
->method('output')
->with('Connection has disconnected');

$messager->onClose(new IoConnection($this->sock));
}

/**
* Test the LiveReloadMessager::onError method
*
* @return void
*/
public function testOnError()
{
$messager = $this
->getMockBuilder(LiveReloadMessager::class)
->setMethods(['output'])
->getMock();

$this->sock = $this
->getMockBuilder('\\React\\Socket\\ConnectionInterface')
->getMock();

$messager
->expects($this->once())
->method('output')
->with('An error has occurred: `An error`');

$messager->onError(new IoConnection($this->sock), new \Exception('An error'));
}
}

0 comments on commit 399453d

Please sign in to comment.