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
|
|
|
private const DEFAULT_PATH = 'vendor/bin/phiremock'; |
30
|
|
|
private const DEFAULT_PORT = 8086; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
public static $events = [ |
34
|
|
|
'suite.before' => 'startProcess', |
35
|
|
|
'suite.after' => 'stopProcess', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected $config = [ |
40
|
|
|
'listen' => '0.0.0.0:' . self::DEFAULT_PORT, |
41
|
|
|
'debug' => false, |
42
|
|
|
'start_delay' => 0, |
43
|
|
|
'bin_path' => self::DEFAULT_PATH, |
44
|
|
|
'expectations_path' => null, |
45
|
|
|
'server_factory' => 'default' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** @var PhiremockProcess */ |
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->setDefaultLogsPath(); |
64
|
|
|
parent::__construct($config, $options); |
65
|
|
|
|
66
|
|
|
$this->initProcess($process); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function startProcess(): void |
70
|
|
|
{ |
71
|
|
|
list($ip, $port) = explode(':', $this->config['listen']); |
72
|
|
|
|
73
|
|
|
$this->writeln('Starting phiremock...'); |
74
|
|
|
$this->process->start( |
75
|
|
|
$ip, |
76
|
|
|
empty($port) ? self::DEFAULT_PORT: (int) $port, |
77
|
|
|
$this->getPathFromCodeceptionDir($this->config['bin_path']), |
78
|
|
|
$this->getPathFromCodeceptionDir($this->config['logs_path']), |
79
|
|
|
$this->config['debug'], |
80
|
|
|
$this->config['expectations_path'] ? $this->getPathFromCodeceptionDir($this->config['expectations_path']) : null, |
81
|
|
|
$this->getFactoryClass() |
82
|
|
|
); |
83
|
|
|
$this->executeDelay(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function stopProcess(): void |
87
|
|
|
{ |
88
|
|
|
$this->writeln('Stopping phiremock...'); |
89
|
|
|
$this->process->stop(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getFactoryClass(): ?string |
93
|
|
|
{ |
94
|
|
|
if (isset($this->config['server_factory'])) { |
95
|
|
|
$factoryClassConfig = $this->config['server_factory']; |
96
|
|
|
if ($factoryClassConfig !== 'default') { |
97
|
|
|
return $this->config['server_factory']; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function executeDelay(): void |
104
|
|
|
{ |
105
|
|
|
if (isset($this->config['startDelay'])) { |
106
|
|
|
$this->writeln('PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay'); |
107
|
|
|
$this->config['start_delay'] = $this->config['startDelay']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->config['start_delay']) { |
111
|
|
|
sleep($this->config['start_delay']); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function initProcess(?PhiremockProcess $process): void |
116
|
|
|
{ |
117
|
|
|
$this->process = $process ?? new PhiremockProcess(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function setDefaultLogsPath(): void |
121
|
|
|
{ |
122
|
|
|
$this->config['logs_path'] = Config::logDir(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getPathFromCodeceptionDir($path): string |
126
|
|
|
{ |
127
|
|
|
if (substr($path, 0, 1) === '/') { |
128
|
|
|
return $path; |
129
|
|
|
} |
130
|
|
|
return realpath(Config::projectDir() . $path); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|