|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context as ContextInterface; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Knp\FriendlyContexts\Dictionary\Backgroundable; |
|
8
|
|
|
use Knp\FriendlyContexts\Dictionary\Taggable; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Context implements ContextInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use Backgroundable, |
|
16
|
|
|
Taggable; |
|
17
|
|
|
|
|
18
|
|
|
protected $config = []; |
|
19
|
|
|
protected $container; |
|
20
|
|
|
|
|
21
|
|
|
public function initialize(array $config, ContainerInterface $container) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->config = array_merge($this->getDefaultOptions(), $config); |
|
24
|
|
|
$this->container = $container; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function getRecordBag() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
return $this->get('friendly.record.bag'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function getEntityHydrator() |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
return $this->get('friendly.entity.hydrator'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function getEntityResolver() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
return $this->get('friendly.entity.resolver'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getTextFormater() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
return $this->get('friendly.text.formater'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getAsserter() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
return $this->get('friendly.asserter'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getGuesserManager() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return $this->get('friendly.guesser.manager'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getObjectReflector() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
return $this->get('friendly.object.reflector'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getFeatureWalker() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
return $this->get('friendly.feature.walker'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function getAliceLoader() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
return $this->get('friendly.alice.fixtures.loader'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Gets Doctrine manager |
|
74
|
|
|
* |
|
75
|
|
|
* @return ObjectManager |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getManager() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->get($this->config['doctrine']['service'])->getManager(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getUniqueCache() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
return $this->get('friendly.unique_cache'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getPageClassResolver() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
return $this->get('friendly.page.resolver'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function getRequestBuilder() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
return $this->get('friendly.builder.request_builder'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function getHttpContentTypeGuesser() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
return $this->get('friendly.http.http_content_type_guesser'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
protected function get($service) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->container->has($service)) { |
|
105
|
|
|
return $this->container->get($service); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) { |
|
109
|
|
|
return $this->getKernel()->getContainer()->get($service); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
throw new ServiceNotFoundException($service); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
View Code Duplication |
protected function getParameter($name) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->container->hasParameter($name)) { |
|
118
|
|
|
return $this->container->getParameter($name); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) { |
|
122
|
|
|
return $this->getKernel()->getContainer()->getParameter($name); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
throw new ParameterNotFoundException($name); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function getKernel() |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->container->has('friendly.symfony.kernel')) { |
|
131
|
|
|
$kernel = $this->container->get('friendly.symfony.kernel'); |
|
132
|
|
|
$kernel->boot(); |
|
133
|
|
|
|
|
134
|
|
|
return $kernel; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function resolveEntity($name) |
|
139
|
|
|
{ |
|
140
|
|
|
$namespaces = $this->getParameter('friendly.entities.namespaces'); |
|
141
|
|
|
|
|
142
|
|
|
$entities = $this |
|
143
|
|
|
->getEntityResolver() |
|
144
|
|
|
->resolve( |
|
145
|
|
|
$this->getManager(), |
|
146
|
|
|
$name, |
|
147
|
|
|
empty($namespaces) ? '' : $namespaces |
|
148
|
|
|
) |
|
149
|
|
|
; |
|
150
|
|
|
|
|
151
|
|
|
switch (true) { |
|
152
|
|
|
case 1 < count($entities): |
|
153
|
|
|
throw new \Exception( |
|
154
|
|
|
sprintf( |
|
155
|
|
|
'Failed to find a unique model from the name "%s", "%s" found', |
|
156
|
|
|
$name, |
|
157
|
|
|
implode('" and "', array_map( |
|
158
|
|
|
function ($rfl) { |
|
159
|
|
|
return $rfl->getName(); |
|
160
|
|
|
}, |
|
161
|
|
|
$entities |
|
162
|
|
|
)) |
|
163
|
|
|
) |
|
164
|
|
|
); |
|
165
|
|
|
break; |
|
|
|
|
|
|
166
|
|
|
case 0 === count($entities): |
|
167
|
|
|
throw new \Exception( |
|
168
|
|
|
sprintf( |
|
169
|
|
|
'Failed to find a model from the name "%s"', |
|
170
|
|
|
$name |
|
171
|
|
|
) |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return current($entities); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function getDefaultOptions() |
|
179
|
|
|
{ |
|
180
|
|
|
return []; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.