Skip to content

Commit

Permalink
Covering with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reisraff committed Apr 14, 2016
1 parent 3a406ba commit 52559fc
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<project name="php-gui" basedir="." default="main">
<property name="examples" value="examples" />
<property name="source" value="src" />
<property name="test" value="test" />
<property name="bindir" value="bin" />

<target name="main" description="Start analyzing our application">
Expand Down Expand Up @@ -30,6 +31,7 @@
<target name="phpcs" description="Coding Standards Analysis">
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${source}" checkreturn="true" />
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${examples}" checkreturn="true" />
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${test}" checkreturn="true" />
</target>

<target name="generatedoc" description="Generate Doc">
Expand Down
62 changes: 62 additions & 0 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Test;

use Gui\Application;
use Gui\Components\Window;

class ApplicationTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -15,4 +16,65 @@ public function testGetNextObjectId()
$this->assertEquals(2, $application->getNextObjectId());
$this->assertEquals(3, $application->getNextObjectId());
}

public function testGetWindow()
{
$application = new Application();

$this->assertInstanceOf('Gui\Components\Window', $application->getWindow());
}

public function testGetLoop()
{
$application = new Application();

$this->assertInstanceOf('React\EventLoop\LoopInterface', $application->getLoop());
}

public function testPing()
{
$mock = $this->getMockBuilder('Gui\Application')
->setMethods(['waitCommand'])
->getMock();

$mock->expects($this->once())
->method('waitCommand')
->willReturn(null);

$this->assertTrue(is_float($mock->ping()));
}

public function testAddObject()
{
$application = new Application();

$this->assertNull($application->getObject(1));

$application->addObject(new Window([], null, $application));

$this->assertInstanceOf('Gui\Components\Window', $application->getObject(1));
}

public function testOnAndFire()
{
$application = new Application();

$bar = 0;
$application->on('foo', function () use (&$bar) {
$bar++;
});

$application->fire('foo');

$this->assertEquals(1, $bar);
}

public function testGetAndSetVerboseLevel()
{
$application = new Application();

$this->assertEquals(2, $application->getVerboseLevel());
$application->setVerboseLevel(1);
$this->assertEquals(1, $application->getVerboseLevel());
}
}
20 changes: 20 additions & 0 deletions test/ColorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Test;

use Gui\Color;

class ColorTest extends \PHPUnit_Framework_TestCase
{
public function testColorToLazarus()
{
$lazarusColor = Color::toLazarus('#112233');

$this->assertTrue(is_int($lazarusColor));
$this->assertEquals(3351057, $lazarusColor);

$lazarusColor = Color::toLazarus('112233');
$this->assertTrue(is_int($lazarusColor));
$this->assertEquals(3351057, $lazarusColor);
}
}
84 changes: 84 additions & 0 deletions test/Components/ObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Test\Components;

use Gui\Application;
use Gui\Components\Window;

class ObjectTest extends \PHPUnit_Framework_TestCase
{
public function testMagicCall()
{
$mock = $this->getMockForAbstractClass(
'Gui\Components\Object',
[
[],
null,
new Application()
]
);

$mock->expects($this->any())
->method('__call');


$mock->setFoo(1);
$this->assertEquals(1, $mock->getFoo());
}

public function testOnAndFire()
{
$appMock = $this->getMockBuilder('Gui\Application')
->setMethods(['sendCommand'])
->getMock();

$appMock->expects($this->any())
->method('sendCommand');

$mock = $this->getMockForAbstractClass(
'Gui\Components\Object',
[
[],
null,
$appMock
]
);

$bar = 0;
$mock->on('foo', function () use (&$bar) {
$bar++;
});

$mock->fire('onfoo');

$this->assertEquals(1, $bar);
}

public function testGetLazarusClass()
{
$mock = $this->getMockForAbstractClass(
'Gui\Components\Object',
[
[],
null,
new Application
]
);

$this->assertEquals('TObject', $mock->getLazarusClass());
}

public function testGetLazarusObjectId()
{
$mock = $this->getMockForAbstractClass(
'Gui\Components\Object',
[
[],
null,
new Application
]
);

$this->assertEquals(1, $mock->getLazarusObjectId());
}
}
29 changes: 29 additions & 0 deletions test/Ipc/CommandMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Test\Ipc;

use Gui\Ipc\CommandMessage;

class CommandMessageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$method = 'method';
$params = ['param1' => 'param1'];
$foo = 0;
$msg = new CommandMessage(
$method,
$params,
function () use (&$foo) {
$foo++;
}
);

$this->assertEquals($msg->method, $method);
$this->assertEquals($msg->params, $params);
$callback = $msg->callback;
$callback();
$this->assertEquals(1, $foo);
$this->assertInstanceOf('Gui\Ipc\MessageInterface', $msg);
}
}
23 changes: 23 additions & 0 deletions test/Ipc/EventMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Test\Ipc;

use Gui\Ipc\CommandMessage;

class EventMessageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$method = 'method';
$params = ['param1' => 'param1'];
$foo = 0;
$event = new CommandMessage(
$method,
$params
);

$this->assertEquals($event->method, $method);
$this->assertEquals($event->params, $params);
$this->assertInstanceOf('Gui\Ipc\MessageInterface', $event);
}
}
69 changes: 69 additions & 0 deletions test/Ipc/ReceiverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Test\Ipc;

use Gui\Application;
use Gui\Ipc\Receiver;

class ReceiverTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$receiver = new Receiver(new Application());

$this->assertInstanceOf('Gui\Application', $receiver->application);
}

public function testAddMessageCallback()
{
$receiver = new Receiver(new Application());

$foo = 0;
$receiver->addMessageCallback(
1,
function () use (&$foo) {
$foo++;
}
);

$this->assertTrue(is_array($receiver->messageCallbacks));
$receiver->messageCallbacks[1]();
$this->assertEquals(1, $foo);
}

public function testCallObjectEventListener()
{
$application = new Application();

$obj = $this->getMockBuilder('Gui\Components\Window')
->setConstructorArgs([[], null, $application])
->setMethods(['fire'])
->getMock();

$obj->expects($this->once())
->method('fire')
->with('foo');

$application->addObject($obj);

$receiver = new Receiver($application);

$receiver->callObjectEventListener(1, 'foo');
}

public function testCallMessageCallback()
{
$receiver = new Receiver(new Application());

$foo = null;
$receiver->addMessageCallback(
1,
function ($result) use (&$foo) {
$foo = $result;
}
);

$receiver->callMessageCallback(1, 1);
$this->assertEquals(1, $foo);
}
}
21 changes: 21 additions & 0 deletions test/Ipc/SenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Test\Ipc;

use Gui\Application;
use Gui\Ipc\Receiver;
use Gui\Ipc\Sender;

class SenderTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$application = new Application();
$receiver = new Receiver($application);

$sender = new Sender($application, $receiver);

$this->assertInstanceOf('Gui\Application', $sender->application);
$this->assertInstanceOf('Gui\Ipc\Receiver', $sender->receiver);
}
}

0 comments on commit 52559fc

Please sign in to comment.