diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0b975e9d..15896e0e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,7 @@ xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" colors="true" beStrictAboutOutputDuringTests="true" + executionOrder="random" > diff --git a/src/Services/DatabaseTools/AbstractDatabaseTool.php b/src/Services/DatabaseTools/AbstractDatabaseTool.php index bca47e83..d5a447c1 100644 --- a/src/Services/DatabaseTools/AbstractDatabaseTool.php +++ b/src/Services/DatabaseTools/AbstractDatabaseTool.php @@ -265,4 +265,9 @@ protected function getCacheMetadataParameter() } abstract protected function getPlatformName(): string; + + public static function clearMetadata(): void + { + self::$cachedMetadatas = []; + } } diff --git a/tests/Test/ConfigEventsTest.php b/tests/Test/ConfigEventsTest.php index 8bb064c5..9a10720c 100644 --- a/tests/Test/ConfigEventsTest.php +++ b/tests/Test/ConfigEventsTest.php @@ -21,7 +21,6 @@ use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; /** * Tests that configuration has been loaded and users can be logged in. @@ -38,7 +37,7 @@ * * @internal */ -class ConfigEventsTest extends KernelTestCase +class ConfigEventsTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/ConfigMongodbTest.php b/tests/Test/ConfigMongodbTest.php index 2ad83367..b2769b2c 100755 --- a/tests/Test/ConfigMongodbTest.php +++ b/tests/Test/ConfigMongodbTest.php @@ -11,6 +11,8 @@ * with this source code in the file LICENSE. */ +namespace Liip\Acme\Tests\Test; + use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Doctrine\Common\DataFixtures\ProxyReferenceRepository; use Doctrine\ODM\MongoDB\Repository\DocumentRepository; @@ -21,7 +23,6 @@ use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\MongoDBDatabaseTool; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; /** * Test MongoDB. @@ -36,7 +37,7 @@ * * @internal */ -class ConfigMongodbTest extends KernelTestCase +class ConfigMongodbTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/ConfigMysqlTest.php b/tests/Test/ConfigMysqlTest.php index e80603e9..d275a9fb 100644 --- a/tests/Test/ConfigMysqlTest.php +++ b/tests/Test/ConfigMysqlTest.php @@ -22,7 +22,6 @@ use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMDatabaseTool; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; // BC, needed by "theofidry/alice-data-fixtures: <1.3" not compatible with "doctrine/persistence: ^2.0" if (interface_exists('\Doctrine\Persistence\ObjectManager') @@ -51,7 +50,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence * * @internal */ -class ConfigMysqlTest extends KernelTestCase +class ConfigMysqlTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/ConfigPhpcrTest.php b/tests/Test/ConfigPhpcrTest.php index 3b308a54..27ddfbdc 100644 --- a/tests/Test/ConfigPhpcrTest.php +++ b/tests/Test/ConfigPhpcrTest.php @@ -22,7 +22,6 @@ use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\PHPCRDatabaseTool; use Symfony\Bundle\FrameworkBundle\Console\Application; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Console\Tester\CommandTester; /** @@ -38,7 +37,7 @@ * * @internal */ -class ConfigPhpcrTest extends KernelTestCase +class ConfigPhpcrTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/ConfigSqliteTest.php b/tests/Test/ConfigSqliteTest.php index 68bcb8c0..94ac198a 100644 --- a/tests/Test/ConfigSqliteTest.php +++ b/tests/Test/ConfigSqliteTest.php @@ -28,7 +28,6 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; /** * @runTestsInSeparateProcesses @@ -40,7 +39,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence * * @internal */ -class ConfigSqliteTest extends KernelTestCase +class ConfigSqliteTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/ConfigTest.php b/tests/Test/ConfigTest.php index d7470ca8..7e1d6245 100644 --- a/tests/Test/ConfigTest.php +++ b/tests/Test/ConfigTest.php @@ -22,7 +22,6 @@ use Liip\TestFixturesBundle\Services\DatabaseToolCollection; use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; /** * Tests that configuration has been loaded and users can be logged in. @@ -42,7 +41,7 @@ * * @internal */ -class ConfigTest extends KernelTestCase +class ConfigTest extends FixturesTestCase { use ContainerProvider; diff --git a/tests/Test/FixturesTestCase.php b/tests/Test/FixturesTestCase.php new file mode 100644 index 00000000..b1c6ea7c --- /dev/null +++ b/tests/Test/FixturesTestCase.php @@ -0,0 +1,72 @@ + + * + * 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\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; + +/** + * @internal + */ +abstract class FixturesTestCase extends KernelTestCase +{ + protected function tearDown(): void + { + if (!$this->isTestSkipped()) { + $this->removeBackups(); + } + + parent::tearDown(); + } + + private function removeBackups(): void + { + $cacheDir = self::getContainer()->getParameter('kernel.cache_dir'); + $backups = glob($cacheDir.'/test_{mongodb,mysql,postgresql,sqlite}_*', GLOB_BRACE); + foreach ($backups as $backup) { + $this->removeFile($backup); + AbstractDatabaseTool::clearMetadata(); + } + } + + private function removeFile(string $filename): void + { + if (!is_dir($filename)) { + unlink($filename); + + return; + } + + $directory = $filename; + $filenames = scandir($directory); + foreach ($filenames as $filename) { + if (\in_array($filename, ['.', '..'], true)) { + continue; + } + + $this->removeFile($filename); + } + + rmdir($directory); + } + + private function isTestSkipped(): bool + { + if (version_compare(\PHPUnit\Runner\Version::id(), '10.0.0', '<')) { + return \PHPUnit\Runner\BaseTestRunner::STATUS_SKIPPED === $this->getStatus(); + } + + return $this->status()->isSkipped(); + } +}