1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phiremock-codeception-extension. |
5
|
|
|
* |
6
|
|
|
* phiremock-codeception-extension is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* phiremock-codeception-extension is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Mcustiel\Phiremock\Codeception\Util; |
21
|
|
|
|
22
|
|
|
use Codeception\Exception\ConfigurationException; |
23
|
|
|
use Mcustiel\Phiremock\Client\Connection\Host; |
24
|
|
|
use Mcustiel\Phiremock\Client\Connection\Port; |
25
|
|
|
use Mcustiel\Phiremock\Client\Factory; |
26
|
|
|
|
27
|
|
|
class Config |
28
|
|
|
{ |
29
|
|
|
public const EXPECTATIONS_PATH_CONFIG = 'expectations_path'; |
30
|
|
|
|
31
|
|
|
public const DEFAULT_CONFIG = [ |
32
|
|
|
'host' => 'localhost', |
33
|
|
|
'port' => 8086, |
34
|
|
|
'reset_before_each_test' => false, |
35
|
|
|
'client_factory' => 'default', |
36
|
|
|
self::EXPECTATIONS_PATH_CONFIG => null, |
37
|
|
|
'extra_connections' => [], |
38
|
|
|
'secure' => false, |
39
|
|
|
]; |
40
|
|
|
private const EXPECTATIONS_PATH = 'phiremock-expectations'; |
41
|
|
|
|
42
|
|
|
/** @var Host */ |
43
|
|
|
private $host; |
44
|
|
|
/** @var Port */ |
45
|
|
|
private $port; |
46
|
|
|
/** @var bool */ |
47
|
|
|
private $resetBeforeEachTest; |
48
|
|
|
/** @var FactoryClass */ |
49
|
|
|
private $clientFactory; |
50
|
|
|
/** @var DirectoryPath */ |
51
|
|
|
private $expectationsPath; |
52
|
|
|
/** @var array */ |
53
|
|
|
private $extraConnections; |
54
|
|
|
/** @var bool */ |
55
|
|
|
private $secure; |
56
|
|
|
/** @var callable */ |
57
|
|
|
private $output; |
58
|
|
|
|
59
|
|
|
/** @throws ConfigurationException */ |
60
|
|
|
public function __construct(array $config, callable $output) |
61
|
|
|
{ |
62
|
|
|
$this->output = $output; |
63
|
|
|
$this->setResetBeforeEachTest($config); |
64
|
|
|
$this->expectationsPath = $this->getExpectationsPathConfiguration($config); |
65
|
|
|
$this->host = new Host($config['host']); |
66
|
|
|
$this->port = new Port($config['port']); |
67
|
|
|
$this->clientFactory = $this->getFactoryClass($config); |
68
|
|
|
$this->initExtraConnections($config); |
69
|
|
|
$this->secure = $config['secure']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @return Config[] */ |
73
|
|
|
public function getExtraConnectionsConfigs(): array |
74
|
|
|
{ |
75
|
|
|
return $this->extraConnections; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getHost(): Host |
79
|
|
|
{ |
80
|
|
|
return $this->host; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPort(): Port |
84
|
|
|
{ |
85
|
|
|
return $this->port; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function isResetBeforeEachTest(): bool |
89
|
|
|
{ |
90
|
|
|
return $this->resetBeforeEachTest; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getClientFactory(): Factory |
94
|
|
|
{ |
95
|
|
|
return $this->clientFactory->getInstance(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getExpectationsPath(): string |
99
|
|
|
{ |
100
|
|
|
return $this->expectationsPath->asString(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function isSecure(): bool |
104
|
|
|
{ |
105
|
|
|
return $this->secure; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function asArray(): array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
'host' => $this->host->asString(), |
112
|
|
|
'port' => $this->port->asInt(), |
113
|
|
|
'reset_before_each_test' => $this->resetBeforeEachTest, |
114
|
|
|
'client_factory' => $this->clientFactory->asString(), |
115
|
|
|
self::EXPECTATIONS_PATH_CONFIG => $this->expectationsPath->asString(), |
116
|
|
|
'extra_connections' => [], |
117
|
|
|
'secure' => $this->secure, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @throws ConfigurationException */ |
122
|
|
|
private function getFactoryClass(array $config): FactoryClass |
123
|
|
|
{ |
124
|
|
|
if (!isset($config['client_factory'])) { |
125
|
|
|
return new FactoryClass('default'); |
126
|
|
|
} |
127
|
|
|
return new FactoryClass($config['client_factory']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @throws ConfigurationException */ |
131
|
|
|
private function getExpectationsPathConfiguration(array $config): DirectoryPath |
132
|
|
|
{ |
133
|
|
|
if (isset($config['expectationsPath'])) { |
134
|
|
|
call_user_func( |
135
|
|
|
$this->output, |
136
|
|
|
'Phiremock/DEPRECATION: expectationsPath option is deprecated and will be removed. Please use expectations_path.' |
137
|
|
|
); |
138
|
|
|
$config[self::EXPECTATIONS_PATH_CONFIG] = $config['expectationsPath']; |
139
|
|
|
} |
140
|
|
|
$configuredPath = $config[self::EXPECTATIONS_PATH_CONFIG] ?? null; |
141
|
|
|
if (empty($configuredPath)) { |
142
|
|
|
$defaultPath = codecept_data_dir(self::EXPECTATIONS_PATH); |
143
|
|
|
return DirectoryPath::createAndGetInstance($defaultPath); |
144
|
|
|
} |
145
|
|
|
return DirectoryPath::createAbsoluteOrRelativeToCodeceptionDir($configuredPath); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function setResetBeforeEachTest($config) |
149
|
|
|
{ |
150
|
|
|
if (isset($config['resetBeforeEachTest'])) { |
151
|
|
|
call_user_func( |
152
|
|
|
$this->output, |
153
|
|
|
'Phiremock/DEPRECATION: resetBeforeEachTest option is deprecated and will be removed. Please use reset_before_each_test.' |
154
|
|
|
); |
155
|
|
|
$config['reset_before_each_test'] = $config['resetBeforeEachTest']; |
156
|
|
|
} |
157
|
|
|
$this->resetBeforeEachTest = $config['reset_before_each_test']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** @throws ConfigurationException */ |
161
|
|
|
private function initExtraConnections(array $config): void |
162
|
|
|
{ |
163
|
|
|
$this->extraConnections = []; |
164
|
|
|
if (isset($config['extra_connections'])) { |
165
|
|
|
foreach ($config['extra_connections'] as $connectionName => $extraConnection) { |
166
|
|
|
$connectionConfig = $extraConnection + self::DEFAULT_CONFIG; |
167
|
|
|
unset($connectionConfig['extra_connections']); |
168
|
|
|
$this->extraConnections[$connectionName] = new Config($connectionConfig, $this->output); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|