|
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\Stub; |
|
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
|
|
|
const CLASS_NAME = Prophet::class; |
|
23
|
|
|
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
|
24 |
|
protected function doBuild(string $fqcn) |
|
48
|
|
|
{ |
|
49
|
24 |
|
return $this->prophet->prophesize($fqcn); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ObjectProphecy $mock |
|
54
|
|
|
* @param Stub $stub |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
23 |
View Code Duplication |
protected function doDecorate($mock, Stub $stub) |
|
58
|
|
|
{ |
|
59
|
23 |
|
$methodName = $stub->getMethodName(); |
|
60
|
23 |
|
$methodValue = $stub->getMethodValue(); |
|
61
|
|
|
|
|
62
|
|
|
/** @var MethodProphecy $partial */ |
|
63
|
23 |
|
$partial = $mock->$methodName(new MaxPriorityToken()); |
|
64
|
23 |
|
$methodValue instanceof \Throwable |
|
65
|
23 |
|
? $partial->willThrow($methodValue) |
|
66
|
23 |
|
: $partial->willReturn($methodValue); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ObjectProphecy $mock |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
* |
|
73
|
|
|
* @throws MockNotCreatedException |
|
74
|
|
|
*/ |
|
75
|
13 |
|
protected function doGet($mock) |
|
76
|
|
|
{ |
|
77
|
|
|
try { |
|
78
|
13 |
|
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
|
|
|
public function call($object, string $name, array $arguments) |
|
90
|
|
|
{ |
|
91
|
|
|
return parent::call($object->reveal(), $name, $arguments); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|