|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Wrep\Notificato\Tests; |
| 3 | +namespace Tests\Wrep\Daemonizable\Command; |
4 | 4 |
|
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; |
6 | 9 |
|
7 | | -class EndlessCommandTest extends \PHPUnit_Framework_TestCase |
| 10 | +class EndlessCommandTest extends TestCase |
8 | 11 | { |
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 | + } |
88 | 99 | } |
0 commit comments