Completed
Push — master ( 4f2fad...dd1cd3 )
by Guillaume
07:10
created

InitHandlerTest::testExistingConfigurationFile()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 93
Code Lines 58

Duplication

Lines 3
Ratio 3.23 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 93
rs 8.4643
cc 2
eloc 58
nc 2
nop 0

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
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\DependencyInjection\Exception\ConfigurationLoadingException;
9
use Hogosha\Monitor\Monitor;
10
use Prophecy\Argument;
11
use Prophecy\type;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\Yaml\Yaml;
14
use Webmozart\Console\Api\Args\Args;
15
use Webmozart\Console\Api\Command\Command;
16
use Webmozart\Console\Api\IO\IO;
17
18
/**
19
 * @author Guillaume Cavana <[email protected]>
20
 */
21
class InitHandlerTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * testExistingConfigurationFile.
25
     */
26
    public function testExistingConfigurationFile()
27
    {
28
29
        $this->file = sys_get_temp_dir().DIRECTORY_SEPARATOR.Monitor::CONFIG_FILENAME;
0 ignored issues
show
Bug introduced by
The property file does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        // Dump the configuration set by the enduser
32
        $configuration = [
33
            'server' => [
34
                'hostname' => null,
35
                'port' => null,
36
                'use_ssl' => null,
37
                'auth' => [
38
                    'username' => null,
39
                    'password' => null,
40
                ],
41
            ],
42
            'urls' => [
43
                'example.com' => [
44
                    'url' => 'https://www.example.com',
45
                    'timeout' => 1,
46
                    'status_code' => 200,
47
                    'service_uid' => '',
48
                ],
49
            ],
50
        ];
51
52
        $content = '';
53 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...
54
            $content .= Yaml::dump([$name => $section], 4).PHP_EOL;
55
        }
56
57
        file_put_contents($this->file, $content);
58
59
        //Test if the enduser configuration is kept by the ConfigurationLoader
60
        $configurationLoader =  new ConfigurationLoader(
61
            sys_get_temp_dir(),
62
            Monitor::CONFIG_FILENAME
63
        );
64
65
        $configurationDumper = new ConfigurationDumper();
66
        $filesystem = new Filesystem();
67
68
        $argsMock = $this->prophesize(Args::class);
69
        $ioMock = $this->prophesize(IO::class);
70
        $ioMock
71
            ->writeLine(Argument::type('string'))
72
            ->shouldBeCalled();
73
74
        $commandMock = $this->prophesize(Command::class);
75
76
        $initHandler = new InitHandler(
77
            $configurationLoader,
78
            $configurationDumper,
79
            $filesystem
80
        );
81
82
        $initHandler->handle(
83
            $argsMock->reveal(),
84
            $ioMock->reveal(),
85
            $commandMock->reveal()
86
        );
87
88
        // Test the configuration of the enduser file
89
        $this->assertEquals(
90
            $this->file,
91
            $configurationLoader->getConfigurationFilepath()
92
        );
93
94
        $this->assertEquals(
95
            [
96
                'server' => [
97
                    'hostname' => null,
98
                    'port' => null,
99
                    'use_ssl' => null,
100
                    'auth' => [
101
                        'username' => null,
102
                        'password' => null,
103
                    ],
104
                ],
105
                'urls' => [
106
                    'example.com' => [
107
                        'url' => 'https://www.example.com',
108
                        'timeout' => 1,
109
                        'status_code' => 200,
110
                        'service_uid' => '',
111
                    ],
112
                ],
113
            ],
114
            Yaml::parse(file_get_contents($this->file))
115
        );
116
117
        unlink($this->file);
118
    }
119
120
    /**
121
     * testDefaultConfigurationFile.
122
     */
123
    public function testDefaultConfigurationFile()
124
    {
125
        $testFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.'.test.yml';
126
127
        $configurationLoader =  new ConfigurationLoader(
128
            sys_get_temp_dir(),
129
            '.test.yml'
130
        );
131
132
        $configurationDumper = new ConfigurationDumper();
133
        $filesystem = new Filesystem();
134
135
        $argsMock = $this->prophesize(Args::class);
136
        $ioMock = $this->prophesize(IO::class);
137
        $ioMock
138
            ->writeLine(Argument::type('string'))
139
            ->shouldBeCalled();
140
141
        $commandMock = $this->prophesize(Command::class);
142
143
        $initHandler = new InitHandler(
144
            $configurationLoader,
145
            $configurationDumper,
146
            $filesystem
147
        );
148
149
        $initHandler->handle(
150
            $argsMock->reveal(),
151
            $ioMock->reveal(),
152
            $commandMock->reveal()
153
        );
154
155
        // Test the configuration of the enduser file
156
        $this->assertEquals(
157
            $testFile,
158
            $configurationLoader->getConfigurationFilepath()
159
        );
160
161
        $this->assertEquals(
162
            [
163
                'server' => [
164
                    'hostname' => null,
165
                    'port' => null,
166
                    'use_ssl' => null,
167
                    'auth' => [
168
                        'username' => null,
169
                        'password' => null,
170
                    ],
171
                ],
172
                'urls' => [
173
                    'google' => [
174
                        'url' => 'https://www.google.fr',
175
                        'timeout' => 1,
176
                        'status_code' => 200,
177
                        'service_uid' => '',
178
                    ],
179
                ],
180
            ],
181
            Yaml::parse(file_get_contents($testFile))
182
        );
183
184
        unlink($testFile);
185
    }
186
}
187