Skip to content

Commit 52559fc

Browse files
committed
Covering with unit tests
1 parent 3a406ba commit 52559fc

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

build.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<project name="php-gui" basedir="." default="main">
33
<property name="examples" value="examples" />
44
<property name="source" value="src" />
5+
<property name="test" value="test" />
56
<property name="bindir" value="bin" />
67

78
<target name="main" description="Start analyzing our application">
@@ -30,6 +31,7 @@
3031
<target name="phpcs" description="Coding Standards Analysis">
3132
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${source}" checkreturn="true" />
3233
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${examples}" checkreturn="true" />
34+
<exec passthru="true" command="${bindir}/phpcs --standard=PSR2 ${test}" checkreturn="true" />
3335
</target>
3436

3537
<target name="generatedoc" description="Generate Doc">

test/ApplicationTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Test;
44

55
use Gui\Application;
6+
use Gui\Components\Window;
67

78
class ApplicationTest extends \PHPUnit_Framework_TestCase
89
{
@@ -15,4 +16,65 @@ public function testGetNextObjectId()
1516
$this->assertEquals(2, $application->getNextObjectId());
1617
$this->assertEquals(3, $application->getNextObjectId());
1718
}
19+
20+
public function testGetWindow()
21+
{
22+
$application = new Application();
23+
24+
$this->assertInstanceOf('Gui\Components\Window', $application->getWindow());
25+
}
26+
27+
public function testGetLoop()
28+
{
29+
$application = new Application();
30+
31+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $application->getLoop());
32+
}
33+
34+
public function testPing()
35+
{
36+
$mock = $this->getMockBuilder('Gui\Application')
37+
->setMethods(['waitCommand'])
38+
->getMock();
39+
40+
$mock->expects($this->once())
41+
->method('waitCommand')
42+
->willReturn(null);
43+
44+
$this->assertTrue(is_float($mock->ping()));
45+
}
46+
47+
public function testAddObject()
48+
{
49+
$application = new Application();
50+
51+
$this->assertNull($application->getObject(1));
52+
53+
$application->addObject(new Window([], null, $application));
54+
55+
$this->assertInstanceOf('Gui\Components\Window', $application->getObject(1));
56+
}
57+
58+
public function testOnAndFire()
59+
{
60+
$application = new Application();
61+
62+
$bar = 0;
63+
$application->on('foo', function () use (&$bar) {
64+
$bar++;
65+
});
66+
67+
$application->fire('foo');
68+
69+
$this->assertEquals(1, $bar);
70+
}
71+
72+
public function testGetAndSetVerboseLevel()
73+
{
74+
$application = new Application();
75+
76+
$this->assertEquals(2, $application->getVerboseLevel());
77+
$application->setVerboseLevel(1);
78+
$this->assertEquals(1, $application->getVerboseLevel());
79+
}
1880
}

test/ColorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Test;
4+
5+
use Gui\Color;
6+
7+
class ColorTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testColorToLazarus()
10+
{
11+
$lazarusColor = Color::toLazarus('#112233');
12+
13+
$this->assertTrue(is_int($lazarusColor));
14+
$this->assertEquals(3351057, $lazarusColor);
15+
16+
$lazarusColor = Color::toLazarus('112233');
17+
$this->assertTrue(is_int($lazarusColor));
18+
$this->assertEquals(3351057, $lazarusColor);
19+
}
20+
}

test/Components/ObjectTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Test\Components;
4+
5+
use Gui\Application;
6+
use Gui\Components\Window;
7+
8+
class ObjectTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testMagicCall()
11+
{
12+
$mock = $this->getMockForAbstractClass(
13+
'Gui\Components\Object',
14+
[
15+
[],
16+
null,
17+
new Application()
18+
]
19+
);
20+
21+
$mock->expects($this->any())
22+
->method('__call');
23+
24+
25+
$mock->setFoo(1);
26+
$this->assertEquals(1, $mock->getFoo());
27+
}
28+
29+
public function testOnAndFire()
30+
{
31+
$appMock = $this->getMockBuilder('Gui\Application')
32+
->setMethods(['sendCommand'])
33+
->getMock();
34+
35+
$appMock->expects($this->any())
36+
->method('sendCommand');
37+
38+
$mock = $this->getMockForAbstractClass(
39+
'Gui\Components\Object',
40+
[
41+
[],
42+
null,
43+
$appMock
44+
]
45+
);
46+
47+
$bar = 0;
48+
$mock->on('foo', function () use (&$bar) {
49+
$bar++;
50+
});
51+
52+
$mock->fire('onfoo');
53+
54+
$this->assertEquals(1, $bar);
55+
}
56+
57+
public function testGetLazarusClass()
58+
{
59+
$mock = $this->getMockForAbstractClass(
60+
'Gui\Components\Object',
61+
[
62+
[],
63+
null,
64+
new Application
65+
]
66+
);
67+
68+
$this->assertEquals('TObject', $mock->getLazarusClass());
69+
}
70+
71+
public function testGetLazarusObjectId()
72+
{
73+
$mock = $this->getMockForAbstractClass(
74+
'Gui\Components\Object',
75+
[
76+
[],
77+
null,
78+
new Application
79+
]
80+
);
81+
82+
$this->assertEquals(1, $mock->getLazarusObjectId());
83+
}
84+
}

test/Ipc/CommandMessageTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Test\Ipc;
4+
5+
use Gui\Ipc\CommandMessage;
6+
7+
class CommandMessageTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testConstructor()
10+
{
11+
$method = 'method';
12+
$params = ['param1' => 'param1'];
13+
$foo = 0;
14+
$msg = new CommandMessage(
15+
$method,
16+
$params,
17+
function () use (&$foo) {
18+
$foo++;
19+
}
20+
);
21+
22+
$this->assertEquals($msg->method, $method);
23+
$this->assertEquals($msg->params, $params);
24+
$callback = $msg->callback;
25+
$callback();
26+
$this->assertEquals(1, $foo);
27+
$this->assertInstanceOf('Gui\Ipc\MessageInterface', $msg);
28+
}
29+
}

test/Ipc/EventMessageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Test\Ipc;
4+
5+
use Gui\Ipc\CommandMessage;
6+
7+
class EventMessageTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testConstructor()
10+
{
11+
$method = 'method';
12+
$params = ['param1' => 'param1'];
13+
$foo = 0;
14+
$event = new CommandMessage(
15+
$method,
16+
$params
17+
);
18+
19+
$this->assertEquals($event->method, $method);
20+
$this->assertEquals($event->params, $params);
21+
$this->assertInstanceOf('Gui\Ipc\MessageInterface', $event);
22+
}
23+
}

test/Ipc/ReceiverTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Test\Ipc;
4+
5+
use Gui\Application;
6+
use Gui\Ipc\Receiver;
7+
8+
class ReceiverTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testConstructor()
11+
{
12+
$receiver = new Receiver(new Application());
13+
14+
$this->assertInstanceOf('Gui\Application', $receiver->application);
15+
}
16+
17+
public function testAddMessageCallback()
18+
{
19+
$receiver = new Receiver(new Application());
20+
21+
$foo = 0;
22+
$receiver->addMessageCallback(
23+
1,
24+
function () use (&$foo) {
25+
$foo++;
26+
}
27+
);
28+
29+
$this->assertTrue(is_array($receiver->messageCallbacks));
30+
$receiver->messageCallbacks[1]();
31+
$this->assertEquals(1, $foo);
32+
}
33+
34+
public function testCallObjectEventListener()
35+
{
36+
$application = new Application();
37+
38+
$obj = $this->getMockBuilder('Gui\Components\Window')
39+
->setConstructorArgs([[], null, $application])
40+
->setMethods(['fire'])
41+
->getMock();
42+
43+
$obj->expects($this->once())
44+
->method('fire')
45+
->with('foo');
46+
47+
$application->addObject($obj);
48+
49+
$receiver = new Receiver($application);
50+
51+
$receiver->callObjectEventListener(1, 'foo');
52+
}
53+
54+
public function testCallMessageCallback()
55+
{
56+
$receiver = new Receiver(new Application());
57+
58+
$foo = null;
59+
$receiver->addMessageCallback(
60+
1,
61+
function ($result) use (&$foo) {
62+
$foo = $result;
63+
}
64+
);
65+
66+
$receiver->callMessageCallback(1, 1);
67+
$this->assertEquals(1, $foo);
68+
}
69+
}

test/Ipc/SenderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Test\Ipc;
4+
5+
use Gui\Application;
6+
use Gui\Ipc\Receiver;
7+
use Gui\Ipc\Sender;
8+
9+
class SenderTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testConstructor()
12+
{
13+
$application = new Application();
14+
$receiver = new Receiver($application);
15+
16+
$sender = new Sender($application, $receiver);
17+
18+
$this->assertInstanceOf('Gui\Application', $sender->application);
19+
$this->assertInstanceOf('Gui\Ipc\Receiver', $sender->receiver);
20+
}
21+
}

0 commit comments

Comments
 (0)