|
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
|
|
|
$this->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
|
|
|
'timeout' => 1, |
|
35
|
|
|
'status_code' => 200, |
|
36
|
|
|
], |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
$content = ''; |
|
41
|
|
View Code Duplication |
foreach ($configuration as $name => $section) { |
|
|
|
|
|
|
42
|
|
|
$content .= Yaml::dump([$name => $section], 4).PHP_EOL; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
file_put_contents($this->file, $content); |
|
46
|
|
|
|
|
47
|
|
|
//Test if the enduser configuration is kept by the ConfigurationLoader |
|
48
|
|
|
$configurationLoader = new ConfigurationLoader(); |
|
49
|
|
|
|
|
50
|
|
|
$configurationDumper = new ConfigurationDumper(); |
|
51
|
|
|
$filesystem = new Filesystem(); |
|
52
|
|
|
|
|
53
|
|
|
$argsMock = $this->prophesize(Args::class); |
|
54
|
|
|
$argsMock |
|
55
|
|
|
->getOption(Argument::type('string')) |
|
56
|
|
|
->willReturn(sys_get_temp_dir()); |
|
57
|
|
|
|
|
58
|
|
|
$ioMock = $this->prophesize(IO::class); |
|
59
|
|
|
$ioMock |
|
60
|
|
|
->writeLine(Argument::type('string')) |
|
61
|
|
|
->shouldBeCalled(); |
|
62
|
|
|
|
|
63
|
|
|
$commandMock = $this->prophesize(Command::class); |
|
64
|
|
|
|
|
65
|
|
|
$initHandler = new InitHandler( |
|
66
|
|
|
$configurationLoader, |
|
67
|
|
|
$configurationDumper, |
|
68
|
|
|
$filesystem |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$initHandler->handle( |
|
72
|
|
|
$argsMock->reveal(), |
|
73
|
|
|
$ioMock->reveal(), |
|
74
|
|
|
$commandMock->reveal() |
|
|
|
|
|
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
// Test the configuration of the enduser file |
|
78
|
|
|
$this->assertEquals( |
|
79
|
|
|
$this->file, |
|
80
|
|
|
$configurationLoader->getConfigurationFilepath() |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals( |
|
84
|
|
|
[ |
|
85
|
|
|
'urls' => [ |
|
86
|
|
|
'example.com' => [ |
|
87
|
|
|
'url' => 'https://www.example.com', |
|
88
|
|
|
'timeout' => 1, |
|
89
|
|
|
'status_code' => 200, |
|
90
|
|
|
], |
|
91
|
|
|
], |
|
92
|
|
|
], |
|
93
|
|
|
Yaml::parse(file_get_contents($this->file)) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
unlink($this->file); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* testDefaultConfigurationFile. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testDefaultConfigurationFile() |
|
103
|
|
|
{ |
|
104
|
|
|
$testFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME; |
|
105
|
|
|
|
|
106
|
|
|
$configurationLoader = new ConfigurationLoader(); |
|
107
|
|
|
$configurationDumper = new ConfigurationDumper(); |
|
108
|
|
|
$filesystem = new Filesystem(); |
|
109
|
|
|
|
|
110
|
|
|
$argsMock = $this->prophesize(Args::class); |
|
111
|
|
|
$argsMock |
|
112
|
|
|
->getOption(Argument::type('string')) |
|
113
|
|
|
->willReturn(sys_get_temp_dir()); |
|
114
|
|
|
$ioMock = $this->prophesize(IO::class); |
|
115
|
|
|
$ioMock |
|
116
|
|
|
->writeLine(Argument::type('string')) |
|
117
|
|
|
->shouldBeCalled(); |
|
118
|
|
|
|
|
119
|
|
|
$commandMock = $this->prophesize(Command::class); |
|
120
|
|
|
|
|
121
|
|
|
$initHandler = new InitHandler( |
|
122
|
|
|
$configurationLoader, |
|
123
|
|
|
$configurationDumper, |
|
124
|
|
|
$filesystem |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$initHandler->handle( |
|
128
|
|
|
$argsMock->reveal(), |
|
129
|
|
|
$ioMock->reveal(), |
|
130
|
|
|
$commandMock->reveal() |
|
|
|
|
|
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
// Test the configuration of the enduser file |
|
134
|
|
|
$this->assertEquals( |
|
135
|
|
|
$testFile, |
|
136
|
|
|
$configurationLoader->getConfigurationFilepath() |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals( |
|
140
|
|
|
[ |
|
141
|
|
|
'urls' => [ |
|
142
|
|
|
'google' => [ |
|
143
|
|
|
'url' => 'https://www.google.fr', |
|
144
|
|
|
'timeout' => 1, |
|
145
|
|
|
'status_code' => 200, |
|
146
|
|
|
], |
|
147
|
|
|
], |
|
148
|
|
|
], |
|
149
|
|
|
Yaml::parse(file_get_contents($testFile)) |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
unlink($testFile); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: