Skip to content

Commit e5cc9b2

Browse files
committed
chore: clear cache in tests
1 parent 8ef1c46 commit e5cc9b2

8 files changed

Lines changed: 85 additions & 12 deletions

File tree

src/Services/DatabaseTools/AbstractDatabaseTool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,9 @@ protected function getCacheMetadataParameter()
265265
}
266266

267267
abstract protected function getPlatformName(): string;
268+
269+
public static function clearMetadata(): void
270+
{
271+
self::$cachedMetadatas = [];
272+
}
268273
}

tests/Test/ConfigEventsTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
2222
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
2323
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool;
24-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2524

2625
/**
2726
* Tests that configuration has been loaded and users can be logged in.
@@ -38,7 +37,7 @@
3837
*
3938
* @internal
4039
*/
41-
class ConfigEventsTest extends KernelTestCase
40+
class ConfigEventsTest extends FixturesTestCase
4241
{
4342
use ContainerProvider;
4443

tests/Test/ConfigMongodbTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* with this source code in the file LICENSE.
1212
*/
1313

14+
namespace Liip\Acme\Tests\Test;
15+
1416
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
1517
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
1618
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
@@ -21,7 +23,6 @@
2123
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
2224
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
2325
use Liip\TestFixturesBundle\Services\DatabaseTools\MongoDBDatabaseTool;
24-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2526

2627
/**
2728
* Test MongoDB.
@@ -36,7 +37,7 @@
3637
*
3738
* @internal
3839
*/
39-
class ConfigMongodbTest extends KernelTestCase
40+
class ConfigMongodbTest extends FixturesTestCase
4041
{
4142
use ContainerProvider;
4243

tests/Test/ConfigMysqlTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
2323
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
2424
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMDatabaseTool;
25-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2625

2726
// BC, needed by "theofidry/alice-data-fixtures: <1.3" not compatible with "doctrine/persistence: ^2.0"
2827
if (interface_exists('\Doctrine\Persistence\ObjectManager')
@@ -51,7 +50,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence
5150
*
5251
* @internal
5352
*/
54-
class ConfigMysqlTest extends KernelTestCase
53+
class ConfigMysqlTest extends FixturesTestCase
5554
{
5655
use ContainerProvider;
5756

tests/Test/ConfigPhpcrTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
2323
use Liip\TestFixturesBundle\Services\DatabaseTools\PHPCRDatabaseTool;
2424
use Symfony\Bundle\FrameworkBundle\Console\Application;
25-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2625
use Symfony\Component\Console\Tester\CommandTester;
2726

2827
/**
@@ -38,7 +37,7 @@
3837
*
3938
* @internal
4039
*/
41-
class ConfigPhpcrTest extends KernelTestCase
40+
class ConfigPhpcrTest extends FixturesTestCase
4241
{
4342
use ContainerProvider;
4443

tests/Test/ConfigSqliteTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence
2828
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
2929
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
3030
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool;
31-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
3231

3332
/**
3433
* @runTestsInSeparateProcesses
@@ -40,7 +39,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence
4039
*
4140
* @internal
4241
*/
43-
class ConfigSqliteTest extends KernelTestCase
42+
class ConfigSqliteTest extends FixturesTestCase
4443
{
4544
use ContainerProvider;
4645

tests/Test/ConfigTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
2323
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
2424
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool;
25-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2625

2726
/**
2827
* Tests that configuration has been loaded and users can be logged in.
@@ -42,7 +41,7 @@
4241
*
4342
* @internal
4443
*/
45-
class ConfigTest extends KernelTestCase
44+
class ConfigTest extends FixturesTestCase
4645
{
4746
use ContainerProvider;
4847

tests/Test/FixturesTestCase.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Liip/TestFixturesBundle
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\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
17+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
19+
/**
20+
* @internal
21+
*/
22+
abstract class FixturesTestCase extends KernelTestCase
23+
{
24+
protected function tearDown(): void
25+
{
26+
if (!$this->isTestSkipped()) {
27+
$this->removeBackups();
28+
}
29+
30+
parent::tearDown();
31+
}
32+
33+
private function removeBackups(): void
34+
{
35+
$cacheDir = self::getContainer()->getParameter('kernel.cache_dir');
36+
$backups = glob($cacheDir.'/test_{mongodb,mysql,postgresql,sqlite}_*', GLOB_BRACE);
37+
foreach ($backups as $backup) {
38+
$this->removeFile($backup);
39+
AbstractDatabaseTool::clearMetadata();
40+
}
41+
}
42+
43+
private function removeFile(string $filename): void
44+
{
45+
if (!is_dir($filename)) {
46+
unlink($filename);
47+
48+
return;
49+
}
50+
51+
$directory = $filename;
52+
$filenames = scandir($directory);
53+
foreach ($filenames as $filename) {
54+
if (\in_array($filename, ['.', '..'], true)) {
55+
continue;
56+
}
57+
58+
$this->removeFile($filename);
59+
}
60+
61+
rmdir($directory);
62+
}
63+
64+
private function isTestSkipped(): bool
65+
{
66+
if (version_compare(\PHPUnit\Runner\Version::id(), '10.0.0', '<')) {
67+
return \PHPUnit\Runner\BaseTestRunner::STATUS_SKIPPED === $this->getStatus();
68+
}
69+
70+
return $this->status()->isSkipped();
71+
}
72+
}

0 commit comments

Comments
 (0)