Completed
Push — master ( 023e21...5ec956 )
by Guillaume
07:09
created

InitHandlerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 135
Duplicated Lines 2.22 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 3
c 4
b 0
f 1
lcom 1
cbo 8
dl 3
loc 135
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testExistingConfigurationFile() 3 73 2
A testDefaultConfigurationFile() 0 52 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
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...
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) {
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...
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()
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...
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()
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...
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