1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Proxy; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidArgumentException; |
7
|
|
|
use Moka\Exception\MockNotCreatedException; |
8
|
|
|
use Moka\Strategy\MockingStrategyInterface; |
9
|
|
|
use function Moka\Stub\Helper\isMethodName; |
10
|
|
|
use function Moka\Stub\Helper\isPropertyName; |
11
|
|
|
use function Moka\Stub\Helper\stripNameAndValidate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait ProxyTrait |
15
|
|
|
* @package Moka\Proxy |
16
|
|
|
*/ |
17
|
|
|
trait ProxyTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var object |
21
|
|
|
*/ |
22
|
|
|
private $__moka_mock; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MockingStrategyInterface |
26
|
|
|
*/ |
27
|
|
|
private $__moka_mockingStrategy; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $__moka_properties = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $__moka_methods = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return object |
41
|
|
|
*/ |
42
|
4 |
|
public function __moka_getMock() |
43
|
|
|
{ |
44
|
4 |
|
return $this->__moka_mock; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param object $mock |
49
|
|
|
* @return ProxyInterface|ProxyTrait |
50
|
|
|
*/ |
51
|
17 |
|
public function __moka_setMock($mock): self |
52
|
|
|
{ |
53
|
17 |
|
$this->__moka_mock = $mock; |
54
|
|
|
|
55
|
17 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param MockingStrategyInterface $mockingStrategy |
60
|
|
|
* @return ProxyInterface|ProxyTrait |
61
|
|
|
*/ |
62
|
15 |
|
public function __moka_setMockingStrategy(MockingStrategyInterface $mockingStrategy): self |
63
|
|
|
{ |
64
|
15 |
|
$this->__moka_mockingStrategy = $mockingStrategy; |
65
|
|
|
|
66
|
15 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $namesWithValues |
71
|
|
|
* @return ProxyInterface |
72
|
|
|
* |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
* @throws MockNotCreatedException |
75
|
|
|
*/ |
76
|
2 |
|
public function stub(array $namesWithValues): ProxyInterface |
77
|
|
|
{ |
78
|
2 |
|
foreach ($namesWithValues as $name => $value) { |
79
|
2 |
|
if (isPropertyName($name)) { |
80
|
1 |
|
$this->__moka_properties[] = stripNameAndValidate($name); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if (isMethodName($name)) { |
84
|
1 |
|
$this->__moka_methods[] = stripNameAndValidate($name); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var $this ProxyInterface */ |
89
|
2 |
|
$this->__moka_mockingStrategy->decorate($this->__moka_mock, $namesWithValues); |
90
|
|
|
|
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* @param array $arguments |
97
|
|
|
* @return mixed |
98
|
|
|
* @throws InvalidArgumentException |
99
|
|
|
* @throws MockNotCreatedException |
100
|
|
|
*/ |
101
|
6 |
|
protected function doCall(string $name, array $arguments) |
102
|
|
|
{ |
103
|
6 |
|
if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) { |
104
|
1 |
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
$target = $this->__moka_mockingStrategy->get($this->__moka_mock); |
108
|
|
|
|
109
|
5 |
|
return $target->$name(...$arguments); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $name |
114
|
|
|
* @return mixed |
115
|
|
|
* @throws InvalidArgumentException |
116
|
|
|
* @throws MockNotCreatedException |
117
|
|
|
*/ |
118
|
4 |
|
protected function doGet(string $name) |
119
|
|
|
{ |
120
|
4 |
|
if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) { |
121
|
1 |
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
if (\in_array($name, $this->__moka_properties, $strict = false)) { |
125
|
1 |
|
return $this->__moka_mockingStrategy->get($this->__moka_mock)->$name; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
return $this->__moka_mockingStrategy->call($this->__moka_mock, $name); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|