Skip to content

Commit 0b05698

Browse files
Merge pull request liip#484 from magnetik/cache-db
Add DisableCacheDb annotation
2 parents de5b198 + 4f94510 commit 0b05698

11 files changed

Lines changed: 110 additions & 6 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This new major version introduces a number of breaking changes; see the [upgrade
88
* Added `.gitattributes` to make package slimmer
99
* Applied `declare(strict_types=1)` everywhere
1010
* Added append fixture feature on `LoadFixtues`
11+
* Added annotation `@DisableDatabaseCache` to disable database cache for a test
1112

1213
### Changed
1314
* Switched to PSR-4 dir structure with `src` and `tests` subfolders
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/FunctionalTestBundle
7+
*
8+
* (c) Baptiste Lafontaine <magnetik@magnetik.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\FunctionalTestBundle\Annotations;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"METHOD"})
19+
*/
20+
final class DisableDatabaseCache
21+
{
22+
}

src/Resources/config/database_tools.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
</service>
4444
<service id="liip_functional_test.services.database_tool_collection" class="Liip\FunctionalTestBundle\Services\DatabaseToolCollection" public="true">
4545
<argument type="service" id="service_container" />
46+
<argument type="service" id="annotations.reader" />
4647
<call method="add">
4748
<argument type="service" id="liip_functional_test.services_database_tools.orm_database_tool" />
4849
</call>

src/Services/DatabaseToolCollection.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Liip\FunctionalTestBundle\Services;
1313

14+
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Liip\FunctionalTestBundle\Annotations\DisableDatabaseCache;
1416
use Liip\FunctionalTestBundle\Services\DatabaseTools\AbstractDatabaseTool;
1517
use Liip\FunctionalTestBundle\Test\WebTestCase;
1618
use Symfony\Bridge\Doctrine\ManagerRegistry;
@@ -23,14 +25,17 @@ final class DatabaseToolCollection
2325
{
2426
private $container;
2527

28+
private $annotationReader;
29+
2630
/**
2731
* @var AbstractDatabaseTool[][]
2832
*/
2933
private $items = [];
3034

31-
public function __construct(ContainerInterface $container)
35+
public function __construct(ContainerInterface $container, AnnotationReader $annotationReader)
3236
{
3337
$this->container = $container;
38+
$this->annotationReader = $annotationReader;
3439
}
3540

3641
public function add(AbstractDatabaseTool $databaseTool): void
@@ -53,6 +58,27 @@ public function get($omName = null, $registryName = 'doctrine', $purgeMode = nul
5358
$databaseTool->setPurgeMode($purgeMode);
5459
$databaseTool->setWebTestCase($webTestCase);
5560

61+
$databaseTool->setDatabaseCacheEnabled($this->isCacheEnabled());
62+
5663
return $databaseTool;
5764
}
65+
66+
public function isCacheEnabled(): bool
67+
{
68+
foreach (debug_backtrace() as $step) {
69+
if ('test' === substr($step['function'], 0, 4)) { //TODO: handle tests with the @test annotation
70+
$annotations = $this->annotationReader->getMethodAnnotations(
71+
new \ReflectionMethod($step['class'], $step['function'])
72+
);
73+
74+
foreach ($annotations as $annotationClass) {
75+
if ($annotationClass instanceof DisableDatabaseCache) {
76+
return false;
77+
}
78+
}
79+
}
80+
}
81+
82+
return true;
83+
}
5884
}

src/Services/DatabaseTools/AbstractDatabaseTool.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ abstract class AbstractDatabaseTool
5959
*/
6060
protected $webTestCase;
6161

62+
/**
63+
* @var bool
64+
*/
65+
protected $databaseCacheEnabled = true;
66+
6267
/**
6368
* @var array
6469
*/
@@ -77,6 +82,11 @@ public function setRegistry(ManagerRegistry $registry): void
7782
$this->registry = $registry;
7883
}
7984

85+
public function setDatabaseCacheEnabled(bool $databaseCacheEnabled): void
86+
{
87+
$this->databaseCacheEnabled = $databaseCacheEnabled;
88+
}
89+
8090
public function setObjectManagerName(string $omName = null): void
8191
{
8292
$this->omName = $omName;

src/Services/DatabaseTools/ORMDatabaseTool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
9797

9898
$backupService = $this->getBackupService();
9999

100-
if ($backupService) {
100+
if ($backupService && $this->databaseCacheEnabled) {
101101
$backupService->init($this->getMetadatas(), $classNames, $append);
102102

103103
if ($backupService->isBackupActual()) {

src/Services/DatabaseTools/ORMSqliteDatabaseTool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
3535
}
3636

3737
$backupService = $this->getBackupService();
38-
if ($backupService) {
38+
if ($backupService && $this->databaseCacheEnabled) {
3939
$backupService->init($this->getMetadatas(), $classNames);
4040

4141
if ($backupService->isBackupActual()) {

tests/Test/WebTestCaseConfigMysqlCacheDbTest.php

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

1414
namespace Liip\FunctionalTestBundle\Tests\Test;
1515

16+
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
1617
use Liip\FunctionalTestBundle\Tests\AppConfigMysqlCacheDb\AppConfigMysqlKernelCacheDb;
1718

1819
/**
@@ -31,6 +32,7 @@
3132
*
3233
* @runTestsInSeparateProcesses
3334
* @preserveGlobalState disabled
35+
* @IgnoreAnnotation("group")
3436
*/
3537
class WebTestCaseConfigMysqlCacheDbTest extends WebTestCaseConfigMysqlTest
3638
{

tests/Test/WebTestCaseConfigMysqlTest.php

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

1414
namespace Liip\FunctionalTestBundle\Tests\Test;
1515

16+
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
1617
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
1718
use Liip\FunctionalTestBundle\Test\WebTestCase;
1819
use Liip\FunctionalTestBundle\Tests\AppConfigMysql\AppConfigMysqlKernel;
@@ -33,6 +34,7 @@
3334
*
3435
* @runTestsInSeparateProcesses
3536
* @preserveGlobalState disabled
37+
* @IgnoreAnnotation("group")
3638
*/
3739
class WebTestCaseConfigMysqlTest extends WebTestCase
3840
{

tests/Test/WebTestCaseConfigTest.php

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

1414
namespace Liip\FunctionalTestBundle\Tests\Test;
1515

16+
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
17+
use Liip\FunctionalTestBundle\Annotations\DisableDatabaseCache;
1618
use Liip\FunctionalTestBundle\Annotations\QueryCount;
1719
use Liip\FunctionalTestBundle\Test\WebTestCase;
1820
use Liip\FunctionalTestBundle\Tests\AppConfig\AppConfigKernel;
@@ -246,6 +248,42 @@ public function testLoadFixturesFilesWithCustomProvider(): void
246248
);
247249
}
248250

251+
/**
252+
* @DisableDatabaseCache()
253+
*/
254+
public function testCacheCanBeDisabled(): void
255+
{
256+
// MD5 hash corresponding to these fixtures files.
257+
$md5 = '0ded9d8daaeaeca1056b18b9d0d433b2';
258+
$databaseFilePath = $this->getContainer()->getParameter('kernel.cache_dir').'/test_sqlite_'.$md5.'.db';
259+
260+
$fixtures = [
261+
'Liip\FunctionalTestBundle\Tests\App\DataFixtures\ORM\LoadDependentUserData',
262+
];
263+
264+
$this->loadFixtures($fixtures);
265+
266+
// Load data from database
267+
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
268+
269+
/** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user1 */
270+
$user1 = $em->getRepository('LiipFunctionalTestBundle:User')->findOneBy(['id' => 1]);
271+
272+
// Store random data, in order to check it after reloading fixtures.
273+
$user1Salt = $user1->getSalt();
274+
275+
sleep(2);
276+
277+
// Reload the fixtures.
278+
$this->loadFixtures($fixtures);
279+
280+
/** @var \Liip\FunctionalTestBundle\Tests\App\Entity\User $user1 */
281+
$user1 = $em->getRepository('LiipFunctionalTestBundle:User')->findOneBy(['id' => 1]);
282+
283+
//The salt are not the same because cache were not used
284+
$this->assertNotSame($user1Salt, $user1->getSalt());
285+
}
286+
249287
/**
250288
* Update a fixture file and check that the cache will be refreshed.
251289
*/

0 commit comments

Comments
 (0)