A PHPStan plugin to forbid mocking specific interfaces or classes.
Add the repository to your composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "[email protected]:NormanAlbert91/mock-check.git"
}
]
}Then install the package:
composer require --dev na/mock-check:dev-mainIf you use phpstan/extension-installer, the plugin is automatically registered. Otherwise, add to your phpstan.neon:
includes:
- vendor/na/mock-check/extension.neonAdd forbidden patterns to your phpstan.neon:
parameters:
mockCheck:
forbiddenPatterns:
-
pattern: 'Psr\'
message: 'PSR interfaces should not be mocked - use real implementations.'
-
pattern: 'Doctrine\ORM\EntityManagerInterface'
message: 'Use the in-memory repository instead of mocking EntityManager.'
-
pattern: 'App\Domain\*'| Pattern | Description |
|---|---|
Psr\ |
Trailing backslash: matches everything starting with Psr\ |
Psr\Log |
Without backslash: matches everything under Psr\Log\ |
Psr\Log\* |
Wildcard: regex-style matching |
Psr\Log\LoggerInterface |
Exact match |
The message parameter is optional. If omitted, a default message is shown:
Mocking Psr\Log\LoggerInterface is forbidden (matches pattern "Psr").
The plugin detects the following PHPUnit mock methods:
createMock()createStub()createPartialMock()createConfiguredMock()createTestProxy()getMockBuilder()getMock()getMockForAbstractClass()getMockForTrait()