From c0be3dac09fcd7d1f78be3e42a5cfa896607110e Mon Sep 17 00:00:00 2001 From: Sebastien Curt <sebastien.curt@tetras-libre.fr> Date: Tue, 12 Apr 2022 11:40:50 +0200 Subject: [PATCH] Test code refactoring --- tests/TestFileHelper.php | 23 ++++++++++++++++++++++ tests/functional/CapsuleControllerTest.php | 10 ++-------- tests/functional/ProjectControllerTest.php | 10 ++-------- 3 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 tests/TestFileHelper.php diff --git a/tests/TestFileHelper.php b/tests/TestFileHelper.php new file mode 100644 index 0000000..168514a --- /dev/null +++ b/tests/TestFileHelper.php @@ -0,0 +1,23 @@ +<?php + +namespace App\Tests; + +use PHPUnit\Framework\Assert; +use Symfony\Component\Filesystem\Filesystem; + +class TestFileHelper +{ + private const LEGACY_DIRECTORY = __DIR__ . '/../legacy'; + + public static function createLegacyDirectoryIfNecessary(Assert $testCase): void + { + $fileSystem = new Filesystem(); + if (!$fileSystem->exists(self::LEGACY_DIRECTORY)) { + $fileSystem->mkdir(self::LEGACY_DIRECTORY, 0644); + } + $testCase->assertTrue( + $fileSystem->exists(self::LEGACY_DIRECTORY), + 'The directory \'' . self::LEGACY_DIRECTORY . '\' should exists' + ); + } +} diff --git a/tests/functional/CapsuleControllerTest.php b/tests/functional/CapsuleControllerTest.php index 804c5ae..5087c1d 100644 --- a/tests/functional/CapsuleControllerTest.php +++ b/tests/functional/CapsuleControllerTest.php @@ -4,6 +4,7 @@ namespace App\Tests\functional; use App\Entity\Capsule; use App\Entity\User; +use App\Tests\TestFileHelper; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\Filesystem\Filesystem; @@ -16,14 +17,7 @@ class CapsuleControllerTest extends WebTestCase protected function setUp(): void { - $fileSystem = new Filesystem(); - if (!$fileSystem->exists(self::TEST_DIR_PATH)) { - $fileSystem->mkdir(self::TEST_DIR_PATH, 0644); - } - $this->assertTrue( - $fileSystem->exists(self::TEST_DIR_PATH), - 'The directory \'' . self::TEST_DIR_PATH . '\' should exists' - ); + TestFileHelper::createLegacyDirectoryIfNecessary($this); chdir(self::TEST_DIR_PATH); self::ensureKernelShutdown(); diff --git a/tests/functional/ProjectControllerTest.php b/tests/functional/ProjectControllerTest.php index e7daa45..0eaf29b 100644 --- a/tests/functional/ProjectControllerTest.php +++ b/tests/functional/ProjectControllerTest.php @@ -4,6 +4,7 @@ namespace App\Tests\functional; use App\Entity\Capsule; use App\Entity\User; +use App\Tests\TestFileHelper; use Doctrine\Persistence\ObjectManager; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -21,14 +22,7 @@ class ProjectControllerTest extends WebTestCase protected function setUp(): void { - $fileSystem = new Filesystem(); - if (!$fileSystem->exists(self::TEST_DIR_PATH)) { - $fileSystem->mkdir(self::TEST_DIR_PATH, 0644); - } - $this->assertTrue( - $fileSystem->exists(self::TEST_DIR_PATH), - 'The directory \'' . self::TEST_DIR_PATH . '\' should exists' - ); + TestFileHelper::createLegacyDirectoryIfNecessary($this); chdir(self::TEST_DIR_PATH); self::ensureKernelShutdown(); -- GitLab