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