Rule php_unit_namespaced¶
PHPUnit classes MUST be used in namespaced version, e.g.
\PHPUnit\Framework\TestCase instead of \PHPUnit_Framework_TestCase.
Description¶
PHPUnit v6 has finally fully switched to namespaces.
You could start preparing the upgrade by switching from non-namespaced TestCase
to namespaced one.
Forward compatibility layer (\PHPUnit\Framework\TestCase class) was
backported to PHPUnit v4.8.35 and PHPUnit v5.4.0.
Extended forward compatibility layer (PHPUnit\Framework\Assert,
PHPUnit\Framework\BaseTestListener, PHPUnit\Framework\TestListener
classes) was introduced in v5.7.0.
Warnings¶
This rule is RISKY¶
Risky when PHPUnit classes are overridden or not accessible, or when project has PHPUnit incompatibilities.
This rule is CONFIGURABLE¶
You can configure this rule using the following option: target.
Configuration¶
target¶
Target version of PHPUnit.
Allowed values: '4.8', '5.7', '6.0' and 'newest'
Default value: 'newest'
Examples¶
Example #1¶
Default configuration.
--- Original
+++ New
<?php
-final class MyTest extends \PHPUnit_Framework_TestCase
+final class MyTest extends \PHPUnit\Framework\TestCase
{
public function testSomething()
{
- PHPUnit_Framework_Assert::assertTrue(true);
+ PHPUnit\Framework\Assert::assertTrue(true);
}
}
Example #2¶
With configuration: ['target' => '4.8'].
--- Original
+++ New
<?php
-final class MyTest extends \PHPUnit_Framework_TestCase
+final class MyTest extends \PHPUnit\Framework\TestCase
{
public function testSomething()
{
PHPUnit_Framework_Assert::assertTrue(true);
}
}
Rule sets¶
The rule is part of the following rule sets:
@PHPUnit4x8Migration:risky with config:
['target' => '4.8']@PHPUnit5x0Migration:risky with config:
['target' => '4.8']@PHPUnit5x2Migration:risky with config:
['target' => '4.8']@PHPUnit5x4Migration:risky with config:
['target' => '4.8']@PHPUnit5x5Migration:risky with config:
['target' => '4.8']@PHPUnit5x6Migration:risky with config:
['target' => '4.8']@PHPUnit5x7Migration:risky with config:
['target' => '5.7']@PHPUnit6x0Migration:risky with config:
['target' => '6.0']@PHPUnit7x5Migration:risky with config:
['target' => '6.0']@PHPUnit8x4Migration:risky with config:
['target' => '6.0']@PHPUnit9x1Migration:risky with config:
['target' => '6.0']@PHPUnit10x0Migration:risky with config:
['target' => '6.0']@PHPUnit48Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit50Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit52Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit54Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit55Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit56Migration:risky (deprecated) with config:
['target' => '4.8']@PHPUnit57Migration:risky (deprecated) with config:
['target' => '5.7']@PHPUnit60Migration:risky (deprecated) with config:
['target' => '6.0']@PHPUnit75Migration:risky (deprecated) with config:
['target' => '6.0']@PHPUnit84Migration:risky (deprecated) with config:
['target' => '6.0']@PHPUnit91Migration:risky (deprecated) with config:
['target' => '6.0']@PHPUnit100Migration:risky (deprecated) with config:
['target' => '6.0']
References¶
Fixer class: PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer
Test class: PhpCsFixer\Tests\Fixer\PhpUnit\PhpUnitNamespacedFixerTest
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.