Browse Source

DX: do not allow overriding constructor of `PHPUnit\Framework\TestCase` (#7563)

Kuba Werłos 1 year ago
parent
commit
fd3b0a2946
2 changed files with 21 additions and 8 deletions
  1. 11 8
      tests/Smoke/InstallViaComposerTest.php
  2. 10 0
      tests/TestCase.php

+ 11 - 8
tests/Smoke/InstallViaComposerTest.php

@@ -32,7 +32,7 @@ use Symfony\Component\Filesystem\Filesystem;
  */
 final class InstallViaComposerTest extends AbstractSmokeTestCase
 {
-    private Filesystem $fs;
+    private ?Filesystem $fs;
 
     /** @var array<string, mixed> */
     private array $currentCodeAsComposerDependency = [
@@ -64,13 +64,6 @@ final class InstallViaComposerTest extends AbstractSmokeTestCase
         'vendor/bin/php-cs-fixer fix --help',
     ];
 
-    public function __construct()
-    {
-        $this->fs = new Filesystem();
-
-        parent::__construct();
-    }
-
     public static function setUpBeforeClass(): void
     {
         parent::setUpBeforeClass();
@@ -98,6 +91,16 @@ final class InstallViaComposerTest extends AbstractSmokeTestCase
         }
     }
 
+    protected function setUp(): void
+    {
+        $this->fs = new Filesystem();
+    }
+
+    protected function tearDown(): void
+    {
+        $this->fs = null;
+    }
+
     public function testInstallationViaPathIsPossible(): void
     {
         $tmpPath = $this->createFakeComposerProject($this->currentCodeAsComposerDependency);

+ 10 - 0
tests/TestCase.php

@@ -25,4 +25,14 @@ use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
 abstract class TestCase extends BaseTestCase
 {
     use ExpectDeprecationTrait;
+
+    final public function testNotDefiningConstructor(): void
+    {
+        $reflection = new \ReflectionObject($this);
+
+        self::assertNotSame(
+            $reflection->getConstructor()->getDeclaringClass()->getName(),
+            $reflection->getName(),
+        );
+    }
 }