Skip to content

Fix CI and update PHPUnit #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
language: php

cache:
directories:
- $HOME/.composer/cache/files

php:
- 7.2
- 7.1
@@ -8,3 +12,6 @@ php:

before_script:
- COMPOSER_ROOT_VERSION=dev-master composer --prefer-dist install

script:
- vendor/bin/phpunit -v
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -19,10 +19,16 @@
"symfony/console": "3.*|4.*",
"symfony/dependency-injection": "3.*|4.*"
},
"require-dev": {
"phpunit/phpunit": "^5.4.3|^6"
},
"suggest": {
"symfony/filesystem": "If you can't use Upstart or systemd"
},
"autoload": {
"psr-0": { "Wrep\\Daemonizable\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Tests\\Wrep\\Daemonizable\\": "tests/" }
}
}
175 changes: 93 additions & 82 deletions tests/Wrep/Daemonizable/Command/EndlessCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,99 @@
<?php

namespace Wrep\Notificato\Tests;
namespace Tests\Wrep\Daemonizable\Command;

use \Wrep\Daemonizable\Command\EndlessCommand;
use PHPUnit\Framework\TestCase;
use Wrep\Daemonizable\Command\EndlessCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class EndlessCommandTest extends \PHPUnit_Framework_TestCase
class EndlessCommandTest extends TestCase
{
private $endlessCommand;

public function setUp()
{
$this->endlessCommand = $this->getMockForAbstractClass('\Wrep\Daemonizable\Command\EndlessCommand', array('phpunit:endlesscommand:test'));
}

/**
* @dataProvider legalTimeouts
*/
public function testTimeout($timeout)
{
$this->assertEquals(EndlessCommand::DEFAULT_TIMEOUT, $this->endlessCommand->getTimeout(), 'Default timeout not used');

$this->endlessCommand->setTimeout($timeout);
$this->assertEquals($timeout, $this->endlessCommand->getTimeout(), 'Timeout change did not persist');
}

public function legalTimeouts()
{
return array(
array(0.5),
array(0),
array(1),
array('1'),
);
}

/**
* @dataProvider illegalTimeouts
*/
public function testIllegalTimeout($timeout)
{
$this->setExpectedException('InvalidArgumentException', 'Invalid timeout provided to Command::setTimeout.');
$this->endlessCommand->setTimeout($timeout);
}

public function illegalTimeouts()
{
return array(
array(-0.5),
array(-1),
array('-1'),
array('just a random string'),
);
}

public function testReturnCode()
{
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Inital return code not zero');

$this->endlessCommand->setReturnCode(9);
$this->assertEquals(9, $this->endlessCommand->getReturnCode(), 'Return code change did not persist');

$this->endlessCommand->setReturnCode(0);
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Return code back to zero did not persist');
}

/**
* Execute a command, that will recieve a sigterm and needs to call
* the handleSignal Method outside the EndlessCommand Class.
*/
public function testInterruptSigtermFromDifferentContext()
{
$cmd = $this->getMock(
'\Wrep\Daemonizable\Command\EndlessCommand',
array('execute'),
array('name' => 'Foo')
);
$cmd->expects($this->any())->method('execute')->will($this->returnCallback(
function() {
// Sending signal to own process, will fail if handleSignal($signal) is private.
posix_kill(posix_getpid(), SIGTERM);
}
));
$input = $this->getMock('\Symfony\Component\Console\Input\InputInterface');
$output = $this->getMock('\Symfony\Component\Console\Output\OutputInterface');
$cmd->run($input, $output);
}
private $endlessCommand;

public function setUp()
{
$this->endlessCommand = $this->getMockForAbstractClass(EndlessCommand::class, ['phpunit:endlesscommand:test']);
}

/**
* @dataProvider legalTimeouts
*/
public function testTimeout($timeout)
{
$this->assertEquals(EndlessCommand::DEFAULT_TIMEOUT, $this->endlessCommand->getTimeout(),
'Default timeout not used');

$this->endlessCommand->setTimeout($timeout);
$this->assertEquals($timeout, $this->endlessCommand->getTimeout(), 'Timeout change did not persist');
}

public function legalTimeouts()
{
return [
[0.5],
[0],
[1],
['1'],
];
}

/**
* @dataProvider illegalTimeouts
*/
public function testIllegalTimeout($timeout)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid timeout provided to Command::setTimeout.');

$this->endlessCommand->setTimeout($timeout);
}

public function illegalTimeouts()
{
return [
[-0.5],
[-1],
['-1'],
['just a random string'],
];
}

public function testReturnCode()
{
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Inital return code not zero');

$this->endlessCommand->setReturnCode(9);
$this->assertEquals(9, $this->endlessCommand->getReturnCode(), 'Return code change did not persist');

$this->endlessCommand->setReturnCode(0);
$this->assertEquals(0, $this->endlessCommand->getReturnCode(), 'Return code back to zero did not persist');
}

/**
* Execute a command, that will receive a sigterm and needs to call
* the handleSignal Method outside the EndlessCommand Class.
*/
public function testInterruptSigtermFromDifferentContext()
{
$cmd = new EndlessSelfTerminatingCommand();

$input = $this->createMock(InputInterface::class);
$output = $this->createMock(OutputInterface::class);

$this->assertSame(0, $cmd->run($input, $output), 'Default exit code is not 0');
}
}

/**
* Class EndlessSelfTerminatingCommand
* @package Tests\Wrep\Daemonizable\Command
* @internal for testing purposes only
*/
class EndlessSelfTerminatingCommand extends EndlessCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
posix_kill(posix_getpid(), SIGTERM);
}
}