Completed
Push — master ( 5becf3...326e32 )
by Arnold
09:07
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 2
        }
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
     * Initialize the module
106
     */
107
    public function init()
108 3
    {
109
        $this->container = $this->initContainer();
110 3
111
        if ($this->container->has(ServerRequestInterface::class)) {
112 2
            $this->baseRequest = $this->container->get(ServerRequestInterface::class);
113 1
        }
114 1
        
115
        if ($this->container->has(ResponseInterface::class)) {
116 2
            $this->baseResponse = $this->container->get(ResponseInterface::class);
117 1
        }
118 1
    }
119 2
    
120
    /**
121
     * Call before suite
122
     * 
123
     * @param array $settings
124
     */
125
    public function _beforeSuite($settings = [])
126 4
    {
127
        parent::_beforeSuite($settings);
128 4
        
129
        if ($this->usesOutputBuffer()) {
130 4
            $this->startOutputBuffering();
131 2
        }
132 1
    }
133 3
    
134
    /**
135
     * Call after suite
136
     */
137
    public function _afterSuite()
138 3
    {
139
        if ($this->usesOutputBuffer()) {
140 3
            $this->stopOutputBuffering();
141 1
        }
142 1
        
143
        parent::_afterSuite();
144 3
    }
145 3
    
146
    /**
147
     * Before each test
148
     * 
149
     * @param TestInterface $test
150
     */
151
    public function _before(TestInterface $test)
152 4
    {
153
        $this->init();
154 4
155
        $this->client = new Connector();
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 2
        }
161
        
162 4
        if (isset($this->baseResponse)) {
163 2
            $this->client->setBaseResponse($this->baseResponse);
164 2
        }
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->sessionDestroy();
178 1
        }
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 1
            }
186
            
187 1
            if (isset($this->baseResponse)) {
188 1
                $this->baseResponse = $this->client->getBaseResponse();
189 1
            }
190 1
        }
191
192
        parent::_after($test);
193 3
    }
194 3
    
195
    /**
196
     * Called when test fails
197
     * 
198
     * @param TestInterface $test
199
     * @param mixed         $fail
200
     */
201
    public function _failed(TestInterface $test, $fail)
202 1
    {
203
        if ($this->container->has(ErrorHandlerInterface::class)) {
204 1
            $error = $this->container->get(ErrorHandlerInterface::class)->getError();
205 1
            
206
            if ($error) {
207 1
                $this->debug((string)$error);
208 1
            }
209 1
        }
210 1
211
        
212
        parent::_failed($test, $fail);
213 1
    }
214 1
    
215
    
216
    /**
217
     * Wrapper around `ob_start()`
218
     * @codeCoverageIgnore
219
     */
220
    protected function obStart()
221
    {
222
        ob_start();
223
    }
224
    
225
    /**
226
     * Wrapper around `ob_get_level()`
227
     * @codeCoverageIgnore
228
     * 
229
     * @return int
230
     */
231
    protected function obGetLevel()
232
    {
233
        return ob_get_level();
234
    }
235
    
236
    /**
237
     * Wrapper around `ob_clean()`
238
     * @codeCoverageIgnore
239
     */
240
    protected function obClean()
241
    {
242
        ob_clean();
243
    }
244
    
245
    /**
246
     * Wrapper around `session_status()`
247
     * @codeCoverageIgnore
248
     * 
249
     * @return int
250
     */
251
    protected function sessionStatus()
252
    {
253
        return session_status();
254
    }
255
    
256
    /**
257
     * Wrapper around `session_unset()` and `session_destroy()`
258
     * @codeCoverageIgnore
259
     */
260
    protected function sessionDestroy()
261
    {
262
        session_unset();
263
        session_destroy();
264
    }
265
}
266