From 90b03db67cc56c2d11c8c380ad7076f08b292003 Mon Sep 17 00:00:00 2001 From: Pierre Foresi Date: Mon, 27 May 2024 21:20:35 +0200 Subject: [PATCH] Chore/compatibilty symfo7 (#41) * Update to symfony 7 --- .github/workflows/ci.yml | 7 ++- composer.json | 6 +-- src/Command/DaemonCommand.php | 45 +++++++++---------- src/DependencyInjection/Configuration.php | 2 +- .../M6WebDaemonExtension.php | 3 +- src/Event/AbstractDaemonEvent.php | 4 +- .../Command/DaemonCommandConcrete.php | 4 +- ...DaemonCommandConcreteIterationCallback.php | 10 +++-- .../DaemonCommandConcreteThrowException.php | 6 +-- ...aemonCommandConcreteThrowStopException.php | 6 ++- tests/Unit/Command/DaemonCommandTest.php | 23 +++++----- 11 files changed, 61 insertions(+), 55 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c43aa8f..ea4ad9d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/composer.json b/composer.json index 5bd17db..60eede1 100644 --- a/composer.json +++ b/composer.json @@ -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" : { diff --git a/src/Command/DaemonCommand.php b/src/Command/DaemonCommand.php index 1a8d486..b082e28 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -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; @@ -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 { @@ -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 { @@ -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()) { @@ -350,9 +350,6 @@ public function getShutdownOnException(): bool return $this->shutdownOnException; } - /** - * @param bool $v value - */ public function setShutdownOnException(bool $shutdownOnException): DaemonCommand { $this->shutdownOnException = $shutdownOnException; @@ -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 diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 4fe9133..491583e 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -13,7 +13,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritdoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('m6_web_daemon'); diff --git a/src/DependencyInjection/M6WebDaemonExtension.php b/src/DependencyInjection/M6WebDaemonExtension.php index dd6ad90..fe49d75 100644 --- a/src/DependencyInjection/M6WebDaemonExtension.php +++ b/src/DependencyInjection/M6WebDaemonExtension.php @@ -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); diff --git a/src/Event/AbstractDaemonEvent.php b/src/Event/AbstractDaemonEvent.php index 4912d82..25a274e 100644 --- a/src/Event/AbstractDaemonEvent.php +++ b/src/Event/AbstractDaemonEvent.php @@ -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) { diff --git a/tests/Fixtures/Command/DaemonCommandConcrete.php b/tests/Fixtures/Command/DaemonCommandConcrete.php index 54d3bc7..ffab741 100644 --- a/tests/Fixtures/Command/DaemonCommandConcrete.php +++ b/tests/Fixtures/Command/DaemonCommandConcrete.php @@ -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; @@ -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; } } diff --git a/tests/Fixtures/Command/DaemonCommandConcreteIterationCallback.php b/tests/Fixtures/Command/DaemonCommandConcreteIterationCallback.php index f3c37dd..9ba9990 100644 --- a/tests/Fixtures/Command/DaemonCommandConcreteIterationCallback.php +++ b/tests/Fixtures/Command/DaemonCommandConcreteIterationCallback.php @@ -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') @@ -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 diff --git a/tests/Fixtures/Command/DaemonCommandConcreteThrowException.php b/tests/Fixtures/Command/DaemonCommandConcreteThrowException.php index b421664..913e80f 100644 --- a/tests/Fixtures/Command/DaemonCommandConcreteThrowException.php +++ b/tests/Fixtures/Command/DaemonCommandConcreteThrowException.php @@ -8,7 +8,7 @@ class DaemonCommandConcreteThrowException extends DaemonCommand { - public static $exceptionMessage = null; + public static ?string $exceptionMessage = null; protected function configure(): void { @@ -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); } diff --git a/tests/Fixtures/Command/DaemonCommandConcreteThrowStopException.php b/tests/Fixtures/Command/DaemonCommandConcreteThrowStopException.php index 7eead2f..f1081c6 100644 --- a/tests/Fixtures/Command/DaemonCommandConcreteThrowStopException.php +++ b/tests/Fixtures/Command/DaemonCommandConcreteThrowStopException.php @@ -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; @@ -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 { @@ -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; } } diff --git a/tests/Unit/Command/DaemonCommandTest.php b/tests/Unit/Command/DaemonCommandTest.php index c42eb58..7282555 100644 --- a/tests/Unit/Command/DaemonCommandTest.php +++ b/tests/Unit/Command/DaemonCommandTest.php @@ -1,6 +1,6 @@ eventDispatcher = $this->createMock(EventDispatcherInterface::class); - $this->loop = Factory::create(); + $this->loop = Loop::get(); } public function tearDown(): void @@ -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 */ @@ -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()); } /** @@ -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()); } @@ -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());