ProcessorTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 219
Duplicated Lines 2.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 6
loc 219
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 12 3
A testInvalidConfiguration() 0 8 1
B provideInvalidConfiguration() 0 83 1
A testParameterHandling() 0 30 2
A initializeTestCase() 0 23 4
A setInteractionExpectations() 0 18 4
A provideParameterHandlingTestCases() 6 14 3

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 Incenteev\ParameterHandler\Tests;
4
5
use Incenteev\ParameterHandler\Processor;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Yaml\Yaml;
8
9
class ProcessorTest extends \PHPUnit_Framework_TestCase
10
{
11
    private $io;
12
    private $environmentBackup = array();
13
14
    /**
15
     * @var Processor
16
     */
17
    private $processor;
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->io = $this->prophesize('Composer\IO\IOInterface');
24
        $this->processor = new Processor($this->io->reveal());
25
    }
26
27
    protected function tearDown()
28
    {
29
        parent::tearDown();
30
31
        foreach ($this->environmentBackup as $var => $value) {
32
            if (false === $value) {
33
                putenv($var);
34
            } else {
35
                putenv($var.'='.$value);
36
            }
37
        }
38
    }
39
40
    /**
41
     * @dataProvider provideInvalidConfiguration
42
     */
43
    public function testInvalidConfiguration(array $config, $exceptionMessage)
44
    {
45
        chdir(__DIR__);
46
47
        $this->setExpectedException('InvalidArgumentException', $exceptionMessage);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48
49
        $this->processor->processFile($config);
50
    }
51
52
    public function provideInvalidConfiguration()
53
    {
54
        return array(
55
            'yml: no file' => array(
56
                array(),
57
                'The extra.incenteev-parameters.file setting is required to use this script handler.',
58
            ),
59
            'yml: no file type' => array(
60
                array(
61
                    'file' => 'fixtures/existent/yml/dist.yml',
62
                ),
63
                'The extra.incenteev-parameters.file-type setting is required to use this script handler.',
64
            ),
65
            'yml: missing default dist file' => array(
66
                array(
67
                    'file' => 'fixtures/invalid/yml/missing.yml',
68
                    'file-type' => 'yml',
69
                ),
70
                'The dist file "fixtures/invalid/yml/missing.yml.dist" does not exist. Check your dist-file config or create it.',
71
            ),
72
            'yml: missing custom dist file' => array(
73
                array(
74
                    'file' => 'fixtures/invalid/yml/missing.yml',
75
                    'dist-file' => 'fixtures/invalid/yml/non-existent.dist.yml',
76
                    'file-type' => 'yml',
77
                ),
78
                'The dist file "fixtures/invalid/yml/non-existent.dist.yml" does not exist. Check your dist-file config or create it.',
79
            ),
80
            'yml: missing top level key in dist file' => array(
81
                array(
82
                    'file' => 'fixtures/invalid/yml/missing_top_level.yml',
83
                    'file-type' => 'yml',
84
                ),
85
                'The top-level key parameters is missing.',
86
            ),
87
            'yml: invalid values in the existing file' => array(
88
                array(
89
                    'file' => 'fixtures/invalid/yml/invalid_existing_values.yml',
90
                    'file-type' => 'yml',
91
                ),
92
                'The existing "fixtures/invalid/yml/invalid_existing_values.yml" file does not contain an array',
93
            ),
94
            'php: no file' => array(
95
                array(),
96
                'The extra.incenteev-parameters.file setting is required to use this script handler.',
97
            ),
98
            'php: no file type' => array(
99
                array(
100
                    'file' => 'fixtures/existent/php/dist.php',
101
                ),
102
                'The extra.incenteev-parameters.file-type setting is required to use this script handler.',
103
            ),
104
            'php: missing default dist file' => array(
105
                array(
106
                    'file' => 'fixtures/invalid/php/missing.php',
107
                    'file-type' => 'php',
108
                ),
109
                'The dist file "fixtures/invalid/php/missing.php.dist" does not exist. Check your dist-file config or create it.',
110
            ),
111
            'php: missing custom dist file' => array(
112
                array(
113
                    'file' => 'fixtures/invalid/php/missing.php',
114
                    'dist-file' => 'fixtures/invalid/php/non-existent.dist.php',
115
                    'file-type' => 'php',
116
                ),
117
                'The dist file "fixtures/invalid/php/non-existent.dist.php" does not exist. Check your dist-file config or create it.',
118
            ),
119
            'php: missing top level key in dist file' => array(
120
                array(
121
                    'file' => 'fixtures/invalid/php/missing_top_level.php',
122
                    'file-type' => 'php',
123
                ),
124
                'The top-level key parameters is missing.',
125
            ),
126
            'php: invalid values in the existing file' => array(
127
                array(
128
                    'file' => 'fixtures/invalid/php/invalid_existing_values.php',
129
                    'file-type' => 'php',
130
                ),
131
                'The existing "fixtures/invalid/php/invalid_existing_values.php" file does not contain an array',
132
            ),
133
        );
134
    }
135
136
    /**
137
     * @dataProvider provideParameterHandlingTestCases
138
     */
139
    public function testParameterHandling($testCaseName, $fileType)
140
    {
141
        $dataDir = __DIR__.'/fixtures/testcases/'. $fileType . '/' .$testCaseName;
142
143
        $testCase = array_replace_recursive(
144
            array(
145
                'title' => 'unknown test',
146
                'config' => array(
147
                    'file' => 'parameters.' . $fileType,
148
                    'file-type' => $fileType,
149
                ),
150
                'dist-file' => 'parameters.'. $fileType .'.dist',
151
                'environment' => array(),
152
                'interactive' => false,
153
            ),
154
            (array) Yaml::parse(file_get_contents($dataDir.'/setup.yml'))
155
        );
156
157
        $workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler';
158
        $exists = $this->initializeTestCase($testCase, $dataDir, $workingDir, $fileType);
159
160
        $message = sprintf('<info>%s the "%s" file</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']);
161
        $this->io->write($message)->shouldBeCalled();
162
163
        $this->setInteractionExpectations($testCase);
164
165
        $this->processor->processFile($testCase['config']);
166
167
        $this->assertFileEquals($dataDir.'/expected.' . $fileType, $workingDir.'/'.$testCase['config']['file'], $testCase['title']);
168
    }
169
170
    private function initializeTestCase(array $testCase, $dataDir, $workingDir, $fileType)
171
    {
172
        $fs = new Filesystem();
173
174
        if (is_dir($workingDir)) {
175
            $fs->remove($workingDir);
176
        }
177
178
        $fs->copy($dataDir.'/dist.' . $fileType, $workingDir.'/'. $testCase['dist-file']);
179
180
        if ($exists = file_exists($dataDir.'/existing.' . $fileType)) {
181
            $fs->copy($dataDir.'/existing.' . $fileType, $workingDir.'/'.$testCase['config']['file']);
182
        }
183
184
        foreach ($testCase['environment'] as $var => $value) {
185
            $this->environmentBackup[$var] = getenv($var);
186
            putenv($var.'='.$value);
187
        };
188
189
        chdir($workingDir);
190
191
        return $exists;
192
    }
193
194
    private function setInteractionExpectations(array $testCase)
195
    {
196
        $this->io->isInteractive()->willReturn($testCase['interactive']);
197
198
        if (!$testCase['interactive']) {
199
            return;
200
        }
201
202
        if (!empty($testCase['requested_params'])) {
203
            $this->io->write('<comment>Some parameters are missing. Please provide them.</comment>')->shouldBeCalledTimes(1);
204
        }
205
206
        foreach ($testCase['requested_params'] as $param => $settings) {
207
            $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $param, $settings['default']), $settings['default'])
208
                ->willReturn($settings['input'])
209
                ->shouldBeCalled();
210
        }
211
    }
212
213
    public function provideParameterHandlingTestCases()
214
    {
215
        $tests = array();
216
217 View Code Duplication
        foreach (glob(__DIR__.'/fixtures/testcases/yml/*/') as $folder) {
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...
218
            $tests[] = array(basename($folder), 'yml');
219
        }
220
221 View Code Duplication
        foreach (glob(__DIR__.'/fixtures/testcases/php/*/') as $folder) {
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...
222
            $tests[] = array(basename($folder), 'php');
223
        }
224
225
        return $tests;
226
    }
227
}
228