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; |
10
|
|
|
use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PHPUnitMockingStrategy |
15
|
|
|
* @package Moka\Strategy |
16
|
|
|
*/ |
17
|
|
|
class PHPUnitMockingStrategy extends AbstractMockingStrategy |
18
|
|
|
{ |
19
|
|
|
private const CLASS_NAME = Generator::class; |
20
|
|
|
private const PACKAGE_NAME = 'phpunit/phpunit-mock-objects'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Generator |
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 Generator(); |
37
|
6 |
|
$this->setMockType(MockObject::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $fqcn |
42
|
|
|
* @return MockObject |
43
|
|
|
* |
44
|
|
|
* @throws \PHPUnit\Framework\Exception |
45
|
|
|
* @throws \PHPUnit\Framework\MockObject\RuntimeException |
46
|
|
|
* @throws \ReflectionException |
47
|
|
|
*/ |
48
|
27 |
|
protected function doBuild(string $fqcn): MockObject |
49
|
|
|
{ |
50
|
27 |
|
return $this->generator->getMock( |
51
|
27 |
|
$fqcn, |
52
|
27 |
|
$methods = [], |
53
|
27 |
|
$arguments = [], |
54
|
27 |
|
$mockClassName = '', |
55
|
27 |
|
$callOriginalConstructor = false |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param MockObject $mock |
61
|
|
|
* @param MethodStub $stub |
62
|
|
|
* |
63
|
|
|
* @throws \PHPUnit\Framework\MockObject\RuntimeException |
64
|
|
|
*/ |
65
|
20 |
|
protected function doDecorateWithMethod($mock, MethodStub $stub): void |
66
|
|
|
{ |
67
|
20 |
|
$methodName = $stub->getName(); |
68
|
20 |
|
$methodValue = $stub->getValue(); |
69
|
|
|
|
70
|
20 |
|
$partial = $mock->expects(new AnyInvokedCount())->method($methodName); |
71
|
20 |
|
$methodValue instanceof \Throwable |
72
|
20 |
|
? $partial->willThrowException($methodValue) |
73
|
20 |
|
: $partial->willReturn($methodValue); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param MockObject $mock |
78
|
|
|
* @return MockObject |
79
|
|
|
*/ |
80
|
22 |
|
protected function doGet($mock): MockObject |
81
|
|
|
{ |
82
|
22 |
|
return $mock; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|