Completed
Push — master ( 4daf57...c8d98f )
by Arnold
9s
created

Module::sessionAbort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Jasny\Codeception;
4
5
use Jasny\RouterInterface;
6
use Jasny\Codeception\Connector;
7
use Jasny\ErrorHandlerInterface;
8
use Codeception\Configuration;
9
use Codeception\Lib\Framework;
10
use Codeception\TestInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Interop\Container\ContainerInterface;
14
15
/**
16
 * Module for running functional tests using Jasny MVC
17
 */
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)
175
    {
176 3
        if ($this->sessionStatus() === PHP_SESSION_ACTIVE) {
177 1
            $this->sessionAbort();
178
        }
179
180 3
        if (isset($this->client) && $this->client instanceof Connector) {
181 1
            $this->client->reset();
182
            
183 1
            if (isset($this->baseRequest)) {
184 1
                $this->baseRequest = $this->client->getBaseRequest();
185
            }
186
            
187 1
            if (isset($this->baseResponse)) {
188 1
                $this->baseResponse = $this->client->getBaseResponse();
189
            }
190
        }
191
192
193 3
        parent::_after($test);
194 3
    }
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)) {
205 1
            $error = $this->container->get(ErrorHandlerInterface::class)->getError();
206
            
207 1
            if ($error) {
208 1
                $this->debug((string)$error);
209
            }
210
        }
211
212
        
213 1
        parent::_failed($test, $fail);
214 1
    }
215
    
216
    
217
    /**
218
     * Wrapper around `ob_start()`
219
     * @codeCoverageIgnore
220
     */
221
    protected function obStart()
222
    {
223
        ob_start();
224
    }
225
    
226
    /**
227
     * Wrapper around `ob_get_level()`
228
     * @codeCoverageIgnore
229
     * 
230
     * @return int
231
     */
232
    protected function obGetLevel()
233
    {
234
        return ob_get_level();
235
    }
236
    
237
    /**
238
     * Wrapper around `ob_clean()`
239
     * @codeCoverageIgnore
240
     */
241
    protected function obClean()
242
    {
243
        ob_clean();
244
    }
245
    
246
    /**
247
     * Wrapper around `session_status()`
248
     * @codeCoverageIgnore
249
     * 
250
     * @return int
251
     */
252
    protected function sessionStatus()
253
    {
254
        return session_status();
255
    }
256
    
257
    /**
258
     * Wrapper around `session_abort()`
259
     * @codeCoverageIgnore
260
     */
261
    protected function sessionAbort()
262
    {
263
        session_abort();
264
    }
265
}
266