1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mcustiel\Phiremock\Codeception\Extension; |
4
|
|
|
|
5
|
|
|
use Codeception\Configuration; |
6
|
|
|
|
7
|
|
|
class Config |
8
|
|
|
{ |
9
|
|
|
public const DEFAULT_INTERFACE = '0.0.0.0'; |
10
|
|
|
public const DEFAULT_PORT = 8086; |
11
|
|
|
public const DEFAULT_PHIREMOCK_PATH = 'vendor/bin/phiremock'; |
12
|
|
|
public const DEFAULT_DELAY = 0; |
13
|
|
|
public const DEFAULT_DEBUG_MODE = false; |
14
|
|
|
public const DEFAULT_EXPECTATIONS_PATH = null; |
15
|
|
|
public const DEFAULT_CERTIFICATE = null; |
16
|
|
|
public const DEFAULT_CERTIFICATE_KEY = null; |
17
|
|
|
public const DEFAULT_CERTIFICATE_PASSPHRASE = null; |
18
|
|
|
public const DEFAULT_SERVER_FACTORY = 'default'; |
19
|
|
|
public const DEFAULT_EXTRA_INSTANCES = []; |
20
|
|
|
|
21
|
|
|
public const DEFAULT_CONFIG = [ |
22
|
|
|
'listen' => self::DEFAULT_INTERFACE . ':' . self::DEFAULT_PORT, |
23
|
|
|
'debug' => self::DEFAULT_DEBUG_MODE, |
24
|
|
|
'start_delay' => self::DEFAULT_DELAY, |
25
|
|
|
'bin_path' => self::DEFAULT_PHIREMOCK_PATH, |
26
|
|
|
'expectations_path' => self::DEFAULT_EXPECTATIONS_PATH, |
27
|
|
|
'server_factory' => self::DEFAULT_SERVER_FACTORY, |
28
|
|
|
'certificate' => self::DEFAULT_CERTIFICATE, |
29
|
|
|
'certificate_key' => self::DEFAULT_CERTIFICATE_KEY, |
30
|
|
|
'cert_passphrase' => self::DEFAULT_CERTIFICATE_PASSPHRASE, |
31
|
|
|
'extra_instances' => self::DEFAULT_EXTRA_INSTANCES, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $interface; |
36
|
|
|
/** @var int */ |
37
|
|
|
private $port; |
38
|
|
|
/** @var int */ |
39
|
|
|
private $delay; |
40
|
|
|
/** @var bool */ |
41
|
|
|
private $debug; |
42
|
|
|
/** @var Path */ |
43
|
|
|
private $phiremockPath; |
44
|
|
|
/** @var Path */ |
45
|
|
|
private $expectationsPath; |
46
|
|
|
/** @var Path */ |
47
|
|
|
private $logsPath; |
48
|
|
|
/** @var string */ |
49
|
|
|
private $serverFactory; |
50
|
|
|
/** @var Path */ |
51
|
|
|
private $certificate; |
52
|
|
|
/** @var Path */ |
53
|
|
|
private $certificateKey; |
54
|
|
|
/** @var string */ |
55
|
|
|
private $certificatePassphrase; |
56
|
|
|
/** @var array */ |
57
|
|
|
private $extraInstances; |
58
|
|
|
|
59
|
|
|
public function __construct(array $config) |
60
|
|
|
{ |
61
|
|
|
$this->initInterfaceAndPort($config); |
62
|
|
|
$this->initExpectationsPath($config); |
63
|
|
|
$this->initServerFactory($config); |
64
|
|
|
$this->delay = (int) $config['start_delay']; |
65
|
|
|
$this->phiremockPath = new Path($config['bin_path']); |
66
|
|
|
$this->logsPath = new Path($config['logs_path']); |
67
|
|
|
$this->debug = (bool) $config['debug']; |
68
|
|
|
$this->initCertificatePath($config); |
69
|
|
|
$this->initCertificateKeyPath($config); |
70
|
|
|
$this->certificatePassphrase = $config['cert_passphrase']; |
71
|
|
|
$this->initExtraInstances($config); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getInterface(): string |
75
|
|
|
{ |
76
|
|
|
return $this->interface; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getPort(): string |
80
|
|
|
{ |
81
|
|
|
return $this->port; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isDebugMode(): bool |
85
|
|
|
{ |
86
|
|
|
return $this->debug; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getStartDelay(): string |
90
|
|
|
{ |
91
|
|
|
return $this->delay; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getPhiremockPath(): string |
95
|
|
|
{ |
96
|
|
|
return $this->phiremockPath->absoluteOrRelativeToCodeceptionDir(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getExpectationsPath(): ?string |
100
|
|
|
{ |
101
|
|
|
return $this->expectationsPath ? $this->expectationsPath->absoluteOrRelativeToCodeceptionDir() : null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getCertificatePath(): ?string |
105
|
|
|
{ |
106
|
|
|
return $this->certificate ? $this->certificate->absoluteOrRelativeToCodeceptionDir() : null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getCertificateKeyPath(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->certificateKey ? $this->certificateKey->absoluteOrRelativeToCodeceptionDir() : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getCertificatePassphrase(): ?string |
115
|
|
|
{ |
116
|
|
|
return $this->certificatePassphrase; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getLogsPath(): string |
120
|
|
|
{ |
121
|
|
|
return $this->logsPath->absoluteOrRelativeToCodeceptionDir(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getServerFactory(): ?string |
125
|
|
|
{ |
126
|
|
|
return $this->serverFactory; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getDelay(): int |
130
|
|
|
{ |
131
|
|
|
return $this->delay; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getExtraInstances(): array |
135
|
|
|
{ |
136
|
|
|
return $this->extraInstances; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function getDefaultLogsPath(): string |
140
|
|
|
{ |
141
|
|
|
return Configuration::logDir(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function initInterfaceAndPort(array $config): void |
145
|
|
|
{ |
146
|
|
|
if (isset($config['listen'])) { |
147
|
|
|
$parts = explode(':', $config['listen']); |
148
|
|
|
$this->interface = $parts[0]; |
149
|
|
|
$this->port = (int) isset($parts[1]) ? $parts[1] : self::DEFAULT_PORT; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function initExpectationsPath(array $config): void |
154
|
|
|
{ |
155
|
|
|
$this->expectationsPath = isset($config['expectations_path']) ? new Path($config['expectations_path']) : null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function initServerFactory(array $config): void |
159
|
|
|
{ |
160
|
|
|
$factory = null; |
161
|
|
|
if (isset($config['server_factory'])) { |
162
|
|
|
$factoryClassConfig = $config['server_factory']; |
163
|
|
|
if ($factoryClassConfig !== 'default') { |
164
|
|
|
$factory = $config['server_factory']; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
$this->serverFactory = $factory; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function initDelay(array $config): void |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
if (isset($config['startDelay'])) { |
173
|
|
|
$this->writeln('PHIREMOCK/DEPRECATION: startDelay option is deprecated and will be removed. Please use start_delay'); |
|
|
|
|
174
|
|
|
$this->delay = $config['startDelay']; |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($config['start_delay']) { |
179
|
|
|
$this->delay = $config['start_delay']; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function initExtraInstances(array $config): void |
184
|
|
|
{ |
185
|
|
|
if (isset($config['extra_instances']) && is_array($config['extra_instances'])) { |
186
|
|
|
foreach ($config['extra_instances'] as $extraInstance) { |
187
|
|
|
$instanceConfig = $extraInstance + self::DEFAULT_CONFIG + ['logs_path' => Config::getDefaultLogsPath()]; |
188
|
|
|
unset($instanceConfig['extra_instances']); |
189
|
|
|
$this->extraInstances[] = new self($instanceConfig); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
private function initCertificateKeyPath($config) |
195
|
|
|
{ |
196
|
|
|
$this->certificateKey = $config['certificate_key'] ? new Path($config['certificate_key']) : null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
private function initCertificatePath($config) |
200
|
|
|
{ |
201
|
|
|
$this->certificate = $config['certificate'] ? new Path($config['certificate']) : null; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.