InitHandlerTest::testExistingConfigurationFile()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 104
Code Lines 72

Duplication

Lines 3
Ratio 2.88 %

Importance

Changes 8
Bugs 0 Features 2
Metric Value
cc 2
eloc 72
c 8
b 0
f 2
nc 2
nop 0
dl 3
loc 104
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Client;
17
18
use Hogosha\Monitor\Configuration\ConfigurationDumper;
19
use Hogosha\Monitor\Configuration\ConfigurationLoader;
20
use Hogosha\Monitor\Console\Handler\InitHandler;
21
use Hogosha\Monitor\Monitor;
22
use Prophecy\Argument;
23
use Prophecy\type;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\Yaml\Yaml;
26
use Webmozart\Console\Api\Args\Args;
27
use Webmozart\Console\Api\Command\Command;
28
use Webmozart\Console\Api\IO\IO;
29
30
/**
31
 * @author Guillaume Cavana <[email protected]>
32
 */
33
class InitHandlerTest extends \PHPUnit_Framework_TestCase
34
{
35
    /**
36
     * testExistingConfigurationFile.
37
     */
38
    public function testExistingConfigurationFile()
39
    {
40
        $file = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME;
41
42
        // Dump the configuration set by the enduser
43
        $configuration = [
44
            'urls' => [
45
                'example.com' => [
46
                    'url' => 'https://www.example.com',
47
                    'method' => 'GET',
48
                    'headers' => [],
49
                    'timeout' => 1,
50
                    'validator' => [],
51
                    'status_code' => 200,
52
                    'metric_uuid' => null,
53
                    'service_uuid' => null,
54
                ],
55
            ],
56
            'hogosha_portal' => [
57
                'username' => '',
58
                'password' => '',
59
                'base_uri' => 'http://localhost:8000/api/',
60
                'metric_update' => false,
61
                'incident_update' => false,
62
                'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%',
63
                'default_resolved_incident_message' => 'The service %service_name% is back to normal',
64
            ],
65
        ];
66
67
        $content = '';
68 View Code Duplication
        foreach ($configuration as $name => $section) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
69
            $content .= Yaml::dump([$name => $section], 4).PHP_EOL;
70
        }
71
72
        file_put_contents($file, $content);
73
74
        //Test if the enduser configuration is kept by the ConfigurationLoader
75
        $configurationLoader = new ConfigurationLoader();
76
77
        $configurationDumper = new ConfigurationDumper();
78
        $filesystem = new Filesystem();
79
80
        $argsMock = $this->prophesize(Args::class);
81
        $argsMock
82
            ->getOption('config')
83
            ->willReturn(sys_get_temp_dir());
84
        $argsMock
85
            ->getOption('force')
86
            ->willReturn(null);
87
88
        $ioMock = $this->prophesize(IO::class);
89
        $ioMock
90
            ->writeLine(Argument::type('string'))
91
            ->shouldBeCalled();
92
93
        $commandMock = $this->prophesize(Command::class);
94
95
        $initHandler = new InitHandler(
96
            $configurationLoader,
97
            $configurationDumper,
98
            $filesystem
99
        );
100
101
        $initHandler->handle(
102
            $argsMock->reveal(),
103
            $ioMock->reveal(),
104
            $commandMock->reveal()
0 ignored issues
show
Unused Code introduced by
The call to InitHandler::handle() has too many arguments starting with $commandMock->reveal().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
        );
106
107
        // Test the configuration of the enduser file
108
        $this->assertEquals(
109
            $file,
110
            $configurationLoader->getConfigurationFilepath()
111
        );
112
113
        $this->assertEquals(
114
            [
115
                'urls' => [
116
                    'example.com' => [
117
                        'url' => 'https://www.example.com',
118
                        'method' => 'GET',
119
                        'headers' => [],
120
                        'timeout' => 1,
121
                        'validator' => [],
122
                        'status_code' => 200,
123
                        'metric_uuid' => null,
124
                        'service_uuid' => null,
125
                    ],
126
                ],
127
                'hogosha_portal' => [
128
                    'username' => '',
129
                    'password' => '',
130
                    'base_uri' => 'http://localhost:8000/api/',
131
                    'metric_update' => false,
132
                    'incident_update' => false,
133
                    'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%',
134
                    'default_resolved_incident_message' => 'The service %service_name% is back to normal',
135
                ],
136
            ],
137
            Yaml::parse(file_get_contents($file))
138
        );
139
140
        unlink($file);
141
    }
142
143
    /**
144
     * testDefaultConfigurationFile.
145
     */
146
    public function testDefaultConfigurationFile()
147
    {
148
        $testFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME;
149
150
        $configurationLoader = new ConfigurationLoader();
151
        $configurationDumper = new ConfigurationDumper();
152
        $filesystem = new Filesystem();
153
154
        $argsMock = $this->prophesize(Args::class);
155
        $argsMock
156
            ->getOption(Argument::type('string'))
157
            ->willReturn(sys_get_temp_dir());
158
        $ioMock = $this->prophesize(IO::class);
159
        $ioMock
160
            ->writeLine(Argument::type('string'))
161
            ->shouldBeCalled();
162
163
        $commandMock = $this->prophesize(Command::class);
164
165
        $initHandler = new InitHandler(
166
            $configurationLoader,
167
            $configurationDumper,
168
            $filesystem
169
        );
170
171
        $initHandler->handle(
172
            $argsMock->reveal(),
173
            $ioMock->reveal(),
174
            $commandMock->reveal()
0 ignored issues
show
Unused Code introduced by
The call to InitHandler::handle() has too many arguments starting with $commandMock->reveal().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
175
        );
176
177
        // Test the configuration of the enduser file
178
        $this->assertEquals(
179
            $testFile,
180
            $configurationLoader->getConfigurationFilepath()
181
        );
182
183
        $this->assertEquals(
184
            [
185
                'urls' => [
186
                    'google' => [
187
                        'url' => 'https://www.google.fr',
188
                        'method' => 'GET',
189
                        'headers' => [],
190
                        'timeout' => 1,
191
                        'validator' => [],
192
                        'status_code' => 200,
193
                        'metric_uuid' => null,
194
                        'service_uuid' => null,
195
                    ],
196
                ],
197
                'hogosha_portal' => [
198
                    'username' => '',
199
                    'password' => '',
200
                    'base_uri' => 'http://localhost:8000/api/',
201
                    'metric_update' => false,
202
                    'incident_update' => false,
203
                    'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%',
204
                    'default_resolved_incident_message' => 'The service %service_name% is back to normal',
205
                ],
206
            ],
207
            Yaml::parse(file_get_contents($testFile))
208
        );
209
210
        unlink($testFile);
211
    }
212
}
213