diff --git a/composer.json b/composer.json index 18c540a..8f2c76e 100644 --- a/composer.json +++ b/composer.json @@ -72,6 +72,7 @@ "phpunit/phpunit": "^10.5 || ^11 || ^12 || ^13", "rector/rector": "*", "roave/security-advisories": "dev-latest", + "symfony/framework-bundle": "^6.4 || ^7.4 || ^8", "symfony/test-pack": "^1.1" }, "config": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6bdb884..9e8b688 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,20 +1,22 @@ + failOnWarning="true" + failOnDeprecation="true" + displayDetailsOnTestsThatTriggerDeprecations="true"> tests - + src diff --git a/rector.php b/rector.php index a601b30..68fbff9 100644 --- a/rector.php +++ b/rector.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector; use Rector\Symfony\Set\SymfonySetList; use Rector\ValueObject\PhpVersion; @@ -20,4 +21,8 @@ ->withSets([ SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION, ]) + ->withSkip([ + // Conditions on the Symfony version: always true with the installed version only + RemoveAlwaysTrueIfConditionRector::class => [__DIR__.'/tests/App/TestKernel.php'], + ]) ; diff --git a/tests/App/TestKernel.php b/tests/App/TestKernel.php new file mode 100644 index 0000000..a5f2fde --- /dev/null +++ b/tests/App/TestKernel.php @@ -0,0 +1,108 @@ +getRunDir().'/cache/'.$this->environment; + } + + public function getLogDir(): string + { + return $this->getRunDir().'/logs'; + } + + protected function configureContainer(ContainerConfigurator $container): void + { + $container->extension('framework', [ + 'secret' => 'test', + 'test' => true, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'validation' => ['email_validation_mode' => 'html5'], + ]); + if (Kernel::VERSION_ID >= 70300) { + // Not setting it is deprecated since Symfony 7.3: its default value changes in 8.0 + $container->extension('framework', ['property_info' => ['with_constructor_extractor' => true]]); + } + + $services = $container->services(); + $services->set('app.rest_client', Client::class) + ->args([service('http_client'), service('logger'), 'app_tagged', 'https://example.com']) + ->tag('cleverage.rest.client', ['code' => 'app_tagged']); + $services->set(AttributedClient::class) + ->autoconfigure() + ->args([service('http_client'), service('logger'), 'app_attributed', 'https://example.com']); + // Exposed to the tests + $services->alias('test.client_registry', ClientRegistry::class)->public(); + + // Tasks referenced by code (task: ): process-bundle 5.4 or later + if (class_exists(TaskRegistry::class)) { + $container->extension('clever_age_process', [ + 'configurations' => [ + 'test.rest' => [ + 'tasks' => [ + 'request' => [ + 'task' => 'rest_request', + 'options' => ['client' => 'app_tagged', 'url' => '/'], + ], + ], + ], + ], + ]); + $services->alias('test.task_registry', TaskRegistry::class)->public(); + $services->alias('test.process_configuration_registry', ProcessConfigurationRegistry::class)->public(); + } + } + + private function getRunDir(): string + { + return sys_get_temp_dir().'/rest_process_bundle_tests_'.getmypid(); + } +} diff --git a/tests/CleverAgeRestProcessBundleTest.php b/tests/CleverAgeRestProcessBundleTest.php index 0fd47d7..635e016 100644 --- a/tests/CleverAgeRestProcessBundleTest.php +++ b/tests/CleverAgeRestProcessBundleTest.php @@ -50,7 +50,7 @@ public function testTaggedClientsAreRegistered(): void new Definition(NullLogger::class), 'api', 'https://example.com/api', - ]))->addTag('cleverage.rest.client'); + ]))->addTag('cleverage.rest.client', ['code' => 'api']); $container->compile(true); /** @var ClientRegistry $registry */ diff --git a/tests/Functional/KernelTest.php b/tests/Functional/KernelTest.php new file mode 100644 index 0000000..b74914b --- /dev/null +++ b/tests/Functional/KernelTest.php @@ -0,0 +1,84 @@ +exceptionHandler = $this->getExceptionHandler(); + } + + protected function tearDown(): void + { + parent::tearDown(); + // The ErrorHandler registered when the kernel boots is not removed by the first Symfony 6.4 versions + if ($this->getExceptionHandler() !== $this->exceptionHandler) { + restore_exception_handler(); + } + } + + public function testClientsAreResolvedByCode(): void + { + $registry = self::getContainer()->get('test.client_registry'); + self::assertInstanceOf(ClientRegistry::class, $registry); + + self::assertInstanceOf(Client::class, $registry->getClient('app_tagged')); + self::assertInstanceOf(AttributedClient::class, $registry->getClient('app_attributed')); + } + + public function testTasksAreResolvedByCode(): void + { + if (!class_exists(TaskRegistry::class)) { + self::markTestSkipped('The tasks are referenced by code since cleverage/process-bundle 5.4.'); + } + $processConfigurationRegistry = self::getContainer()->get('test.process_configuration_registry'); + self::assertInstanceOf(ProcessConfigurationRegistry::class, $processConfigurationRegistry); + $taskRegistry = self::getContainer()->get('test.task_registry'); + self::assertInstanceOf(TaskRegistry::class, $taskRegistry); + + $taskConfiguration = $processConfigurationRegistry->getProcessConfiguration('test.rest')->getTaskConfiguration('request'); + self::assertInstanceOf(RequestTask::class, $taskRegistry->getTask($taskConfiguration)); + } + + private function getExceptionHandler(): mixed + { + $handler = set_exception_handler(null); + restore_exception_handler(); + + return $handler; + } + + protected static function getKernelClass(): string + { + return TestKernel::class; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..091c408 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,19 @@ +