1 | <?php |
||
18 | class Module extends Framework |
||
19 | { |
||
20 | /** |
||
21 | * Required configuration fields |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $requiredFields = ['container']; |
||
25 | |||
26 | /** |
||
27 | * @var ContainerInterface |
||
28 | */ |
||
29 | public $container; |
||
30 | |||
31 | /** |
||
32 | * @var ServerRequestInterface |
||
33 | */ |
||
34 | public $baseRequest; |
||
35 | |||
36 | /** |
||
37 | * @var ResponseInterface |
||
38 | */ |
||
39 | public $baseResponse; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Load the container by including the file. |
||
44 | * @codeCoverageIgnore |
||
45 | * |
||
46 | * @param string $file |
||
47 | * @return ContainerInterface |
||
48 | */ |
||
49 | protected function loadContainer($file) |
||
50 | { |
||
51 | return include $file; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get the container. |
||
56 | * |
||
57 | * @return ContainerInterface |
||
58 | */ |
||
59 | 3 | protected function initContainer() |
|
60 | { |
||
61 | 3 | $container = $this->loadContainer(Configuration::projectDir() . $this->config['container']); |
|
62 | |||
63 | 3 | if (!$container instanceof ContainerInterface) { |
|
64 | 1 | throw new \UnexpectedValueException("Failed to get a container from '{$this->config['container']}'"); |
|
65 | } |
||
66 | |||
67 | 2 | return $container; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Check if the response writes to the output buffer |
||
72 | * |
||
73 | * @return boolean |
||
74 | */ |
||
75 | 7 | protected function usesOutputBuffer() |
|
76 | { |
||
77 | 7 | return isset($this->baseResponse) && $this->baseResponse->getBody()->getMetadata('uri') === 'php://output'; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Enable output buffering |
||
82 | * |
||
83 | * @throws \RuntimeException |
||
84 | */ |
||
85 | 2 | protected function startOutputBuffering() |
|
86 | { |
||
87 | 2 | if ($this->obGetLevel() === 0) { |
|
88 | 2 | $this->obStart(); |
|
89 | } |
||
90 | |||
91 | 2 | if ($this->obGetLevel() < 1) { |
|
92 | 1 | throw new \RuntimeException("Failed to start output buffering"); |
|
93 | } |
||
94 | 1 | } |
|
95 | |||
96 | /** |
||
97 | * Disable output buffering |
||
98 | */ |
||
99 | 1 | protected function stopOutputBuffering() |
|
100 | { |
||
101 | 1 | $this->obClean(); |
|
102 | 1 | } |
|
103 | |||
104 | |||
105 | /** |
||
106 | * Initialize the module |
||
107 | */ |
||
108 | 3 | public function _initialize() |
|
109 | { |
||
110 | 3 | $this->container = $this->initContainer(); |
|
111 | |||
112 | 2 | if ($this->container->has(ServerRequestInterface::class)) { |
|
113 | 1 | $this->baseRequest = $this->container->get(ServerRequestInterface::class); |
|
114 | } |
||
115 | |||
116 | 2 | if ($this->container->has(ResponseInterface::class)) { |
|
117 | 1 | $this->baseResponse = $this->container->get(ResponseInterface::class); |
|
118 | } |
||
119 | 2 | } |
|
120 | |||
121 | /** |
||
122 | * Call before suite |
||
123 | * |
||
124 | * @param array $settings |
||
125 | */ |
||
126 | 4 | public function _beforeSuite($settings = []) |
|
127 | { |
||
128 | 4 | parent::_beforeSuite($settings); |
|
129 | |||
130 | 4 | if ($this->usesOutputBuffer()) { |
|
131 | 2 | $this->startOutputBuffering(); |
|
132 | } |
||
133 | 3 | } |
|
134 | |||
135 | /** |
||
136 | * Call after suite |
||
137 | */ |
||
138 | 3 | public function _afterSuite() |
|
139 | { |
||
140 | 3 | if ($this->usesOutputBuffer()) { |
|
141 | 1 | $this->stopOutputBuffering(); |
|
142 | } |
||
143 | |||
144 | 3 | parent::_afterSuite(); |
|
145 | 3 | } |
|
146 | |||
147 | /** |
||
148 | * Before each test |
||
149 | * |
||
150 | * @param TestInterface $test |
||
151 | */ |
||
152 | 4 | public function _before(TestInterface $test) |
|
153 | { |
||
154 | 4 | $this->client = new Connector(); |
|
155 | |||
156 | 4 | $this->client->setRouter($this->container->get(RouterInterface::class)); |
|
157 | |||
158 | 4 | if (isset($this->baseRequest)) { |
|
159 | 2 | $this->client->setBaseRequest($this->baseRequest); |
|
160 | } |
||
161 | |||
162 | 4 | if (isset($this->baseResponse)) { |
|
163 | 2 | $this->client->setBaseResponse($this->baseResponse); |
|
164 | } |
||
165 | |||
166 | 4 | parent::_before($test); |
|
167 | 4 | } |
|
168 | |||
169 | /** |
||
170 | * After each test |
||
171 | * |
||
172 | * @param TestInterface $test |
||
173 | */ |
||
174 | 3 | public function _after(TestInterface $test) |
|
195 | |||
196 | /** |
||
197 | * Called when test fails |
||
198 | * |
||
199 | * @param TestInterface $test |
||
200 | * @param mixed $fail |
||
201 | */ |
||
202 | 1 | public function _failed(TestInterface $test, $fail) |
|
203 | { |
||
204 | 1 | if ($this->container->has(ErrorHandlerInterface::class)) { |
|
215 | |||
216 | |||
217 | /** |
||
218 | * Wrapper around `ob_start()` |
||
219 | * @codeCoverageIgnore |
||
220 | */ |
||
221 | protected function obStart() |
||
225 | |||
226 | /** |
||
227 | * Wrapper around `ob_get_level()` |
||
228 | * @codeCoverageIgnore |
||
229 | * |
||
230 | * @return int |
||
231 | */ |
||
232 | protected function obGetLevel() |
||
236 | |||
237 | /** |
||
238 | * Wrapper around `ob_clean()` |
||
239 | * @codeCoverageIgnore |
||
240 | */ |
||
241 | protected function obClean() |
||
245 | |||
246 | /** |
||
247 | * Wrapper around `session_status()` |
||
248 | * @codeCoverageIgnore |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | protected function sessionStatus() |
||
256 | |||
257 | /** |
||
258 | * Wrapper around `session_abort()` |
||
259 | * @codeCoverageIgnore |
||
260 | */ |
||
261 | protected function sessionAbort() |
||
265 | } |
||
266 |