Skip to content

Commit 0c3a63e

Browse files
feat: split tests of query.max_query_count
1 parent e2fdc70 commit 0c3a63e

File tree

9 files changed

+218
-71
lines changed

9 files changed

+218
-71
lines changed

src/QueryCountClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function checkQueryCount(): void
5252
echo "\n".
5353
'Profiler is disabled, it must be enabled for the '.
5454
'Query Counter. '.
55-
'See https://github.com/liip/LiipFunctionalTestBundle#query-counter'.
55+
'See https://github.com/liip/LiipFunctionalTestBundle/blob/master/doc/query.md'.
5656
"\n";
5757
// @codeCoverageIgnoreEnd
5858
}

tests/AppConfig/config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ framework:
99
liip_functional_test:
1010
command_verbosity: "very_verbose"
1111
command_decoration: false
12+
# this is necessary to enable the query counter,
13+
# so that the @QueryCount(x) annotation works
1214
query:
13-
# TODO: it should be 1
14-
max_query_count: 0
15+
max_query_count: 1000
1516
authentication:
1617
username: "foobar"
1718
password: "12341234"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/FunctionalTestBundle
7+
*
8+
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Liip\Acme\Tests\AppConfigMaxQueryCount;
15+
16+
/*
17+
* This file is part of the Liip/FunctionalTestBundle
18+
*
19+
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
20+
*
21+
* This source file is subject to the MIT license that is bundled
22+
* with this source code in the file LICENSE.
23+
*/
24+
25+
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
26+
use Symfony\Component\Config\Loader\LoaderInterface;
27+
28+
class AppConfigMaxQueryCountKernel extends AppConfigKernel
29+
{
30+
/**
31+
* Load the config.yml from the current directory.
32+
*/
33+
public function registerContainerConfiguration(LoaderInterface $loader): void
34+
{
35+
// Load the default file.
36+
parent::registerContainerConfiguration($loader);
37+
38+
// Load the file with "liip_functional_test" parameters
39+
$loader->load(__DIR__.'/config.yml');
40+
}
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# inherits configuration from ../AppConfig/config.yml
2+
3+
liip_functional_test:
4+
query:
5+
max_query_count: 0
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/FunctionalTestBundle
7+
*
8+
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace DependencyInjection;
15+
16+
use Liip\Acme\Tests\AppConfigMaxQueryCount\AppConfigMaxQueryCountKernel;
17+
use Liip\Acme\Tests\DependencyInjection\ConfigurationTest;
18+
19+
/**
20+
* Use Tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php instead of
21+
* Tests/App/AppKernel.php.
22+
* So it must be loaded in a separate process.
23+
*
24+
* @runTestsInSeparateProcesses
25+
*
26+
* @preserveGlobalState disabled
27+
*/
28+
class ConfigurationConfigMaxQueryCountTest extends ConfigurationTest
29+
{
30+
/**
31+
* Use another Kernel to load another config file.
32+
*/
33+
protected static function getKernelClass(): string
34+
{
35+
return AppConfigMaxQueryCountKernel::class;
36+
}
37+
38+
/**
39+
* Override values to be tested.
40+
*/
41+
public static function parametersProvider(): array
42+
{
43+
return [
44+
['command_verbosity', 'very_verbose'],
45+
['command_decoration', false],
46+
['query.max_query_count', 0],
47+
['authentication.username', 'foobar'],
48+
['authentication.password', '12341234'],
49+
];
50+
}
51+
}

tests/DependencyInjection/ConfigurationConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function parametersProvider(): array
4242
return [
4343
['command_verbosity', 'very_verbose'],
4444
['command_decoration', false],
45-
['query.max_query_count', 0],
45+
['query.max_query_count', 1000],
4646
['authentication.username', 'foobar'],
4747
['authentication.password', '12341234'],
4848
];
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/FunctionalTestBundle
7+
*
8+
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Liip\Acme\Tests\Test;
15+
16+
use Liip\Acme\Tests\App\Entity\User;
17+
use Liip\Acme\Tests\AppConfigMaxQueryCount\AppConfigMaxQueryCountKernel;
18+
use Liip\Acme\Tests\Traits\LiipAcmeFixturesTrait;
19+
use Liip\FunctionalTestBundle\Test\WebTestCase;
20+
use Symfony\Component\HttpKernel\Kernel;
21+
22+
/**
23+
* Tests that AllowedQueriesExceededException is thrown.
24+
*
25+
* Use Tests/AppConfigMaxQueryCount/AppConfigMaxQueryCountKernel.php instead of
26+
* Tests/App/AppKernel.php.
27+
* So it must be loaded in a separate process.
28+
*
29+
* @runTestsInSeparateProcesses
30+
*
31+
* @preserveGlobalState disabled
32+
*/
33+
class WebTestCaseConfigMaxQueryCountTest extends WebTestCase
34+
{
35+
use LiipAcmeFixturesTrait;
36+
37+
/** @var \Symfony\Bundle\FrameworkBundle\Client client */
38+
private $client;
39+
40+
protected function tearDown(): void
41+
{
42+
parent::tearDown();
43+
44+
restore_exception_handler();
45+
}
46+
47+
protected static function getKernelClass(): string
48+
{
49+
return AppConfigMaxQueryCountKernel::class;
50+
}
51+
52+
/**
53+
* Log in as the user defined in the Data Fixtures and except an
54+
* AllowedQueriesExceededException exception.
55+
*
56+
* There will be 2 queries:
57+
* - the user 1 is loaded from the database when logging in
58+
* - the user 2 is loaded by the controller
59+
*
60+
* In the configuration the limit is 1, an Exception will be thrown.
61+
*/
62+
public function testAllowedQueriesExceededException(): void
63+
{
64+
$this->skipTestIfSymfonyHasVersion7();
65+
66+
$user = $this->loadTestFixtures();
67+
68+
$this->assertInstanceOf(
69+
User::class,
70+
$user
71+
);
72+
73+
$this->client = static::makeClient();
74+
75+
$this->loginClient($this->client, $user, 'secured_area');
76+
77+
$path = '/user/2';
78+
79+
$this->expectException(\Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException::class);
80+
81+
$crawler = $this->client->request('GET', $path);
82+
83+
// The following code is called if no exception has been thrown, it should help to understand why
84+
$this->assertStatusCode(200, $this->client);
85+
$this->assertSame(
86+
'LiipFunctionalTestBundle',
87+
$crawler->filter('h1')->text()
88+
);
89+
$this->assertSame(
90+
'Logged in as foo bar.',
91+
$crawler->filter('p#user')->text()
92+
);
93+
$this->assertSame(
94+
'Name: alice bob',
95+
$crawler->filter('div#content p:nth-child(1)')->text()
96+
);
97+
$this->assertSame(
98+
'Email: alice@example.com',
99+
$crawler->filter('div#content p:nth-child(2)')->text()
100+
);
101+
}
102+
103+
private function skipTestIfSymfonyHasVersion7(): void
104+
{
105+
if (Kernel::MAJOR_VERSION >= 7) {
106+
$this->markTestSkipped('The QueryCount is not compatible with Symfony 7+');
107+
}
108+
}
109+
}

tests/Test/WebTestCaseConfigTest.php

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace Liip\Acme\Tests\Test;
1515

16-
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
17-
use Liip\Acme\Tests\App\Entity\User;
1816
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
1917
use Liip\Acme\Tests\Traits\LiipAcmeFixturesTrait;
2018
use Liip\FunctionalTestBundle\Annotations\QueryCount;
@@ -31,11 +29,6 @@
3129
* @runTestsInSeparateProcesses
3230
*
3331
* @preserveGlobalState disabled
34-
*
35-
* Avoid conflict with PHPUnit annotation when reading QueryCount
36-
* annotation:
37-
*
38-
* @IgnoreAnnotation("expectedException")
3932
*/
4033
class WebTestCaseConfigTest extends WebTestCase
4134
{
@@ -44,6 +37,13 @@ class WebTestCaseConfigTest extends WebTestCase
4437
/** @var \Symfony\Bundle\FrameworkBundle\Client client */
4538
private $client;
4639

40+
protected function tearDown(): void
41+
{
42+
parent::tearDown();
43+
44+
restore_exception_handler();
45+
}
46+
4747
protected static function getKernelClass(): string
4848
{
4949
return AppConfigKernel::class;
@@ -54,8 +54,6 @@ protected static function getKernelClass(): string
5454
*/
5555
public function testIndexClientWithCredentials(): void
5656
{
57-
$this->skipTestIfSymfonyHasVersion7();
58-
5957
$this->client = static::makeClientWithCredentials('foobar', '12341234');
6058

6159
$path = '/admin';
@@ -87,8 +85,6 @@ public function testIndexClientWithCredentials(): void
8785
*/
8886
public function testIndexAuthenticatedClient(): void
8987
{
90-
$this->skipTestIfSymfonyHasVersion7();
91-
9288
$this->client = static::makeAuthenticatedClient();
9389

9490
$path = '/admin';
@@ -120,8 +116,6 @@ public function testIndexAuthenticatedClient(): void
120116
*/
121117
public function testIndexAuthenticationLoginAs(): void
122118
{
123-
$this->skipTestIfSymfonyHasVersion7();
124-
125119
$user = $this->loadTestFixtures();
126120

127121
$loginAs = $this->loginAs($user, 'secured_area');
@@ -160,8 +154,6 @@ public function testIndexAuthenticationLoginAs(): void
160154
*/
161155
public function testIndexAuthenticationLoginClient(): void
162156
{
163-
$this->skipTestIfSymfonyHasVersion7();
164-
165157
$user = $this->loadTestFixtures();
166158

167159
$this->client = static::makeClient();
@@ -190,57 +182,6 @@ public function testIndexAuthenticationLoginClient(): void
190182
);
191183
}
192184

193-
/**
194-
* Log in as the user defined in the Data Fixtures and except an
195-
* AllowedQueriesExceededException exception.
196-
*
197-
* There will be 2 queries:
198-
* - the user 1 is loaded from the database when logging in
199-
* - the user 2 is loaded by the controller
200-
*
201-
* In the configuration the limit is 1, an Exception will be thrown.
202-
*/
203-
public function testAllowedQueriesExceededException(): void
204-
{
205-
$this->skipTestIfSymfonyHasVersion7();
206-
207-
$user = $this->loadTestFixtures();
208-
209-
$this->assertInstanceOf(
210-
User::class,
211-
$user
212-
);
213-
214-
$this->client = static::makeClient();
215-
216-
$this->loginClient($this->client, $user, 'secured_area');
217-
218-
$path = '/user/2';
219-
220-
$this->expectException(\Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException::class);
221-
222-
$crawler = $this->client->request('GET', $path);
223-
224-
// The following code is called if no exception has been thrown, it should help to understand why
225-
$this->assertStatusCode(200, $this->client);
226-
$this->assertSame(
227-
'LiipFunctionalTestBundle',
228-
$crawler->filter('h1')->text()
229-
);
230-
$this->assertSame(
231-
'Logged in as foo bar.',
232-
$crawler->filter('p#user')->text()
233-
);
234-
$this->assertSame(
235-
'Name: alice bob',
236-
$crawler->filter('div#content p:nth-child(1)')->text()
237-
);
238-
$this->assertSame(
239-
'Email: alice@example.com',
240-
$crawler->filter('div#content p:nth-child(2)')->text()
241-
);
242-
}
243-
244185
/**
245186
* Expect an exception due to the QueryCount annotation.
246187
*

tests/Test/WebTestCaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
/**
2727
* @IgnoreAnnotation("depends")
28-
* @IgnoreAnnotation("expectedException")
2928
*/
3029
class WebTestCaseTest extends WebTestCase
3130
{

0 commit comments

Comments
 (0)