|
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
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait ProxyTrait |
|
12
|
|
|
* @package Moka\Proxy |
|
13
|
|
|
*/ |
|
14
|
|
|
trait ProxyTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var object |
|
18
|
|
|
*/ |
|
19
|
|
|
private $__moka_mock; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var MockingStrategyInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $__moka_mockingStrategy; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return object |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __moka_getMock() |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $this->__moka_mock; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param object $mock |
|
36
|
|
|
* @return ProxyInterface|ProxyTrait |
|
37
|
|
|
*/ |
|
38
|
16 |
|
public function __moka_setMock($mock): self |
|
39
|
|
|
{ |
|
40
|
16 |
|
$this->__moka_mock = $mock; |
|
41
|
|
|
|
|
42
|
16 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param MockingStrategyInterface $mockingStrategy |
|
47
|
|
|
* @return ProxyInterface|ProxyTrait |
|
48
|
|
|
*/ |
|
49
|
14 |
|
public function __moka_setMockingStrategy(MockingStrategyInterface $mockingStrategy): self |
|
50
|
|
|
{ |
|
51
|
14 |
|
$this->__moka_mockingStrategy = $mockingStrategy; |
|
52
|
|
|
|
|
53
|
14 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $namesWithValues |
|
58
|
|
|
* @return ProxyInterface |
|
59
|
|
|
* |
|
60
|
|
|
* @throws InvalidArgumentException |
|
61
|
|
|
* @throws MockNotCreatedException |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function stub(array $namesWithValues): ProxyInterface |
|
64
|
|
|
{ |
|
65
|
|
|
/** @var $this ProxyInterface */ |
|
66
|
1 |
|
$this->__moka_mockingStrategy->decorate($this->__moka_mock, $namesWithValues); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @param array $arguments |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
6 |
|
protected function doCall(string $name, array $arguments) |
|
77
|
|
|
{ |
|
78
|
6 |
|
if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) { |
|
79
|
1 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
5 |
|
$target = $this->__moka_mockingStrategy->get($this->__moka_mock); |
|
83
|
|
|
|
|
84
|
5 |
|
return $target->$name(...$arguments); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
3 |
|
protected function doGet(string $name) |
|
92
|
|
|
{ |
|
93
|
3 |
|
if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) { |
|
94
|
1 |
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
return $this->__moka_mockingStrategy->call($this->__moka_mock, $name); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: