Skip to content

Commit bc4cd00

Browse files
authored
Merge pull request #41 from Jean85/update-phpunit
Fix CI and update PHPUnit
2 parents e8bc756 + 49862a0 commit bc4cd00

File tree

3 files changed

+106
-82
lines changed

3 files changed

+106
-82
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
language: php
22

3+
cache:
4+
directories:
5+
- $HOME/.composer/cache/files
6+
37
php:
48
- 7.2
59
- 7.1
@@ -8,3 +12,6 @@ php:
812

913
before_script:
1014
- COMPOSER_ROOT_VERSION=dev-master composer --prefer-dist install
15+
16+
script:
17+
- vendor/bin/phpunit -v

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
"symfony/console": "3.*|4.*",
2020
"symfony/dependency-injection": "3.*|4.*"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^5.4.3|^6"
24+
},
2225
"suggest": {
2326
"symfony/filesystem": "If you can't use Upstart or systemd"
2427
},
2528
"autoload": {
2629
"psr-0": { "Wrep\\Daemonizable\\": "src/" }
30+
},
31+
"autoload-dev": {
32+
"psr-4": { "Tests\\Wrep\\Daemonizable\\": "tests/" }
2733
}
2834
}
Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,99 @@
11
<?php
22

3-
namespace Wrep\Notificato\Tests;
3+
namespace Tests\Wrep\Daemonizable\Command;
44

5-
use \Wrep\Daemonizable\Command\EndlessCommand;
5+
use PHPUnit\Framework\TestCase;
6+
use Wrep\Daemonizable\Command\EndlessCommand;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
69

7-
class EndlessCommandTest extends \PHPUnit_Framework_TestCase
10+
class EndlessCommandTest extends TestCase
811
{
9-
private $endlessCommand;
10-
11-
public function setUp()
12-
{
13-
$this->endlessCommand = $this->getMockForAbstractClass('\Wrep\Daemonizable\Command\EndlessCommand', array('phpunit:endlesscommand:test'));
14-
}
15-
16-
/**
17-
* @dataProvider legalTimeouts
18-
*/
19-
public function testTimeout($timeout)
20-
{
21-
$this->assertEquals(EndlessCommand::DEFAULT_TIMEOUT, $this->endlessCommand->getTimeout(), 'Default timeout not used');
22-
23-
$this->endlessCommand->setTimeout($timeout);
24-
$this->assertEquals($timeout, $this->endlessCommand->getTimeout(), 'Timeout change did not persist');
25-
}
26-
27-
public function legalTimeouts()
28-
{
29-
return array(
30-
array(0.5),
31-
array(0),
32-
array(1),
33-
array('1'),
34-
);
35-
}
36-
37-
/**
38-
* @dataProvider illegalTimeouts
39-
*/
40-
public function testIllegalTimeout($timeout)
41-
{
42-
$this->setExpectedException('InvalidArgumentException', 'Invalid timeout provided to Command::setTimeout.');
43-
$this->endlessCommand->setTimeout($timeout);
44-
}
45-
46-
public function illegalTimeouts()
47-
{
48-
return array(
49-
array(-0.5),
50-
array(-1),
51-
array('-1'),
52-
array('just a random string'),
53-
);
54-
}
55-
56-
public function testReturnCode()
57-
{
58-
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Inital return code not zero');
59-
60-
$this->endlessCommand->setReturnCode(9);
61-
$this->assertEquals(9, $this->endlessCommand->getReturnCode(), 'Return code change did not persist');
62-
63-
$this->endlessCommand->setReturnCode(0);
64-
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Return code back to zero did not persist');
65-
}
66-
67-
/**
68-
* Execute a command, that will recieve a sigterm and needs to call
69-
* the handleSignal Method outside the EndlessCommand Class.
70-
*/
71-
public function testInterruptSigtermFromDifferentContext()
72-
{
73-
$cmd = $this->getMock(
74-
'\Wrep\Daemonizable\Command\EndlessCommand',
75-
array('execute'),
76-
array('name' => 'Foo')
77-
);
78-
$cmd->expects($this->any())->method('execute')->will($this->returnCallback(
79-
function() {
80-
// Sending signal to own process, will fail if handleSignal($signal) is private.
81-
posix_kill(posix_getpid(), SIGTERM);
82-
}
83-
));
84-
$input = $this->getMock('\Symfony\Component\Console\Input\InputInterface');
85-
$output = $this->getMock('\Symfony\Component\Console\Output\OutputInterface');
86-
$cmd->run($input, $output);
87-
}
12+
private $endlessCommand;
13+
14+
public function setUp()
15+
{
16+
$this->endlessCommand = $this->getMockForAbstractClass(EndlessCommand::class, ['phpunit:endlesscommand:test']);
17+
}
18+
19+
/**
20+
* @dataProvider legalTimeouts
21+
*/
22+
public function testTimeout($timeout)
23+
{
24+
$this->assertEquals(EndlessCommand::DEFAULT_TIMEOUT, $this->endlessCommand->getTimeout(),
25+
'Default timeout not used');
26+
27+
$this->endlessCommand->setTimeout($timeout);
28+
$this->assertEquals($timeout, $this->endlessCommand->getTimeout(), 'Timeout change did not persist');
29+
}
30+
31+
public function legalTimeouts()
32+
{
33+
return [
34+
[0.5],
35+
[0],
36+
[1],
37+
['1'],
38+
];
39+
}
40+
41+
/**
42+
* @dataProvider illegalTimeouts
43+
*/
44+
public function testIllegalTimeout($timeout)
45+
{
46+
$this->expectException(\InvalidArgumentException::class);
47+
$this->expectExceptionMessage('Invalid timeout provided to Command::setTimeout.');
48+
49+
$this->endlessCommand->setTimeout($timeout);
50+
}
51+
52+
public function illegalTimeouts()
53+
{
54+
return [
55+
[-0.5],
56+
[-1],
57+
['-1'],
58+
['just a random string'],
59+
];
60+
}
61+
62+
public function testReturnCode()
63+
{
64+
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Inital return code not zero');
65+
66+
$this->endlessCommand->setReturnCode(9);
67+
$this->assertEquals(9, $this->endlessCommand->getReturnCode(), 'Return code change did not persist');
68+
69+
$this->endlessCommand->setReturnCode(0);
70+
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Return code back to zero did not persist');
71+
}
72+
73+
/**
74+
* Execute a command, that will receive a sigterm and needs to call
75+
* the handleSignal Method outside the EndlessCommand Class.
76+
*/
77+
public function testInterruptSigtermFromDifferentContext()
78+
{
79+
$cmd = new EndlessSelfTerminatingCommand();
80+
81+
$input = $this->createMock(InputInterface::class);
82+
$output = $this->createMock(OutputInterface::class);
83+
84+
$this->assertSame(0, $cmd->run($input, $output), 'Default exit code is not 0');
85+
}
86+
}
87+
88+
/**
89+
* Class EndlessSelfTerminatingCommand
90+
* @package Tests\Wrep\Daemonizable\Command
91+
* @internal for testing purposes only
92+
*/
93+
class EndlessSelfTerminatingCommand extends EndlessCommand
94+
{
95+
protected function execute(InputInterface $input, OutputInterface $output)
96+
{
97+
posix_kill(posix_getpid(), SIGTERM);
98+
}
8899
}

0 commit comments

Comments
 (0)