Skip to content

Commit 0970385

Browse files
func0dericanhazstring
authored andcommitted
Re-arrange tests to differenciate between integration and unit tests. (#4)
* Re-arrange tests to differenciate between integration and unit tests. * Split unit tests in unit and integration tests and clean them up a little. * Fix AbstractUnit to handle service names with suffixes in them correctly while instance name determination * Add dedicated namespace for tests in composer.json. * Add AbstractUnitTest * Remove obsolete data provider
1 parent 6b79594 commit 0970385

9 files changed

Lines changed: 292 additions & 81 deletions

File tree

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
"autoload": {
1414
"psr-4": {
1515
"SystemCtl\\": [
16-
"src/",
16+
"src/"
17+
]
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"SystemCtl\\Tests\\": [
1723
"tests/"
1824
]
1925
}

phpunit.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
bootstrap="./vendor/autoload.php"
1313
>
1414
<testsuites>
15-
<testsuite name="unittests">
16-
<directory>./tests/</directory>
15+
<testsuite name="unit-tests">
16+
<directory>./tests/Unit</directory>
17+
</testsuite>
18+
<testsuite name="integration-tests">
19+
<directory>./tests/Integration</directory>
1720
</testsuite>
1821
</testsuites>
1922

@@ -26,4 +29,4 @@
2629
</whitelist>
2730
</filter>
2831

29-
</phpunit>
32+
</phpunit>

src/Unit/AbstractUnit.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ public function isMultiInstance(): bool
4242

4343
/**
4444
* @inheritDoc
45+
* @todo: Everything in here should happen inside the constructor and stored
46+
* afterwards.
4547
*/
4648
public function getInstanceName(): ?string
4749
{
48-
$parts = explode('@', $this->name);
49-
return $parts[1] ?? null;
50+
$instanceName = explode('@', $this->name, 2)[1] ?? null;
51+
52+
if (is_string($instanceName) && strpos($instanceName, '.') !== false) {
53+
$instanceName = explode('.', $instanceName, 2)[0];
54+
}
55+
56+
return $instanceName;
5057
}
5158

5259
/**
Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace SystemCtl\Test;
3+
namespace SystemCtl\Test\Integration;
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Process\Process;
@@ -13,36 +13,6 @@
1313

1414
class SystemCtlTest extends TestCase
1515
{
16-
/**
17-
* @param string $output
18-
* @return \PHPUnit_Framework_MockObject_MockObject|SystemCtl
19-
*/
20-
private function buildSystemCtlMock($output)
21-
{
22-
23-
$process = $this->getMockBuilder(Process::class)
24-
->disableOriginalConstructor()
25-
->setMethods(['getOutput'])
26-
->getMock();
27-
28-
$process->method('getOutput')->willReturn($output);
29-
30-
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
31-
->disableOriginalConstructor()
32-
->setMethods(['getProcess'])
33-
->getMock();
34-
35-
$processBuilder->method('getProcess')->willReturn($process);
36-
37-
$systemctl = $this->getMockBuilder(SystemCtl::class)
38-
->setMethods(['getProcessBuilder'])
39-
->getMock();
40-
41-
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
42-
43-
return $systemctl;
44-
}
45-
4616
public function testListUnitsWithAvailableUnits()
4717
{
4818
$output = <<<EOT
@@ -55,7 +25,7 @@ public function testListUnitsWithAvailableUnits()
5525
systemd-ask-password-wall.path loaded active waiting
5626
acpid.service loaded active running
5727
beanstalkd.service loaded active running
58-
console-setup.service loaded active exited
28+
console-setup.service loaded active exited
5929
cron.service loaded active running
6030
EOT;
6131
$systemctl = $this->buildSystemCtlMock($output);
@@ -75,7 +45,7 @@ public function testListUnitsWithSupportedUnits()
7545
systemd-ask-password-wall.path loaded active waiting
7646
acpid.service loaded active running
7747
beanstalkd.service loaded active running
78-
console-setup.service loaded active exited
48+
console-setup.service loaded active exited
7949
cron.service loaded active running
8050
EOT;
8151
$systemctl = $this->buildSystemCtlMock($output);
@@ -97,16 +67,6 @@ public function testCreateUnitFromUnsupportedSuffixShouldRaiseException()
9767
SystemCtl::unitFromSuffix('unsupported', 'FailUnit');
9868
}
9969

100-
public function testGetServiceWithName()
101-
{
102-
$output = 'testService.service Active Running';
103-
$systemctl = $this->buildSystemCtlMock($output);
104-
105-
$service = $systemctl->getService('testService');
106-
$this->assertInstanceOf(Service::class, $service);
107-
$this->assertEquals('testService', $service->getName());
108-
}
109-
11070
public function testGetServices()
11171
{
11272
$output = <<<EOT
@@ -115,7 +75,7 @@ public function testGetServices()
11575
awesomeservice.service Active running
11676
nonservice.timer Active running
11777
PLACEHOLDER STUFF
118-
78+
11979
EOT;
12080

12181
$systemctl = $this->buildSystemCtlMock($output);
@@ -124,15 +84,6 @@ public function testGetServices()
12484
$this->assertCount(2, $services);
12585
}
12686

127-
public function testGetTimerWithName()
128-
{
129-
$systemctl = new SystemCtl();
130-
131-
$timer = $systemctl->getTimer('testTimer');
132-
$this->assertInstanceOf(Timer::class, $timer);
133-
$this->assertEquals('testTimer', $timer->getName());
134-
}
135-
13687
public function testGetTimers()
13788
{
13889
$output = <<<EOT
@@ -161,4 +112,33 @@ public function testSetBinaryShouldChangeCommand()
161112
$processBuilder = $systemCtl->getProcessBuilder();
162113
$this->assertEquals("'/usr/sbin/systemctl'", $processBuilder->getProcess()->getCommandLine());
163114
}
115+
116+
/**
117+
* @param string $output
118+
* @return \PHPUnit_Framework_MockObject_MockObject|SystemCtl
119+
*/
120+
private function buildSystemCtlMock($output)
121+
{
122+
$process = $this->getMockBuilder(Process::class)
123+
->disableOriginalConstructor()
124+
->setMethods(['getOutput'])
125+
->getMock();
126+
127+
$process->method('getOutput')->willReturn($output);
128+
129+
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
130+
->disableOriginalConstructor()
131+
->setMethods(['getProcess'])
132+
->getMock();
133+
134+
$processBuilder->method('getProcess')->willReturn($process);
135+
136+
$systemctl = $this->getMockBuilder(SystemCtl::class)
137+
->setMethods(['getProcessBuilder'])
138+
->getMock();
139+
140+
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
141+
142+
return $systemctl;
143+
}
164144
}
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace SystemCtl\Test\Unit;
3+
namespace SystemCtl\Test\Integration\Unit;
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Process\Process;
@@ -131,24 +131,4 @@ public function testTimerCommandsIfProcessIsUnsuccessFulShouldRaiseException()
131131
$this->expectException(CommandFailedException::class);
132132
$timer->start();
133133
}
134-
135-
public function testMultiInstanceUnit()
136-
{
137-
$process = $this->getMockBuilder(Process::class)
138-
->disableOriginalConstructor()
139-
->getMock();
140-
141-
/** @var \PHPUnit_Framework_MockObject_MockObject|ProcessBuilder $processBuilder */
142-
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
143-
->disableOriginalConstructor()
144-
->setMethods(['getProcess'])
145-
->getMock();
146-
147-
$processBuilder->method('getProcess')->willReturn($process);
148-
149-
$unit = new Service('service@1', $processBuilder);
150-
$this->assertEquals('service@1', $unit->getName());
151-
$this->assertTrue($unit->isMultiInstance());
152-
$this->assertEquals('1', $unit->getInstanceName());
153-
}
154134
}

tests/Unit/SystemCtlTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace SystemCtl\Test\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Process\Process;
7+
use Symfony\Component\Process\ProcessBuilder;
8+
use SystemCtl\Exception\UnitTypeNotSupportedException;
9+
use SystemCtl\Unit\Service;
10+
use SystemCtl\SystemCtl;
11+
use SystemCtl\Unit\Timer;
12+
use SystemCtl\Unit\UnitInterface;
13+
14+
class SystemCtlTest extends TestCase
15+
{
16+
/**
17+
* @param string $output
18+
* @return \PHPUnit_Framework_MockObject_MockObject|SystemCtl
19+
*/
20+
private function buildSystemCtlMock($output)
21+
{
22+
23+
$process = $this->getMockBuilder(Process::class)
24+
->disableOriginalConstructor()
25+
->setMethods(['getOutput'])
26+
->getMock();
27+
28+
$process->method('getOutput')->willReturn($output);
29+
30+
$processBuilder = $this->getMockBuilder(ProcessBuilder::class)
31+
->disableOriginalConstructor()
32+
->setMethods(['getProcess'])
33+
->getMock();
34+
35+
$processBuilder->method('getProcess')->willReturn($process);
36+
37+
$systemctl = $this->getMockBuilder(SystemCtl::class)
38+
->setMethods(['getProcessBuilder'])
39+
->getMock();
40+
41+
$systemctl->method('getProcessBuilder')->willReturn($processBuilder);
42+
43+
return $systemctl;
44+
}
45+
46+
public function testGetServiceWithName()
47+
{
48+
$systemctl = new SystemCtl();
49+
50+
$service = $systemctl->getService('testService');
51+
$this->assertInstanceOf(Service::class, $service);
52+
$this->assertEquals('testService', $service->getName());
53+
}
54+
55+
public function testGetTimerWithName()
56+
{
57+
$systemctl = new SystemCtl();
58+
59+
$timer = $systemctl->getTimer('testTimer');
60+
$this->assertInstanceOf(Timer::class, $timer);
61+
$this->assertEquals('testTimer', $timer->getName());
62+
}
63+
}

0 commit comments

Comments
 (0)