Rule php_unit_mock¶
Usages of ->getMock and ->getMockWithoutInvokingTheOriginalConstructor
methods MUST be replaced by ->createMock or ->createPartialMock methods.
Warning¶
Using this rule is risky¶
Risky when PHPUnit classes are overridden or not accessible, or when project has PHPUnit incompatibilities.
Configuration¶
target¶
Target version of PHPUnit.
Allowed values: '5.4', '5.5' and 'newest'
Default value: 'newest'
Examples¶
Example #1¶
Default configuration.
--- Original
+++ New
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
- $mock = $this->getMockWithoutInvokingTheOriginalConstructor("Foo");
- $mock1 = $this->getMock("Foo");
- $mock1 = $this->getMock("Bar", ["aaa"]);
+ $mock = $this->createMock("Foo");
+ $mock1 = $this->createMock("Foo");
+ $mock1 = $this->createPartialMock("Bar", ["aaa"]);
$mock1 = $this->getMock("Baz", ["aaa"], ["argument"]); // version with more than 2 params is not supported
}
}
Example #2¶
With configuration: ['target' => '5.4'].
--- Original
+++ New
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
- $mock1 = $this->getMock("Foo");
+ $mock1 = $this->createMock("Foo");
$mock1 = $this->getMock("Bar", ["aaa"]); // version with multiple params is not supported
}
}
Rule sets¶
The rule is part of the following rule sets:
@PHPUnit5x4Migration:risky with config:
['target' => '5.4']@PHPUnit5x5Migration:risky with config:
['target' => '5.5']@PHPUnit5x6Migration:risky with config:
['target' => '5.5']@PHPUnit5x7Migration:risky with config:
['target' => '5.5']@PHPUnit6x0Migration:risky with config:
['target' => '5.5']@PHPUnit7x5Migration:risky with config:
['target' => '5.5']@PHPUnit8x4Migration:risky with config:
['target' => '5.5']@PHPUnit9x1Migration:risky with config:
['target' => '5.5']@PHPUnit10x0Migration:risky with config:
['target' => '5.5']@PHPUnit54Migration:risky (deprecated) with config:
['target' => '5.4']@PHPUnit55Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit56Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit57Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit60Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit75Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit84Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit91Migration:risky (deprecated) with config:
['target' => '5.5']@PHPUnit100Migration:risky (deprecated) with config:
['target' => '5.5']
References¶
Fixer class: PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer
Test class: PhpCsFixer\Tests\Fixer\PhpUnit\PhpUnitMockFixerTest
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.