From 32c98b3b22c425376743fa19003811cd96771e29 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Thu, 12 Sep 2024 06:15:22 +0200 Subject: [PATCH 1/2] [FrameworkBundle] Fix service reset between tests --- Test/KernelTestCase.php | 5 ++++ .../ResettableService.php | 27 +++++++++++++++++++ Tests/Functional/KernelTestCaseTest.php | 11 ++++++++ .../app/TestServiceContainer/services.yml | 5 ++++ composer.json | 2 +- 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Tests/Functional/Bundle/TestBundle/TestServiceContainer/ResettableService.php diff --git a/Test/KernelTestCase.php b/Test/KernelTestCase.php index 456085014..89dbd40af 100644 --- a/Test/KernelTestCase.php +++ b/Test/KernelTestCase.php @@ -160,6 +160,11 @@ protected static function ensureKernelShutdown() static::$kernel->shutdown(); static::$booted = false; + if ($container->has('services_resetter')) { + // Instantiate the service because Container::reset() only resets services that have been used + $container->get('services_resetter'); + } + if ($container instanceof ResetInterface) { $container->reset(); } diff --git a/Tests/Functional/Bundle/TestBundle/TestServiceContainer/ResettableService.php b/Tests/Functional/Bundle/TestBundle/TestServiceContainer/ResettableService.php new file mode 100644 index 000000000..e723da81e --- /dev/null +++ b/Tests/Functional/Bundle/TestBundle/TestServiceContainer/ResettableService.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer; + +class ResettableService +{ + private $count = 0; + + public function myCustomName(): void + { + ++$this->count; + } + + public function getCount(): int + { + return $this->count; + } +} diff --git a/Tests/Functional/KernelTestCaseTest.php b/Tests/Functional/KernelTestCaseTest.php index 32bee3b58..5c979a2d0 100644 --- a/Tests/Functional/KernelTestCaseTest.php +++ b/Tests/Functional/KernelTestCaseTest.php @@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService; +use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\ResettableService; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -41,4 +42,14 @@ public function testThatPrivateServicesAreAvailableIfTestConfigIsEnabled() $this->assertTrue($container->has('private_service')); $this->assertFalse($container->has(UnusedPrivateService::class)); } + + public function testServicesAreResetOnEnsureKernelShutdown() + { + static::bootKernel(['test_case' => 'TestServiceContainer']); + + $resettableService = static::getContainer()->get(ResettableService::class); + + self::ensureKernelShutdown(); + self::assertSame(1, $resettableService->getCount()); + } } diff --git a/Tests/Functional/app/TestServiceContainer/services.yml b/Tests/Functional/app/TestServiceContainer/services.yml index 523cca58d..c2b6f3698 100644 --- a/Tests/Functional/app/TestServiceContainer/services.yml +++ b/Tests/Functional/app/TestServiceContainer/services.yml @@ -13,3 +13,8 @@ services: arguments: - '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService' - '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService' + + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\ResettableService: + public: true + tags: + - kernel.reset: { method: 'myCustomName' } diff --git a/composer.json b/composer.json index c1f08b183..3bae1c386 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "ext-xml": "*", "symfony/cache": "^5.2|^6.0", "symfony/config": "^5.3|^6.0", - "symfony/dependency-injection": "^5.4.5|^6.0.5", + "symfony/dependency-injection": "^5.4.44|^6.0.5", "symfony/deprecation-contracts": "^2.1|^3", "symfony/event-dispatcher": "^5.1|^6.0", "symfony/error-handler": "^4.4.1|^5.0.1|^6.0", From 2f1b7a7ec50337d9b681427fe13a66b1abbb36b0 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Fri, 13 Sep 2024 03:03:54 +0200 Subject: [PATCH 2/2] [FrameworkBundle] Fix schema & finish incomplete tests for lock & semaphore config --- .../DependencyInjection/Fixtures/php/lock.php | 9 +++ .../Fixtures/php/lock_named.php | 16 ++++++ .../Fixtures/php/semaphore.php | 9 +++ .../Fixtures/php/semaphore_named.php | 14 +++++ .../DependencyInjection/Fixtures/xml/lock.xml | 4 +- .../Fixtures/xml/lock_named.xml | 3 +- .../Fixtures/xml/semaphore.xml | 4 +- .../Fixtures/xml/semaphore_named.xml | 16 ++++++ .../FrameworkExtensionTestCase.php | 56 +++++++++++++++++++ 9 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 Tests/DependencyInjection/Fixtures/php/lock.php create mode 100644 Tests/DependencyInjection/Fixtures/php/lock_named.php create mode 100644 Tests/DependencyInjection/Fixtures/php/semaphore.php create mode 100644 Tests/DependencyInjection/Fixtures/php/semaphore_named.php create mode 100644 Tests/DependencyInjection/Fixtures/xml/semaphore_named.xml diff --git a/Tests/DependencyInjection/Fixtures/php/lock.php b/Tests/DependencyInjection/Fixtures/php/lock.php new file mode 100644 index 000000000..116e074df --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/php/lock.php @@ -0,0 +1,9 @@ +loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'lock' => null, +]); diff --git a/Tests/DependencyInjection/Fixtures/php/lock_named.php b/Tests/DependencyInjection/Fixtures/php/lock_named.php new file mode 100644 index 000000000..de8a8f495 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/php/lock_named.php @@ -0,0 +1,16 @@ +setParameter('env(REDIS_DSN)', 'redis://paas.com'); + +$container->loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'lock' => [ + 'foo' => 'semaphore', + 'bar' => 'flock', + 'baz' => ['semaphore', 'flock'], + 'qux' => '%env(REDIS_DSN)%', + ], +]); diff --git a/Tests/DependencyInjection/Fixtures/php/semaphore.php b/Tests/DependencyInjection/Fixtures/php/semaphore.php new file mode 100644 index 000000000..c2a1e3b6e --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/php/semaphore.php @@ -0,0 +1,9 @@ +loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'semaphore' => 'redis://localhost', +]); diff --git a/Tests/DependencyInjection/Fixtures/php/semaphore_named.php b/Tests/DependencyInjection/Fixtures/php/semaphore_named.php new file mode 100644 index 000000000..c42b55983 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/php/semaphore_named.php @@ -0,0 +1,14 @@ +setParameter('env(REDIS_DSN)', 'redis://paas.com'); + +$container->loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'semaphore' => [ + 'foo' => 'redis://paas.com', + 'qux' => '%env(REDIS_DSN)%', + ], +]); diff --git a/Tests/DependencyInjection/Fixtures/xml/lock.xml b/Tests/DependencyInjection/Fixtures/xml/lock.xml index 5ddb62f11..ab344ae0e 100644 --- a/Tests/DependencyInjection/Fixtures/xml/lock.xml +++ b/Tests/DependencyInjection/Fixtures/xml/lock.xml @@ -8,6 +8,8 @@ - + + semaphore + diff --git a/Tests/DependencyInjection/Fixtures/xml/lock_named.xml b/Tests/DependencyInjection/Fixtures/xml/lock_named.xml index 85cf3cb57..02659713e 100644 --- a/Tests/DependencyInjection/Fixtures/xml/lock_named.xml +++ b/Tests/DependencyInjection/Fixtures/xml/lock_named.xml @@ -5,7 +5,6 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - redis://paas.com @@ -18,7 +17,7 @@ flock semaphore flock - %env(REDIS_URL)% + %env(REDIS_DSN)% diff --git a/Tests/DependencyInjection/Fixtures/xml/semaphore.xml b/Tests/DependencyInjection/Fixtures/xml/semaphore.xml index 7acbe2ced..dcab80326 100644 --- a/Tests/DependencyInjection/Fixtures/xml/semaphore.xml +++ b/Tests/DependencyInjection/Fixtures/xml/semaphore.xml @@ -8,6 +8,8 @@ - + + redis://localhost + diff --git a/Tests/DependencyInjection/Fixtures/xml/semaphore_named.xml b/Tests/DependencyInjection/Fixtures/xml/semaphore_named.xml new file mode 100644 index 000000000..7e454c2fd --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/xml/semaphore_named.xml @@ -0,0 +1,16 @@ + + + + + + + + redis://paas.com + %env(REDIS_DSN)% + + + diff --git a/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/Tests/DependencyInjection/FrameworkExtensionTestCase.php index 705ec5f31..e6616a07d 100644 --- a/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -2396,6 +2396,62 @@ public function testAssetMapperWithoutAssets() $this->assertFalse($container->has('assets._default_package')); } + public function testDefaultLock() + { + $container = $this->createContainerFromFile('lock'); + + self::assertTrue($container->hasDefinition('lock.default.factory')); + $storeDef = $container->getDefinition($container->getDefinition('lock.default.factory')->getArgument(0)); + self::assertEquals(new Reference('semaphore'), $storeDef->getArgument(0)); + } + + public function testNamedLocks() + { + $container = $this->createContainerFromFile('lock_named'); + + self::assertTrue($container->hasDefinition('lock.foo.factory')); + $storeDef = $container->getDefinition($container->getDefinition('lock.foo.factory')->getArgument(0)); + self::assertEquals(new Reference('semaphore'), $storeDef->getArgument(0)); + + self::assertTrue($container->hasDefinition('lock.bar.factory')); + $storeDef = $container->getDefinition($container->getDefinition('lock.bar.factory')->getArgument(0)); + self::assertEquals(new Reference('flock'), $storeDef->getArgument(0)); + + self::assertTrue($container->hasDefinition('lock.baz.factory')); + $storeDef = $container->getDefinition($container->getDefinition('lock.baz.factory')->getArgument(0)); + self::assertIsArray($storeDefArg = $storeDef->getArgument(0)); + $storeDef1 = $container->getDefinition($storeDefArg[0]); + $storeDef2 = $container->getDefinition($storeDefArg[1]); + self::assertEquals(new Reference('semaphore'), $storeDef1->getArgument(0)); + self::assertEquals(new Reference('flock'), $storeDef2->getArgument(0)); + + self::assertTrue($container->hasDefinition('lock.qux.factory')); + $storeDef = $container->getDefinition($container->getDefinition('lock.qux.factory')->getArgument(0)); + self::assertStringContainsString('REDIS_DSN', $storeDef->getArgument(0)); + } + + public function testDefaultSemaphore() + { + $container = $this->createContainerFromFile('semaphore'); + + self::assertTrue($container->hasDefinition('semaphore.default.factory')); + $storeDef = $container->getDefinition($container->getDefinition('semaphore.default.factory')->getArgument(0)); + self::assertSame('redis://localhost', $storeDef->getArgument(0)); + } + + public function testNamedSemaphores() + { + $container = $this->createContainerFromFile('semaphore_named'); + + self::assertTrue($container->hasDefinition('semaphore.foo.factory')); + $storeDef = $container->getDefinition($container->getDefinition('semaphore.foo.factory')->getArgument(0)); + self::assertSame('redis://paas.com', $storeDef->getArgument(0)); + + self::assertTrue($container->hasDefinition('semaphore.qux.factory')); + $storeDef = $container->getDefinition($container->getDefinition('semaphore.qux.factory')->getArgument(0)); + self::assertStringContainsString('REDIS_DSN', $storeDef->getArgument(0)); + } + protected function createContainer(array $data = []) { return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([