1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Plugin\Prophecy; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\MissingDependencyException; |
7
|
|
|
use Moka\Exception\MockNotCreatedException; |
8
|
|
|
use Moka\Plugin\Prophecy\Token\MaxPriorityToken; |
9
|
|
|
use Moka\Strategy\AbstractMockingStrategy; |
10
|
|
|
use Moka\Stub\MethodStub; |
11
|
|
|
use Prophecy\Exception\Prophecy\ObjectProphecyException; |
12
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use Prophecy\Prophet; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ProphecyMockingStrategy |
18
|
|
|
* @package Moka\Strategy |
19
|
|
|
*/ |
20
|
|
|
class ProphecyMockingStrategy extends AbstractMockingStrategy |
21
|
|
|
{ |
22
|
|
|
private const CLASS_NAME = Prophet::class; |
23
|
|
|
private const PACKAGE_NAME = 'phpspec/prophecy'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Prophet |
27
|
|
|
*/ |
28
|
|
|
private $prophet; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* PHPUnitMockingStrategy constructor. |
32
|
|
|
* |
33
|
|
|
* @throws MissingDependencyException |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct() |
36
|
|
|
{ |
37
|
2 |
|
self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME); |
38
|
|
|
|
39
|
2 |
|
$this->prophet = new Prophet(); |
40
|
2 |
|
$this->setMockType(ObjectProphecy::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $fqcn |
45
|
|
|
* @return ObjectProphecy |
46
|
|
|
*/ |
47
|
22 |
|
protected function doBuild(string $fqcn): ObjectProphecy |
48
|
|
|
{ |
49
|
22 |
|
return $this->prophet->prophesize($fqcn); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ObjectProphecy $mock |
54
|
|
|
* @param MethodStub $stub |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
20 |
|
protected function doDecorateWithMethod($mock, MethodStub $stub): void |
58
|
|
|
{ |
59
|
20 |
|
$methodName = $stub->getName(); |
60
|
20 |
|
$methodValue = $stub->getValue(); |
61
|
|
|
|
62
|
|
|
/** @var MethodProphecy $partial */ |
63
|
20 |
|
$partial = $mock->$methodName(new MaxPriorityToken()); |
64
|
20 |
|
$methodValue instanceof \Throwable |
65
|
20 |
|
? $partial->willThrow($methodValue) |
66
|
20 |
|
: $partial->willReturn($methodValue); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ObjectProphecy $mock |
71
|
|
|
* @return mixed |
72
|
|
|
* |
73
|
|
|
* @throws MockNotCreatedException |
74
|
|
|
*/ |
75
|
16 |
|
protected function doGet($mock) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
16 |
|
return $mock->reveal(); |
79
|
1 |
|
} catch (ObjectProphecyException $exception) { |
80
|
1 |
|
throw new MockNotCreatedException( |
81
|
1 |
|
sprintf( |
82
|
1 |
|
'Cannot create mock object: %s', |
83
|
1 |
|
$exception->getMessage() |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param ObjectProphecy $mock |
91
|
|
|
* @param string $methodName |
92
|
|
|
* @return object |
93
|
|
|
*/ |
94
|
2 |
|
protected function doCall($mock, string $methodName) |
95
|
|
|
{ |
96
|
2 |
|
return new MethodProphecyHelper($mock, $methodName); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|