1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mcustiel\Phiremock\Codeception\Util; |
4
|
|
|
|
5
|
|
|
use Codeception\Exception\ConfigurationException; |
6
|
|
|
use Mcustiel\Phiremock\Client\Connection\Host; |
7
|
|
|
use Mcustiel\Phiremock\Client\Connection\Port; |
8
|
|
|
use Mcustiel\Phiremock\Client\Factory; |
9
|
|
|
|
10
|
|
|
class Config |
11
|
|
|
{ |
12
|
|
|
public const EXPECTATIONS_PATH_CONFIG = 'expectations_path'; |
13
|
|
|
|
14
|
|
|
public const DEFAULT_CONFIG = [ |
15
|
|
|
'host' => 'localhost', |
16
|
|
|
'port' => 8086, |
17
|
|
|
'reset_before_each_test' => false, |
18
|
|
|
'client_factory' => 'default', |
19
|
|
|
self::EXPECTATIONS_PATH_CONFIG => null, |
20
|
|
|
'extra_connections' => [], |
21
|
|
|
'secure' => false, |
22
|
|
|
]; |
23
|
|
|
private const EXPECTATIONS_PATH = 'phiremock-expectations'; |
24
|
|
|
|
25
|
|
|
/** @var Host */ |
26
|
|
|
private $host; |
27
|
|
|
/** @var Port */ |
28
|
|
|
private $port; |
29
|
|
|
/** @var bool */ |
30
|
|
|
private $resetBeforeEachTest; |
31
|
|
|
/** @var FactoryClass */ |
32
|
|
|
private $clientFactory; |
33
|
|
|
/** @var DirectoryPath */ |
34
|
|
|
private $expectationsPath; |
35
|
|
|
/** @var array */ |
36
|
|
|
private $extraConnections; |
37
|
|
|
/** @var bool */ |
38
|
|
|
private $secure; |
39
|
|
|
/** @var callable */ |
40
|
|
|
private $output; |
41
|
|
|
|
42
|
|
|
/** @throws ConfigurationException */ |
43
|
|
|
public function __construct(array $config, callable $output) |
44
|
|
|
{ |
45
|
|
|
$this->output = $output; |
46
|
|
|
$this->setResetBeforeEachTest($config); |
47
|
|
|
$this->expectationsPath = $this->getExpectationsPathConfiguration($config); |
48
|
|
|
$this->host = new Host($config['host']); |
49
|
|
|
$this->port = new Port($config['port']); |
50
|
|
|
$this->clientFactory = $this->getFactoryClass($config); |
51
|
|
|
$this->initExtraConnections($config); |
52
|
|
|
$this->secure = $config['secure']; |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @return Config[] */ |
57
|
|
|
public function getExtraConnectionsConfigs(): array |
58
|
|
|
{ |
59
|
|
|
return $this->extraConnections; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getHost(): Host |
63
|
|
|
{ |
64
|
|
|
return $this->host; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPort(): Port |
68
|
|
|
{ |
69
|
|
|
return $this->port; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function isResetBeforeEachTest(): bool |
73
|
|
|
{ |
74
|
|
|
return $this->resetBeforeEachTest; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getClientFactory(): Factory |
78
|
|
|
{ |
79
|
|
|
return $this->clientFactory->getInstance(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getExpectationsPath(): string |
83
|
|
|
{ |
84
|
|
|
return $this->expectationsPath->asString(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isSecure(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->secure; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function asArray(): array |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'host' => $this->host->asString(), |
96
|
|
|
'port' => $this->port->asInt(), |
97
|
|
|
'reset_before_each_test' => $this->resetBeforeEachTest, |
98
|
|
|
'client_factory' => $this->clientFactory->asString(), |
99
|
|
|
self::EXPECTATIONS_PATH_CONFIG => $this->expectationsPath->asString(), |
100
|
|
|
'extra_connections' => [], |
101
|
|
|
'secure' => $this->secure, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @throws ConfigurationException */ |
106
|
|
|
private function getFactoryClass(array $config): FactoryClass |
107
|
|
|
{ |
108
|
|
|
if (!isset($config['client_factory'])) { |
109
|
|
|
return new FactoryClass('default'); |
110
|
|
|
} |
111
|
|
|
return new FactoryClass($config['client_factory']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @throws ConfigurationException */ |
115
|
|
|
private function getExpectationsPathConfiguration(array $config): DirectoryPath |
116
|
|
|
{ |
117
|
|
|
if (isset($config['expectationsPath'])) { |
118
|
|
|
call_user_func( |
119
|
|
|
$this->output, |
120
|
|
|
'Phiremock/DEPRECATION: expectationsPath option is deprecated and will be removed. Please use expectations_path.' |
121
|
|
|
); |
122
|
|
|
$config[self::EXPECTATIONS_PATH_CONFIG] = $config['expectationsPath']; |
123
|
|
|
} |
124
|
|
|
$configuredPath = $config[self::EXPECTATIONS_PATH_CONFIG] ?? null; |
125
|
|
|
if (empty($configuredPath)) { |
126
|
|
|
$defaultPath = codecept_data_dir(self::EXPECTATIONS_PATH); |
127
|
|
|
return DirectoryPath::createAndGetInstance($defaultPath); |
128
|
|
|
} |
129
|
|
|
return DirectoryPath::createAbsoluteOrRelativeToCodeceptionDir($configuredPath); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function setResetBeforeEachTest($config) |
133
|
|
|
{ |
134
|
|
|
if (isset($config['resetBeforeEachTest'])) { |
135
|
|
|
call_user_func( |
136
|
|
|
$this->output, |
137
|
|
|
'Phiremock/DEPRECATION: resetBeforeEachTest option is deprecated and will be removed. Please use reset_before_each_test.' |
138
|
|
|
); |
139
|
|
|
$config['reset_before_each_test'] = $config['resetBeforeEachTest']; |
140
|
|
|
} |
141
|
|
|
$this->resetBeforeEachTest = $config['reset_before_each_test']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** @throws ConfigurationException */ |
145
|
|
|
private function initExtraConnections(array $config): void |
146
|
|
|
{ |
147
|
|
|
$this->extraConnections = []; |
148
|
|
|
if (isset($config['extra_connections'])) { |
149
|
|
|
foreach ($config['extra_connections'] as $connectionName => $extraConnection) { |
150
|
|
|
$connectionConfig = $extraConnection + self::DEFAULT_CONFIG; |
151
|
|
|
unset($connectionConfig['extra_connections']); |
152
|
|
|
$this->extraConnections[$connectionName] = new Config($connectionConfig, $this->output); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|