diff --git a/src/QueryCountClient.php b/src/QueryCountClient.php index 46652709..ae782c39 100644 --- a/src/QueryCountClient.php +++ b/src/QueryCountClient.php @@ -52,7 +52,7 @@ private function checkQueryCount(): void echo "\n". 'Profiler is disabled, it must be enabled for the '. 'Query Counter. '. - 'See https://github.com/liip/LiipFunctionalTestBundle#query-counter'. + 'See https://github.com/liip/LiipFunctionalTestBundle/blob/master/doc/query.md'. "\n"; // @codeCoverageIgnoreEnd } diff --git a/src/QueryCounter.php b/src/QueryCounter.php index b9b2347f..21ff164f 100644 --- a/src/QueryCounter.php +++ b/src/QueryCounter.php @@ -16,6 +16,7 @@ use Doctrine\Common\Annotations\Reader; use Liip\FunctionalTestBundle\Annotations\QueryCount; use Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException; +use Symfony\Component\HttpKernel\Kernel; final class QueryCounter { @@ -50,8 +51,16 @@ private function getMaxQueryCount(): ?int private function getMaxQueryAnnotation(): ?int { + if (Kernel::MAJOR_VERSION >= 7) { + trigger_error('The annotationReader is not available and it can’t be enabled on Symfony 7+, @QueryCount is not supported', \E_USER_WARNING); + + return null; + } + if (null === $this->annotationReader) { - @trigger_error('The annotationReader is not available', \E_USER_ERROR); + trigger_error('The annotationReader is not available', \E_USER_ERROR); + + return null; } foreach (debug_backtrace() as $step) { diff --git a/tests/AppConfig/config.yml b/tests/AppConfig/config.yml index 639d5cc8..b741d866 100644 --- a/tests/AppConfig/config.yml +++ b/tests/AppConfig/config.yml @@ -9,9 +9,10 @@ framework: liip_functional_test: command_verbosity: "very_verbose" command_decoration: false + # this is necessary to enable the query counter, + # so that the @QueryCount(x) annotation works query: - # TODO: it should be 1 - max_query_count: 0 + max_query_count: 1000 authentication: username: "foobar" password: "12341234" diff --git a/tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php b/tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php new file mode 100644 index 00000000..64164789 --- /dev/null +++ b/tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php @@ -0,0 +1,41 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\AppConfigMaxQueryCount; + +/* + * This file is part of the Liip/FunctionalTestBundle + * + * (c) Lukas Kahwe Smith + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +use Liip\Acme\Tests\AppConfig\AppConfigKernel; +use Symfony\Component\Config\Loader\LoaderInterface; + +class AppConfigMaxQueryCountKernel extends AppConfigKernel +{ + /** + * Load the config.yml from the current directory. + */ + public function registerContainerConfiguration(LoaderInterface $loader): void + { + // Load the default file. + parent::registerContainerConfiguration($loader); + + // Load the file with "liip_functional_test" parameters + $loader->load(__DIR__.'/config.yml'); + } +} diff --git a/tests/AppConfigMaxQueryCount/config.yml b/tests/AppConfigMaxQueryCount/config.yml new file mode 100644 index 00000000..c4270dc5 --- /dev/null +++ b/tests/AppConfigMaxQueryCount/config.yml @@ -0,0 +1,5 @@ +# inherits configuration from ../AppConfig/config.yml + +liip_functional_test: + query: + max_query_count: 0 diff --git a/tests/DependencyInjection/ConfigurationConfigMaxQueryCountTest.php b/tests/DependencyInjection/ConfigurationConfigMaxQueryCountTest.php new file mode 100644 index 00000000..7365f8cb --- /dev/null +++ b/tests/DependencyInjection/ConfigurationConfigMaxQueryCountTest.php @@ -0,0 +1,51 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace DependencyInjection; + +use Liip\Acme\Tests\AppConfigMaxQueryCount\AppConfigMaxQueryCountKernel; +use Liip\Acme\Tests\DependencyInjection\ConfigurationTest; + +/** + * Use Tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php instead of + * Tests/App/AppKernel.php. + * So it must be loaded in a separate process. + * + * @runTestsInSeparateProcesses + * + * @preserveGlobalState disabled + */ +class ConfigurationConfigMaxQueryCountTest extends ConfigurationTest +{ + /** + * Use another Kernel to load another config file. + */ + protected static function getKernelClass(): string + { + return AppConfigMaxQueryCountKernel::class; + } + + /** + * Override values to be tested. + */ + public static function parametersProvider(): array + { + return [ + ['command_verbosity', 'very_verbose'], + ['command_decoration', false], + ['query.max_query_count', 0], + ['authentication.username', 'foobar'], + ['authentication.password', '12341234'], + ]; + } +} diff --git a/tests/DependencyInjection/ConfigurationConfigTest.php b/tests/DependencyInjection/ConfigurationConfigTest.php index 439a9122..3986f140 100644 --- a/tests/DependencyInjection/ConfigurationConfigTest.php +++ b/tests/DependencyInjection/ConfigurationConfigTest.php @@ -42,7 +42,7 @@ public static function parametersProvider(): array return [ ['command_verbosity', 'very_verbose'], ['command_decoration', false], - ['query.max_query_count', 0], + ['query.max_query_count', 1000], ['authentication.username', 'foobar'], ['authentication.password', '12341234'], ]; diff --git a/tests/Test/WebTestCaseConfigMaxQueryCountTest.php b/tests/Test/WebTestCaseConfigMaxQueryCountTest.php new file mode 100644 index 00000000..287a23be --- /dev/null +++ b/tests/Test/WebTestCaseConfigMaxQueryCountTest.php @@ -0,0 +1,109 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Liip\Acme\Tests\Test; + +use Liip\Acme\Tests\App\Entity\User; +use Liip\Acme\Tests\AppConfigMaxQueryCount\AppConfigMaxQueryCountKernel; +use Liip\Acme\Tests\Traits\LiipAcmeFixturesTrait; +use Liip\FunctionalTestBundle\Test\WebTestCase; +use Symfony\Component\HttpKernel\Kernel; + +/** + * Tests that AllowedQueriesExceededException is thrown. + * + * Use Tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php instead of + * Tests/App/AppKernel.php. + * So it must be loaded in a separate process. + * + * @runTestsInSeparateProcesses + * + * @preserveGlobalState disabled + */ +class WebTestCaseConfigMaxQueryCountTest extends WebTestCase +{ + use LiipAcmeFixturesTrait; + + /** @var \Symfony\Bundle\FrameworkBundle\Client client */ + private $client; + + protected function tearDown(): void + { + parent::tearDown(); + + restore_exception_handler(); + } + + protected static function getKernelClass(): string + { + return AppConfigMaxQueryCountKernel::class; + } + + /** + * Log in as the user defined in the Data Fixtures and except an + * AllowedQueriesExceededException exception. + * + * There will be 2 queries: + * - the user 1 is loaded from the database when logging in + * - the user 2 is loaded by the controller + * + * In the configuration the limit is 1, an Exception will be thrown. + */ + public function testAllowedQueriesExceededException(): void + { + $this->skipTestIfSymfonyHasVersion7(); + + $user = $this->loadTestFixtures(); + + $this->assertInstanceOf( + User::class, + $user + ); + + $this->client = static::makeClient(); + + $this->loginClient($this->client, $user, 'secured_area'); + + $path = '/user/2'; + + $this->expectException(\Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException::class); + + $crawler = $this->client->request('GET', $path); + + // The following code is called if no exception has been thrown, it should help to understand why + $this->assertStatusCode(200, $this->client); + $this->assertSame( + 'LiipFunctionalTestBundle', + $crawler->filter('h1')->text() + ); + $this->assertSame( + 'Logged in as foo bar.', + $crawler->filter('p#user')->text() + ); + $this->assertSame( + 'Name: alice bob', + $crawler->filter('div#content p:nth-child(1)')->text() + ); + $this->assertSame( + 'Email: alice@example.com', + $crawler->filter('div#content p:nth-child(2)')->text() + ); + } + + private function skipTestIfSymfonyHasVersion7(): void + { + if (Kernel::MAJOR_VERSION >= 7) { + $this->markTestSkipped('The QueryCount is not compatible with Symfony 7+'); + } + } +} diff --git a/tests/Test/WebTestCaseConfigTest.php b/tests/Test/WebTestCaseConfigTest.php index 68d4f4ec..65521b85 100644 --- a/tests/Test/WebTestCaseConfigTest.php +++ b/tests/Test/WebTestCaseConfigTest.php @@ -13,8 +13,6 @@ namespace Liip\Acme\Tests\Test; -use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation; -use Liip\Acme\Tests\App\Entity\User; use Liip\Acme\Tests\AppConfig\AppConfigKernel; use Liip\Acme\Tests\Traits\LiipAcmeFixturesTrait; use Liip\FunctionalTestBundle\Annotations\QueryCount; @@ -31,11 +29,6 @@ * @runTestsInSeparateProcesses * * @preserveGlobalState disabled - * - * Avoid conflict with PHPUnit annotation when reading QueryCount - * annotation: - * - * @IgnoreAnnotation("expectedException") */ class WebTestCaseConfigTest extends WebTestCase { @@ -44,6 +37,13 @@ class WebTestCaseConfigTest extends WebTestCase /** @var \Symfony\Bundle\FrameworkBundle\Client client */ private $client; + protected function tearDown(): void + { + parent::tearDown(); + + restore_exception_handler(); + } + protected static function getKernelClass(): string { return AppConfigKernel::class; @@ -54,8 +54,6 @@ protected static function getKernelClass(): string */ public function testIndexClientWithCredentials(): void { - $this->skipTestIfSymfonyHasVersion7(); - $this->client = static::makeClientWithCredentials('foobar', '12341234'); $path = '/admin'; @@ -87,8 +85,6 @@ public function testIndexClientWithCredentials(): void */ public function testIndexAuthenticatedClient(): void { - $this->skipTestIfSymfonyHasVersion7(); - $this->client = static::makeAuthenticatedClient(); $path = '/admin'; @@ -120,8 +116,6 @@ public function testIndexAuthenticatedClient(): void */ public function testIndexAuthenticationLoginAs(): void { - $this->skipTestIfSymfonyHasVersion7(); - $user = $this->loadTestFixtures(); $loginAs = $this->loginAs($user, 'secured_area'); @@ -160,8 +154,6 @@ public function testIndexAuthenticationLoginAs(): void */ public function testIndexAuthenticationLoginClient(): void { - $this->skipTestIfSymfonyHasVersion7(); - $user = $this->loadTestFixtures(); $this->client = static::makeClient(); @@ -190,57 +182,6 @@ public function testIndexAuthenticationLoginClient(): void ); } - /** - * Log in as the user defined in the Data Fixtures and except an - * AllowedQueriesExceededException exception. - * - * There will be 2 queries: - * - the user 1 is loaded from the database when logging in - * - the user 2 is loaded by the controller - * - * In the configuration the limit is 1, an Exception will be thrown. - */ - public function testAllowedQueriesExceededException(): void - { - $this->skipTestIfSymfonyHasVersion7(); - - $user = $this->loadTestFixtures(); - - $this->assertInstanceOf( - User::class, - $user - ); - - $this->client = static::makeClient(); - - $this->loginClient($this->client, $user, 'secured_area'); - - $path = '/user/2'; - - $this->expectException(\Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException::class); - - $crawler = $this->client->request('GET', $path); - - // The following code is called if no exception has been thrown, it should help to understand why - $this->assertStatusCode(200, $this->client); - $this->assertSame( - 'LiipFunctionalTestBundle', - $crawler->filter('h1')->text() - ); - $this->assertSame( - 'Logged in as foo bar.', - $crawler->filter('p#user')->text() - ); - $this->assertSame( - 'Name: alice bob', - $crawler->filter('div#content p:nth-child(1)')->text() - ); - $this->assertSame( - 'Email: alice@example.com', - $crawler->filter('div#content p:nth-child(2)')->text() - ); - } - /** * Expect an exception due to the QueryCount annotation. * diff --git a/tests/Test/WebTestCaseTest.php b/tests/Test/WebTestCaseTest.php index d2608c81..c2627688 100644 --- a/tests/Test/WebTestCaseTest.php +++ b/tests/Test/WebTestCaseTest.php @@ -25,7 +25,6 @@ /** * @IgnoreAnnotation("depends") - * @IgnoreAnnotation("expectedException") */ class WebTestCaseTest extends WebTestCase {