1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hogosha\Monitor\Client; |
4
|
|
|
|
5
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationDumper; |
6
|
|
|
use Hogosha\Monitor\Configuration\ConfigurationLoader; |
7
|
|
|
use Hogosha\Monitor\Console\Handler\InitHandler; |
8
|
|
|
use Hogosha\Monitor\Monitor; |
9
|
|
|
use Prophecy\Argument; |
10
|
|
|
use Prophecy\type; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
use Webmozart\Console\Api\Args\Args; |
14
|
|
|
use Webmozart\Console\Api\Command\Command; |
15
|
|
|
use Webmozart\Console\Api\IO\IO; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Guillaume Cavana <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class InitHandlerTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* testExistingConfigurationFile. |
24
|
|
|
*/ |
25
|
|
|
public function testExistingConfigurationFile() |
26
|
|
|
{ |
27
|
|
|
$file = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME; |
28
|
|
|
|
29
|
|
|
// Dump the configuration set by the enduser |
30
|
|
|
$configuration = [ |
31
|
|
|
'urls' => [ |
32
|
|
|
'example.com' => [ |
33
|
|
|
'url' => 'https://www.example.com', |
34
|
|
|
'method' => 'GET', |
35
|
|
|
'headers' => [], |
36
|
|
|
'timeout' => 1, |
37
|
|
|
'status_code' => 200, |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$content = ''; |
43
|
|
View Code Duplication |
foreach ($configuration as $name => $section) { |
|
|
|
|
44
|
|
|
$content .= Yaml::dump([$name => $section], 4).PHP_EOL; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
file_put_contents($file, $content); |
48
|
|
|
|
49
|
|
|
//Test if the enduser configuration is kept by the ConfigurationLoader |
50
|
|
|
$configurationLoader = new ConfigurationLoader(); |
51
|
|
|
|
52
|
|
|
$configurationDumper = new ConfigurationDumper(); |
53
|
|
|
$filesystem = new Filesystem(); |
54
|
|
|
|
55
|
|
|
$argsMock = $this->prophesize(Args::class); |
56
|
|
|
$argsMock |
57
|
|
|
->getOption('config') |
58
|
|
|
->willReturn(sys_get_temp_dir()); |
59
|
|
|
$argsMock |
60
|
|
|
->getOption('force') |
61
|
|
|
->willReturn(null); |
62
|
|
|
|
63
|
|
|
$ioMock = $this->prophesize(IO::class); |
64
|
|
|
$ioMock |
65
|
|
|
->writeLine(Argument::type('string')) |
66
|
|
|
->shouldBeCalled(); |
67
|
|
|
|
68
|
|
|
$commandMock = $this->prophesize(Command::class); |
69
|
|
|
|
70
|
|
|
$initHandler = new InitHandler( |
71
|
|
|
$configurationLoader, |
72
|
|
|
$configurationDumper, |
73
|
|
|
$filesystem |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$initHandler->handle( |
77
|
|
|
$argsMock->reveal(), |
78
|
|
|
$ioMock->reveal(), |
79
|
|
|
$commandMock->reveal() |
|
|
|
|
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// Test the configuration of the enduser file |
83
|
|
|
$this->assertEquals( |
84
|
|
|
$file, |
85
|
|
|
$configurationLoader->getConfigurationFilepath() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( |
89
|
|
|
[ |
90
|
|
|
'urls' => [ |
91
|
|
|
'example.com' => [ |
92
|
|
|
'url' => 'https://www.example.com', |
93
|
|
|
'method' => 'GET', |
94
|
|
|
'headers' => [], |
95
|
|
|
'timeout' => 1, |
96
|
|
|
'status_code' => 200, |
97
|
|
|
], |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
Yaml::parse(file_get_contents($file)) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
unlink($file); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* testDefaultConfigurationFile. |
108
|
|
|
*/ |
109
|
|
|
public function testDefaultConfigurationFile() |
110
|
|
|
{ |
111
|
|
|
$testFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME; |
112
|
|
|
|
113
|
|
|
$configurationLoader = new ConfigurationLoader(); |
114
|
|
|
$configurationDumper = new ConfigurationDumper(); |
115
|
|
|
$filesystem = new Filesystem(); |
116
|
|
|
|
117
|
|
|
$argsMock = $this->prophesize(Args::class); |
118
|
|
|
$argsMock |
119
|
|
|
->getOption(Argument::type('string')) |
120
|
|
|
->willReturn(sys_get_temp_dir()); |
121
|
|
|
$ioMock = $this->prophesize(IO::class); |
122
|
|
|
$ioMock |
123
|
|
|
->writeLine(Argument::type('string')) |
124
|
|
|
->shouldBeCalled(); |
125
|
|
|
|
126
|
|
|
$commandMock = $this->prophesize(Command::class); |
127
|
|
|
|
128
|
|
|
$initHandler = new InitHandler( |
129
|
|
|
$configurationLoader, |
130
|
|
|
$configurationDumper, |
131
|
|
|
$filesystem |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$initHandler->handle( |
135
|
|
|
$argsMock->reveal(), |
136
|
|
|
$ioMock->reveal(), |
137
|
|
|
$commandMock->reveal() |
|
|
|
|
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
// Test the configuration of the enduser file |
141
|
|
|
$this->assertEquals( |
142
|
|
|
$testFile, |
143
|
|
|
$configurationLoader->getConfigurationFilepath() |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$this->assertEquals( |
147
|
|
|
[ |
148
|
|
|
'urls' => [ |
149
|
|
|
'google' => [ |
150
|
|
|
'url' => 'https://www.google.fr', |
151
|
|
|
'method' => 'GET', |
152
|
|
|
'headers' => [], |
153
|
|
|
'timeout' => 1, |
154
|
|
|
'status_code' => 200, |
155
|
|
|
], |
156
|
|
|
], |
157
|
|
|
], |
158
|
|
|
Yaml::parse(file_get_contents($testFile)) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
unlink($testFile); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.