1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Plugin\PHPUnit; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\MissingDependencyException; |
7
|
|
|
use Moka\Strategy\AbstractMockingStrategy; |
8
|
|
|
use Moka\Stub\MethodStub; |
9
|
|
|
use PHPUnit_Framework_MockObject_Generator as MockGenerator; |
10
|
|
|
use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount as AnyInvokedCountMatcher; |
11
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PHPUnitMockingStrategy |
15
|
|
|
* @package Moka\Strategy |
16
|
|
|
*/ |
17
|
|
|
class PHPUnitMockingStrategy extends AbstractMockingStrategy |
18
|
|
|
{ |
19
|
|
|
const CLASS_NAME = MockGenerator::class; |
20
|
|
|
const PACKAGE_NAME = 'phpunit/phpunit-mock-object'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var MockGenerator |
24
|
|
|
*/ |
25
|
|
|
private $generator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* PHPUnitMockingStrategy constructor. |
29
|
|
|
* |
30
|
|
|
* @throws MissingDependencyException |
31
|
|
|
*/ |
32
|
6 |
|
public function __construct() |
33
|
|
|
{ |
34
|
6 |
|
self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME); |
35
|
|
|
|
36
|
6 |
|
$this->generator = new MockGenerator(); |
37
|
6 |
|
$this->setMockType(MockObject::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $fqcn |
42
|
|
|
* @return MockObject |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
* @throws \PHPUnit_Framework_MockObject_RuntimeException |
46
|
|
|
*/ |
47
|
29 |
|
protected function doBuild(string $fqcn) |
48
|
|
|
{ |
49
|
29 |
|
return $this->generator->getMock( |
50
|
29 |
|
$fqcn, |
51
|
29 |
|
$methods = [], |
52
|
29 |
|
$arguments = [], |
53
|
29 |
|
$mockClassName = '', |
54
|
29 |
|
$callOriginalConstructor = false |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param MockObject $mock |
60
|
|
|
* @param MethodStub $stub |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
22 |
View Code Duplication |
protected function doDecorateWithMethod($mock, MethodStub $stub) |
|
|
|
|
64
|
|
|
{ |
65
|
22 |
|
$methodName = $stub->getName(); |
66
|
22 |
|
$methodValue = $stub->getValue(); |
67
|
|
|
|
68
|
22 |
|
$partial = $mock->expects(new AnyInvokedCountMatcher())->method($methodName); |
69
|
22 |
|
$methodValue instanceof \Throwable |
70
|
22 |
|
? $partial->willThrowException($methodValue) |
71
|
22 |
|
: $partial->willReturn($methodValue); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param MockObject $mock |
76
|
|
|
* @return MockObject |
77
|
|
|
*/ |
78
|
18 |
|
protected function doGet($mock) |
79
|
|
|
{ |
80
|
18 |
|
return $mock; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.