diff --git a/phpunit.xml b/phpunit.xml index 7219ee8..913cbff 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -18,5 +18,8 @@ + + + diff --git a/tests/App/Kernel.php b/tests/App/Kernel.php index ec43650..0455c9a 100644 --- a/tests/App/Kernel.php +++ b/tests/App/Kernel.php @@ -48,9 +48,25 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'processes' => 1, ], ], + 'tasks' => [ + [ + 'command' => 'app:test_task', + 'schedule' => '1 second', + ], + ], + 'processes' => [ + [ + 'command' => 'app:test_process', + ], + ], ]); + $container->setParameter('task_status_file', '%kernel.project_dir%/var/task_status.log'); + $container->setParameter('process_status_file', '%kernel.project_dir%/var/process_status.log'); + $container->autowire(RequestTestController::class)->setAutoconfigured(true); + $container->autowire(TestTask::class)->setAutoconfigured(true); + $container->autowire(TestProcess::class)->setAutoconfigured(true); }); } diff --git a/tests/App/TestProcess.php b/tests/App/TestProcess.php index a87c0b0..ebe88d8 100644 --- a/tests/App/TestProcess.php +++ b/tests/App/TestProcess.php @@ -4,21 +4,30 @@ namespace Luzrain\PhpRunnerBundle\Test\App; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; -//#[AsProcess(name: 'Test process')] -final class TestProcess +#[AsCommand( + name: 'app:test_process', + description: 'test process', +)] +final class TestProcess extends Command { public function __construct( - #[Autowire(value: '%kernel.project_dir%/var/process_status.log')] + #[Autowire(param: 'process_status_file')] private string $statusFile, ) { + parent::__construct(); } - public function __invoke(): void + protected function execute(InputInterface $input, OutputInterface $output): int { \file_put_contents($this->statusFile, \time()); - \sleep(1); - exit; + \sleep(300); + + return Command::SUCCESS; } } diff --git a/tests/App/TestTask.php b/tests/App/TestTask.php index e5fbf1e..462a905 100644 --- a/tests/App/TestTask.php +++ b/tests/App/TestTask.php @@ -4,19 +4,29 @@ namespace Luzrain\PhpRunnerBundle\Test\App; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; -//#[AsTask(name: 'Test task', schedule: '1 second')] -final class TestTask +#[AsCommand( + name: 'app:test_task', + description: 'test task', +)] +final class TestTask extends Command { public function __construct( - #[Autowire(value: '%kernel.project_dir%/var/task_status.log')] + #[Autowire(param: 'task_status_file')] private string $statusFile, ) { + parent::__construct(); } - public function __invoke(): void + protected function execute(InputInterface $input, OutputInterface $output): int { \file_put_contents($this->statusFile, \time()); + + return Command::SUCCESS; } } diff --git a/tests/App/bootstrap.php b/tests/App/bootstrap.php index b4cc039..bb74b0b 100644 --- a/tests/App/bootstrap.php +++ b/tests/App/bootstrap.php @@ -6,6 +6,11 @@ \startServer(); \register_shutdown_function(stopServer(...)); +\register_shutdown_function(static function () { + if (\is_dir($dir = \realpath(__DIR__ . '/../../var/cache/test'))) { + \exec("rm -rf $dir"); + } +}); function startServer(): void { diff --git a/tests/App/index.php b/tests/App/index.php index 6eda6c0..9070171 100644 --- a/tests/App/index.php +++ b/tests/App/index.php @@ -6,9 +6,6 @@ require_once \dirname(__DIR__, 2) . '/vendor/autoload_runtime.php'; -$_SERVER['APP_RUNTIME'] = 'Luzrain\PhpRunnerBundle\Runtime'; -$_SERVER['SHELL_VERBOSITY'] = -1; - return function (array $context) { return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); }; diff --git a/tests/ProcessTest.php b/tests/ProcessTest.php new file mode 100644 index 0000000..36a4cc7 --- /dev/null +++ b/tests/ProcessTest.php @@ -0,0 +1,40 @@ +getContainer()->getParameter('process_status_file'))) { + \unlink($file); + } + } + + public function testProcessIsLive(): void + { + $content = $this->getProcessStatusFileContent() ?? $this->fail('Process status file is not found'); + + $this->assertTrue((int) $content > \time() - 4, 'Process started more than 4 seconds ago'); + } + + private function getProcessStatusFileContent(): string|null + { + $file = $this->getContainer()->getParameter('process_status_file'); + $eventLoop = new StreamSelectDriver(); + $suspension = $eventLoop->getSuspension(); + $eventLoop->delay(3, fn() => $suspension->resume()); + $eventLoop->repeat(0.5, function () use ($file, $suspension) { + if ((\file_exists($file) && $content = @\file_get_contents($file))) { + $suspension->resume($content); + } + }); + + return $suspension->suspend(); + } +} diff --git a/tests/TaskTest.php b/tests/TaskTest.php new file mode 100644 index 0000000..6144d5d --- /dev/null +++ b/tests/TaskTest.php @@ -0,0 +1,40 @@ +getContainer()->getParameter('task_status_file'))) { + \unlink($file); + } + } + + public function testTaskIsRunning(): void + { + $content = $this->getTaskStatusFileContent() ?? $this->fail('Task status file is not found'); + + $this->assertTrue((int) $content > \time() - 4, 'Task was called more than 4 seconds ago'); + } + + private function getTaskStatusFileContent(): string|null + { + $file = $this->getContainer()->getParameter('task_status_file'); + $eventLoop = new StreamSelectDriver(); + $suspension = $eventLoop->getSuspension(); + $eventLoop->delay(3, fn() => $suspension->resume()); + $eventLoop->repeat(0.5, function () use ($file, $suspension) { + if ((\file_exists($file) && $content = @\file_get_contents($file))) { + $suspension->resume($content); + } + }); + + return $suspension->suspend(); + } +}