ClassMockingCorrectness
error on by defaultGroup: Probable bugs · PhpStorm name: ClassMockingCorrectnessInspection
Test doubles are generated by subclassing: final classes cannot be mocked, traits and abstract classes need dedicated factory methods, and mocks built with the constructor enabled need the constructor's required arguments. Point at the class argument of PHPUnit / Prophecy mocking calls (and PhpSpec collaborator type hints) that will fail at runtime.
Example
php
<?php
namespace PhpSpec {
class ObjectBehavior {}
}
namespace PHPUnit\Framework {
class TestCase
{
public function getMockBuilder($c) {}
public function getMockForTrait($c) {}
public function getMockForAbstractClass($c) {}
public function getMockClass($c) {}
public function createMock($c) {}
}
}
namespace {
class PHPUnit_Framework_MockObject_Generator
{
public function getMock($c) {}
}
final class Sealed {}
abstract class Blueprint {}
trait Mixin {}
interface Contract {}
class Gateway { public function __construct($dsn, $retries = 3) {} }
class Relaxed { public function __construct($dsn = '') {} }
class OrderTest extends \PHPUnit\Framework\TestCase
{
public function testDoubles()
{
$this->createMock(Sealed::class);
$this->createMock(Mixin::class);
$this->createMock(Contract::class);
$this->getMockBuilder('Sealed');
$this->getMockBuilder("\\Sealed");
$this->getMockBuilder('Blueprint');
$this->getMockBuilder(Blueprint::class);
$this->getMockBuilder(Blueprint::class)->getMockForAbstractClass();
$this->getMockBuilder(Mixin::class);
$this->getMockBuilder(Mixin::class)->getMockForTrait();
$this->getMockBuilder(Contract::class);
$this->getMockForTrait(Blueprint::class);
$this->getMockForAbstractClass(Gateway::class);
$this->getMockForAbstractClass(Contract::class);
$this->getMockClass(Sealed::class);
$this->getMockBuilder(Gateway::class)->getMock();
$this->getMockBuilder(Relaxed::class)->getMock();
$this->getMockBuilder(Gateway::class)->disableOriginalConstructor()->getMock();
}
}
$generator = new PHPUnit_Framework_MockObject_Generator();
$generator->getMock(Sealed::class);
$generator->getMock(ArrayObject::class);
class GatewaySpec extends \PhpSpec\ObjectBehavior
{
function it_charges(Sealed $card, Relaxed $log) {}
}
}Reported:
- line 35: Final classes cannot be mocked.
- line 36: Traits cannot be mocked this way.
- line 38: Final classes cannot be mocked.
- line 39: Final classes cannot be mocked.
- line 41: Abstract class: build the double with getMockForAbstractClass().
- line 43: Trait: build the double with getMockForTrait().
- line 46: This factory expects a trait.
- line 47: This factory expects an abstract class.
- line 49: Final classes cannot be mocked.
- line 50: Mocked constructor needs arguments; pass them or disable the constructor.
- line 57: Final classes cannot be mocked.
- line 62: Final classes cannot be mocked.
Configure
In custos.json:
json
{
"rules": {
"ClassMockingCorrectness": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore ClassMockingCorrectness
/**
* @noinspection ClassMockingCorrectnessInspection
*/