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
|
|
|
public function __construct(array $config, callable $output) |
43
|
|
|
{ |
44
|
|
|
$this->output = $output; |
45
|
|
|
$this->setResetBeforeEachTest($config); |
46
|
|
|
$this->expectationsPath = $this->getExpectationsPathConfiguration($config); |
47
|
|
|
$this->host = new Host($config['host']); |
48
|
|
|
$this->port = new Port($config['port']); |
49
|
|
|
$this->clientFactory = $this->getFactoryClass($config); |
50
|
|
|
$this->initExtraConnections($config); |
51
|
|
|
$this->secure = $config['secure']; |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @return Config[] */ |
56
|
|
|
public function getExtraConnectionsConfigs(): array |
57
|
|
|
{ |
58
|
|
|
return $this->extraConnections; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getHost(): Host |
62
|
|
|
{ |
63
|
|
|
return $this->host; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getPort(): Port |
67
|
|
|
{ |
68
|
|
|
return $this->port; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isResetBeforeEachTest(): bool |
72
|
|
|
{ |
73
|
|
|
return $this->resetBeforeEachTest; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getClientFactory(): Factory |
77
|
|
|
{ |
78
|
|
|
return $this->clientFactory->getInstance(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getExpectationsPath(): string |
82
|
|
|
{ |
83
|
|
|
return $this->expectationsPath->asString(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isSecure(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->secure; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function asArray(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'host' => $this->host->asString(), |
95
|
|
|
'port' => $this->port->asInt(), |
96
|
|
|
'reset_before_each_test' => $this->resetBeforeEachTest, |
97
|
|
|
'client_factory' => $this->clientFactory->asString(), |
98
|
|
|
self::EXPECTATIONS_PATH_CONFIG => $this->expectationsPath->asString(), |
99
|
|
|
'extra_connections' => [], |
100
|
|
|
'secure' => $this->secure, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @throws ConfigurationException */ |
105
|
|
|
private function getFactoryClass(array $config): FactoryClass |
106
|
|
|
{ |
107
|
|
|
if (!isset($config['client_factory'])) { |
108
|
|
|
return new FactoryClass('default'); |
109
|
|
|
} |
110
|
|
|
return new FactoryClass($config['client_factory']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getExpectationsPathConfiguration(array $config): DirectoryPath |
114
|
|
|
{ |
115
|
|
|
if (isset($config['expectationsPath'])) { |
116
|
|
|
call_user_func( |
117
|
|
|
$this->output, |
118
|
|
|
'Phiremock/DEPRECATION: expectationsPath option is deprecated and will be removed. Please use expectations_path.' |
119
|
|
|
); |
120
|
|
|
$config[self::EXPECTATIONS_PATH_CONFIG] = $config['expectationsPath']; |
121
|
|
|
} |
122
|
|
|
$configuredPath = $config[self::EXPECTATIONS_PATH_CONFIG] ?? null; |
123
|
|
|
if (empty($configuredPath)) { |
124
|
|
|
$defaultPath = codecept_data_dir(self::EXPECTATIONS_PATH); |
125
|
|
|
return DirectoryPath::createAndGetInstance($defaultPath); |
126
|
|
|
} |
127
|
|
|
return DirectoryPath::createAbsoluteOrRelativeToCodeceptionDir($configuredPath); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function setResetBeforeEachTest($config) |
131
|
|
|
{ |
132
|
|
|
if (isset($config['resetBeforeEachTest'])) { |
133
|
|
|
call_user_func( |
134
|
|
|
$this->output, |
135
|
|
|
'Phiremock/DEPRECATION: resetBeforeEachTest option is deprecated and will be removed. Please use reset_before_each_test.' |
136
|
|
|
); |
137
|
|
|
$config['reset_before_each_test'] = $config['resetBeforeEachTest']; |
138
|
|
|
} |
139
|
|
|
$this->resetBeforeEachTest = $config['reset_before_each_test']; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function initExtraConnections(array $config): void |
143
|
|
|
{ |
144
|
|
|
$this->extraConnections = []; |
145
|
|
|
if (isset($config['extra_connections'])) { |
146
|
|
|
foreach ($config['extra_connections'] as $connectionName => $extraConnection) { |
147
|
|
|
$connectionConfig = $extraConnection + self::DEFAULT_CONFIG; |
148
|
|
|
unset($connectionConfig['extra_connections']); |
149
|
|
|
$this->extraConnections[$connectionName] = new Config($connectionConfig, $this->output); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|