Passed
Push — master ( c5e3ea...d7e52f )
by Mariano
02:32
created

Phiremock::getBinPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

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 6
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of phiremock-codeception-extension.
4
 *
5
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Codeception\Extension;
20
21
use Codeception\Configuration as Config;
22
use Codeception\Extension as CodeceptionExtension;
23
24
/**
25
 * Codeception Extension for Phiremock.
26
 */
27
class Phiremock extends CodeceptionExtension
28
{
29
    /**
30
     * @var array
31
     */
32
    public static $events = [
33
        'suite.before' => 'startProcess',
34
        'suite.after'  => 'stopProcess',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    protected $config = [
41
        'listen'     => '0.0.0.0:8086',
42
        'debug'      => false,
43
        'startDelay' => 0,
44
    ];
45
46
    /**
47
     * @var PhiremockProcess
48
     */
49
    private $process;
50
51
    /**
52
     * Class constructor.
53
     *
54
     * @param array            $config
55
     * @param array            $options
56
     * @param PhireMockProcess $process optional PhiremockProcess object
57
     */
58
    public function __construct(
59
        array $config,
60
        array $options,
61
        PhiremockProcess $process = null
62
    ) {
63
        $this->config['bin_path'] = Config::projectDir() . '../vendor/bin/phiremock';
64
        $this->config['logs_path'] = Config::logDir();
65
        $this->config['expectations_path'] = null;
66
67
        parent::__construct($config, $options);
68
69
        $this->initProcess($process);
70
    }
71
72
    public function startProcess()
73
    {
74
        list($ip, $port) = explode(':', $this->config['listen']);
75
76
        $this->process->start(
77
            $ip,
78
            $port,
79
            $this->getBinPath($this->config['bin_path']),
80
            realpath($this->config['logs_path']),
81
            $this->config['debug'],
82
            $this->config['expectations_path'] ? realpath($this->config['expectations_path']) : null
83
        );
84
        $this->executeDelay();
85
    }
86
87
    public function stopProcess()
88
    {
89
        $this->process->stop();
90
    }
91
92
    private function executeDelay()
93
    {
94
        if ($this->config['startDelay']) {
95
            sleep($this->config['startDelay']);
96
        }
97
    }
98
99
    /**
100
     * @param PhiremockProcess|null $process
101
     */
102
    private function initProcess($process)
103
    {
104
        $this->process = null === $process ? new PhiremockProcess() : $process;
105
    }
106
107
    private function getBinPath($path)
108
    {
109
        $currentDirectory = getcwd();
110
        chdir(Config::projectDir());
111
        $binPath = realpath($path);
112
        chdir($currentDirectory);
113
114
        return $binPath;
115
    }
116
}
117