1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RDV\SymfonyContainerMocks\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Prophecy\Prophet; |
6
|
|
|
use Symfony\Component\DependencyInjection\Container; |
7
|
|
|
|
8
|
|
|
class TestContainer extends Container |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $mocked = array(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Prophet |
17
|
|
|
*/ |
18
|
|
|
protected $prophet; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $id The service identifier |
22
|
|
|
* @param string|null $class Class or interface fully qualified name |
23
|
|
|
* @return \Prophecy\Prophecy\ObjectProphecy |
24
|
|
|
* @throws \InvalidArgumentException |
25
|
|
|
* @throws \BadMethodCallException |
26
|
|
|
* @throws \Prophecy\Exception\Prophecy\ObjectProphecyException |
27
|
|
|
*/ |
28
|
|
|
public function prophesize($id, $class = null) |
29
|
|
|
{ |
30
|
|
|
if (array_key_exists($id, $this->mocked)) { |
31
|
|
|
throw new \InvalidArgumentException('This service already mocked and can have references'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (empty($class)) { |
35
|
|
|
$class = $this->detectClass($id); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$mock = $this->getProphet()->prophesize($class); |
39
|
|
|
$this->mocked[$id] = $mock->reveal(); |
40
|
|
|
|
41
|
|
|
return $mock; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Remove all mocked services |
46
|
|
|
*/ |
47
|
|
|
public function tearDown() |
48
|
|
|
{ |
49
|
|
|
$this->mocked = array(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function reset() |
56
|
|
|
{ |
57
|
|
|
if (interface_exists('Symfony\Component\DependencyInjection\ResettableContainerInterface') |
58
|
|
|
&& $this instanceof \Symfony\Component\DependencyInjection\ResettableContainerInterface) { |
59
|
|
|
parent::reset(); |
60
|
|
|
} |
61
|
|
|
$this->mocked = array(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $id |
66
|
|
|
* @param mixed $mock |
67
|
|
|
*/ |
68
|
|
|
public function setMock($id, $mock) |
69
|
|
|
{ |
70
|
|
|
$this->mocked[$id] = $mock; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $id |
75
|
|
|
*/ |
76
|
|
|
public function unMock($id) |
77
|
|
|
{ |
78
|
|
|
unset($this->mocked[$id]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) |
85
|
|
|
{ |
86
|
|
|
if (array_key_exists($id, $this->mocked)) { |
87
|
|
|
return $this->mocked[$id]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return parent::get($id, $invalidBehavior); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function has($id) |
97
|
|
|
{ |
98
|
|
|
if (array_key_exists($id, $this->mocked)) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::has($id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function initialized($id) |
109
|
|
|
{ |
110
|
|
|
if (array_key_exists($id, $this->mocked)) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return parent::initialized($id); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getMockedServices() |
121
|
|
|
{ |
122
|
|
|
return $this->mocked; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Prophet |
127
|
|
|
*/ |
128
|
|
|
protected function getProphet() |
129
|
|
|
{ |
130
|
|
|
if (!$this->prophet) { |
131
|
|
|
$this->prophet = new Prophet(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->prophet; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $service |
139
|
|
|
* @return string |
140
|
|
|
* @throws \BadMethodCallException |
141
|
|
|
*/ |
142
|
|
|
protected function detectClass($service) |
143
|
|
|
{ |
144
|
|
|
return DefinitionLoader::getClassName($service, $this); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|