Failed Conditions
Push — master ( 508499...365050 )
by Arnold
07:49
created

Module::_before()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Jasny\Codeception;
4
5
use Jasny\Router;
6
use Codeception\Configuration;
7
use Codeception\Lib\Framework;
8
use Codeception\TestInterface;
9
use Jasny\Codeception\Connector;
10
11
/**
12
 * Module for running functional tests using Jasny MVC
13
 */
14
class Module extends Framework
15
{
16
    /**
17
     * Required configuration fields
18
     * @var array
19
     */
20
    protected $requiredFields = ['router'];
21
    
22
    /**
23
     * @var Router 
24
     */
25
    public $router;
26
    
27
    
28
    /**
29
     * Load the router by including the file.
30
     * @codeCoverageIgnore
31
     * 
32
     * @param string $file
33
     * @return Router
34
     */
35
    protected function loadRouter($file)
36
    {
37
        return include $file;
38
    }
39
    
40
    /**
41
     * Initialize the router
42
     */
43
    protected function initRouter()
44
    {
45
        $router = $this->loadRouter(Configuration::projectDir() . $this->config['router']);
46
        
47
        if (!$router instanceof Router) {
48
            throw new \UnexpectedValueException("Failed to get router from '{$this->config['router']}'");
49
        }
50
        
51
        $this->router = $router;
52
    }
53
    
54
    /**
55
     * Initialize the module
56
     */
57
    public function _initialize()
58
    {
59
        $this->initRouter();
60
        
61
        parent::_initialize();
62
    }
63
    
64
    
65
    /**
66
     * Before each test
67
     * 
68
     * @param TestInterface $test
69
     */
70
    public function _before(TestInterface $test)
71
    {
72
        $this->client = new Connector();
73
        
74
        $this->client->setRouter($this->router);
75
        $this->client->useGlobalEnvironment(!empty($this->config['global_environment']));
76
        
77
        parent::_before($test);
78
    }
79
    
80
    /**
81
     * After each test
82
     * 
83
     * @param TestInterface $test
84
     */
85
    public function _after(TestInterface $test)
86
    {
87
        if (session_status() === PHP_SESSION_ACTIVE) {
88
            session_write_close();
89
        }
90
        
91
        parent::_after($test);
92
    }
93
}
94