Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/QueryCountClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 10 additions & 1 deletion src/QueryCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing this information is better than before, but this warning may not appear when running tests, so it may be ineffective.


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) {
Expand Down
5 changes: 3 additions & 2 deletions tests/AppConfig/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
41 changes: 41 additions & 0 deletions tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* 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 <smith@pooteeweet.org>
*
* 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');
}
}
5 changes: 5 additions & 0 deletions tests/AppConfigMaxQueryCount/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# inherits configuration from ../AppConfig/config.yml

liip_functional_test:
query:
max_query_count: 0
51 changes: 51 additions & 0 deletions tests/DependencyInjection/ConfigurationConfigMaxQueryCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* 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'],
];
}
}
2 changes: 1 addition & 1 deletion tests/DependencyInjection/ConfigurationConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
Expand Down
109 changes: 109 additions & 0 deletions tests/Test/WebTestCaseConfigMaxQueryCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* 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+');
}
}
}
73 changes: 7 additions & 66 deletions tests/Test/WebTestCaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,11 +29,6 @@
* @runTestsInSeparateProcesses
*
* @preserveGlobalState disabled
*
* Avoid conflict with PHPUnit annotation when reading QueryCount
* annotation:
*
* @IgnoreAnnotation("expectedException")
*/
class WebTestCaseConfigTest extends WebTestCase
{
Expand All @@ -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;
Expand All @@ -54,8 +54,6 @@ protected static function getKernelClass(): string
*/
public function testIndexClientWithCredentials(): void
{
$this->skipTestIfSymfonyHasVersion7();

$this->client = static::makeClientWithCredentials('foobar', '12341234');

$path = '/admin';
Expand Down Expand Up @@ -87,8 +85,6 @@ public function testIndexClientWithCredentials(): void
*/
public function testIndexAuthenticatedClient(): void
{
$this->skipTestIfSymfonyHasVersion7();

$this->client = static::makeAuthenticatedClient();

$path = '/admin';
Expand Down Expand Up @@ -120,8 +116,6 @@ public function testIndexAuthenticatedClient(): void
*/
public function testIndexAuthenticationLoginAs(): void
{
$this->skipTestIfSymfonyHasVersion7();

$user = $this->loadTestFixtures();

$loginAs = $this->loginAs($user, 'secured_area');
Expand Down Expand Up @@ -160,8 +154,6 @@ public function testIndexAuthenticationLoginAs(): void
*/
public function testIndexAuthenticationLoginClient(): void
{
$this->skipTestIfSymfonyHasVersion7();

$user = $this->loadTestFixtures();

$this->client = static::makeClient();
Expand Down Expand Up @@ -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.
*
Expand Down
Loading