Completed
Push — master ( 483143...6b44c3 )
by Guillaume
05:17
created

InitHandlerTest::testExistingConfigurationFile()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 80
Code Lines 50

Duplication

Lines 3
Ratio 3.75 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 3
loc 80
rs 8.8387
cc 2
eloc 50
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\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) {
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...
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()
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...
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()
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...
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