Passed
Push — develop ( 745cf8...734f70 )
by ANTHONIUS
05:36 queued 03:34
created

RemoteSession::doStartSession()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 3
nop 0
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 6
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Behat\Coverage\Bridge\CodeCoverage\Session;
15
16
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Driver\Dummy;
17
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Processor;
18
use Doyo\Behat\Coverage\Bridge\CodeCoverage\TestCase;
19
use SebastianBergmann\CodeCoverage\Filter;
20
21
class RemoteSession extends Session
22
{
23
    const HEADER_SESSION_KEY   = 'HTTP_DOYO_COVERAGE_SESSION';
24
    const HEADER_TEST_CASE_KEY = 'HTTP_DOYO_COVERAGE_TESTCASE';
25
26
    public static function startSession()
27
    {
28
        if (!isset($_SERVER[static::HEADER_SESSION_KEY])) {
29
            return false;
30
        }
31
32
        $name    = $_SERVER[static::HEADER_SESSION_KEY];
33
        $session = new static($name);
34
        if (isset($_SERVER[static::HEADER_TEST_CASE_KEY])) {
35
            $session->doStartSession();
36
        } else {
37
            return false;
38
        }
39
        $session->save();
40
41
        return true;
42
    }
43
44
    public function init(array $config)
45
    {
46
        $filter = new Filter();
47
        if (isset($config['filterOptions'])) {
48
            $filter->setWhitelistedFiles($config['filterOptions']['whitelistedFiles']);
49
        }
50
51
        $processor    = new Processor(new Dummy(), $filter);
52
        $codeCoverage = $processor->getCodeCoverage();
53
        if (isset($config['codeCoverageOptions'])) {
54
            foreach ($config['codeCoverageOptions'] as $method => $option) {
55
                $method = 'set'.ucfirst($method);
56
                \call_user_func_array([$codeCoverage, $method], [$option]);
57
            }
58
            $processor->setCodeCoverageOptions($config['codeCoverageOptions']);
59
        }
60
        $this->setProcessor($processor);
61
        $this->reset();
62
    }
63
64
    public function doStartSession()
65
    {
66
        $name     = $_SERVER[static::HEADER_TEST_CASE_KEY];
67
        $testCase = new TestCase($name);
68
        $this->setTestCase($testCase);
69
70
        try {
71
            $this->start();
72
            register_shutdown_function([$this, 'shutdown']);
73
        } catch (\Exception $e) {
74
            $this->reset();
75
            $this->exceptions[] = $e;
76
        }
77
    }
78
}
79