Skip to content

Commit

Permalink
Chore/compatibilty symfo7 (#41)
Browse files Browse the repository at this point in the history
* Update to symfony 7
  • Loading branch information
pforesi authored May 27, 2024
1 parent 4d424c0 commit 90b03db
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 55 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
php-version: ['8.1']
symfony-version: ['^6.1']
php-version: ['8.1', '8.2']
symfony-version: ['^6.1', '^7.0']
exclude:
- php-version: '8.1'
symfony-version: '^7.0'
steps:
- uses: actions/checkout@master
- uses: shivammathur/setup-php@v2
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"php" : ">=8.1",
"ext-pcntl" : "*",
"psr/event-dispatcher": ">=1.0",
"symfony/console" : "~6.1",
"symfony/framework-bundle" : "~6.1",
"symfony/yaml": "~6.1",
"symfony/console" : "~6.1||~7.0",
"symfony/framework-bundle" : "~6.1||~7.0",
"symfony/yaml": "~6.1||~7.0",
"react/event-loop": "@stable"
},
"require-dev" : {
Expand Down
45 changes: 22 additions & 23 deletions src/Command/DaemonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -25,49 +26,49 @@
abstract class DaemonCommand extends Command
{
/** @var bool tells if shutdown is requested */
protected $shutdownRequested = false;
protected bool $shutdownRequested = false;

/** @var int allows the concrete command to setup an exit code */
protected $returnCode = 0;
protected int $returnCode = 0;

/** @var int loop count */
protected $loopCount = 0;
protected int $loopCount = 0;

/** @var int store max loop option value */
protected $loopMax;
/** @var ?int store max loop option value */
protected ?int $loopMax = null;

/** @var int store max memory option value */
protected $memoryMax = 0;
protected int $memoryMax = 0;

/** @var bool store shutdown on exception option value */
protected $shutdownOnException;
protected bool $shutdownOnException;

/** @var bool display or not exception on command output */
protected $showExceptions;
protected bool $showExceptions;

/** @var ?EventDispatcherInterface */
protected $dispatcher;
protected ?EventDispatcherInterface $dispatcher;

/** @var LoopInterface */
protected $loop;
protected LoopInterface $loop;

/** @var callable */
protected $loopCallback;

/** @var \Exception */
protected $lastException;
protected \Exception $lastException;

/** @var float */
protected $startTime;
/** @var ?float */
protected ?float $startTime = null;

/** @var float time in seconds */
protected $nextIterationSleepingTime = 0.0;
protected float $nextIterationSleepingTime = 0.0;

/** @var array */
protected $iterationsEvents = [];
protected array $iterationsEvents = [];

/** @var array */
protected $iterationsIntervalCallbacks = [];
protected array $iterationsIntervalCallbacks = [];

public function setEventDispatcher(EventDispatcherInterface $dispatcher = null): DaemonCommand
{
Expand Down Expand Up @@ -141,9 +142,8 @@ public function getLoop(): LoopInterface
}

/**
* @see \Symfony\Component\Console\Command\Command::run()
*
* @throws \Exception
* @throws ExceptionInterface
* @see Command::run
*/
public function run(InputInterface $input, OutputInterface $output): int
{
Expand Down Expand Up @@ -256,7 +256,7 @@ protected function loop(InputInterface $input, OutputInterface $output): void
$this->dispatchEvent(DaemonLoopExceptionGeneralEvent::class);

if ($this->getShowExceptions()) {
$this->getApplication()->renderThrowable($e, $output);
$this->getApplication()?->renderThrowable($e, $output);
}

if ($this->getShutdownOnException()) {
Expand Down Expand Up @@ -350,9 +350,6 @@ public function getShutdownOnException(): bool
return $this->shutdownOnException;
}

/**
* @param bool $v value
*/
public function setShutdownOnException(bool $shutdownOnException): DaemonCommand
{
$this->shutdownOnException = $shutdownOnException;
Expand Down Expand Up @@ -474,6 +471,8 @@ protected function setLastException(\Exception $exception): DaemonCommand

/**
* Add your own callback after every iteration interval.
*
* @param int $iterationsInterval
* @param callable $onIterationsInterval
*/
public function addIterationsIntervalCallback(int $iterationsInterval, callable $onIterationsInterval): void
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('m6_web_daemon');

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/M6WebDaemonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class M6WebDaemonExtension extends Extension
{
/**
* {@inheritdoc}
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand Down
4 changes: 2 additions & 2 deletions src/Event/AbstractDaemonEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
abstract class AbstractDaemonEvent extends Event
{
/** @var DaemonCommand */
protected $command;
protected DaemonCommand $command;

/** @var float */
protected $executionTime;
protected float $executionTime;

public function __construct(DaemonCommand $command)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/Command/DaemonCommandConcrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace M6Web\Bundle\DaemonBundle\Tests\Fixtures\Command;

use M6Web\Bundle\DaemonBundle\Command\DaemonCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -15,7 +16,8 @@ protected function configure(): void
->setDescription('command for unit test');
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace M6Web\Bundle\DaemonBundle\Tests\Fixtures\Command;

use M6Web\Bundle\DaemonBundle\Command\DaemonCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DaemonCommandConcreteIterationCallback extends DaemonCommand
{
public $countCall = 0;
public $iterationInterval = 5;
public int $countCall = 0;
public int $iterationInterval = 5;

protected function configure()
protected function configure(): void
{
$this
->setName('test:daemontest')
Expand All @@ -23,8 +24,9 @@ protected function setup(InputInterface $input, OutputInterface $output): void
$this->addIterationsIntervalCallback($this->iterationInterval, [$this, 'myCallback']);
}

protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
return Command::SUCCESS;
}

protected function myCallback(InputInterface $input, OutputInterface $output): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class DaemonCommandConcreteThrowException extends DaemonCommand
{
public static $exceptionMessage = null;
public static ?string $exceptionMessage = null;

protected function configure(): void
{
Expand All @@ -20,9 +20,9 @@ protected function configure(): void
/**
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
self::$exceptionMessage = (string) uniqid(mt_rand(), true);
self::$exceptionMessage = uniqid(mt_rand(), true);

throw new \Exception(self::$exceptionMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use M6Web\Bundle\DaemonBundle\Command\DaemonCommand;
use M6Web\Bundle\DaemonBundle\Command\StopLoopException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -16,7 +17,7 @@ class DaemonCommandConcreteThrowStopException extends DaemonCommand
public const EXCEPTION_MESSAGE = 'Stop loop exception';

/** @var int */
private $count = 0;
private int $count = 0;

protected function configure(): void
{
Expand All @@ -28,10 +29,11 @@ protected function configure(): void
/**
* @throws StopLoopException
*/
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (++$this->count >= static::MAX_ITERATION) {
throw new StopLoopException(static::EXCEPTION_MESSAGE);
}
return Command::FAILURE;
}
}
23 changes: 10 additions & 13 deletions tests/Unit/Command/DaemonCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace M6Web\Bundle\DaemonBundle\Tests\Units\Command;
namespace M6Web\Bundle\DaemonBundle\Tests\Unit\Command;

use Exception;
use M6Web\Bundle\DaemonBundle\Command\DaemonCommand;
Expand All @@ -20,26 +20,23 @@
use M6Web\Bundle\DaemonBundle\Tests\Fixtures\Command\DaemonCommandConcreteThrowStopException;
use M6Web\Bundle\DaemonBundle\Tests\Fixtures\Event\EachFiveEvent;
use M6Web\Bundle\DaemonBundle\Tests\Fixtures\Event\EachTenEvent;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class DaemonCommandTest extends TestCase
{
/** @var MockObject|EventDispatcherInterface */
protected $eventDispatcher;
protected ?EventDispatcherInterface $eventDispatcher;

/** @var LoopInterface */
protected $loop;
protected ?LoopInterface $loop;

public function setUp(): void
{
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->loop = Factory::create();
$this->loop = Loop::get();
}

public function tearDown(): void
Expand All @@ -50,7 +47,7 @@ public function tearDown(): void

private function createDaemonCommand(
string $class = DaemonCommandConcrete::class,
EventDispatcherInterface $eventDispatcher = null,
?EventDispatcherInterface $eventDispatcher = null,
array $iterationEvents = []
): DaemonCommand {
/** @var DaemonCommand $command */
Expand Down Expand Up @@ -97,14 +94,14 @@ public function testShutdownRequest(): void

// With
$this->assertIsBool($command->isShutdownRequested());
$this->assertEquals(false, $command->isShutdownRequested());
$this->assertFalse($command->isShutdownRequested());

// When
$command->requestShutdown();

// Then
$this->assertIsBool($command->isShutdownRequested());
$this->assertEquals(true, $command->isShutdownRequested());
$this->assertTrue($command->isShutdownRequested());
}

/**
Expand Down Expand Up @@ -340,7 +337,7 @@ public function testCommandExceptionWithShowException(): void
);

// Then
$this->assertInstanceOf(\Exception::class, $exception = $command->getLastException());
$this->assertInstanceOf(\Exception::class, $command->getLastException());
$this->assertStringContainsString(DaemonCommandConcreteThrowException::$exceptionMessage, $commandTester->getDisplay());
$this->assertStringContainsString('Exception', $commandTester->getDisplay());
}
Expand Down Expand Up @@ -374,7 +371,7 @@ static function ($eventObject) use (&$lastEvent) {
);

// Then
$this->assertInstanceOf(\Exception::class, $exception = $command->getLastException());
$this->assertInstanceOf(\Exception::class, $command->getLastException());
$this->assertStringNotContainsString(DaemonCommandConcreteThrowException::$exceptionMessage, $commandTester->getDisplay());
$this->assertStringNotContainsString('Exception', $commandTester->getDisplay());

Expand Down

0 comments on commit 90b03db

Please sign in to comment.