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