Completed
Pull Request — master (#35)
by Robbie
02:14
created

DumpCommandTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 184
Duplicated Lines 8.15 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 2
cbo 4
dl 15
loc 184
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestCommand() 0 4 1
A testConfigure() 0 5 1
A testInvalidTypeThrowsException() 0 4 1
A testExecute() 0 6 1
A testExecuteWithFilteredResults() 0 6 1
A testFilterOnAnyColumn() 0 8 1
A testAllDataContainsBothYamlAndStatic() 8 8 1
A testGetOnlyYamlConfiguration() 0 6 1
A testGetOnlyStaticConfiguration() 0 6 1
A testGetOnlyOverrideConfiguration() 7 7 1
A testAllIsDefaultType() 0 7 1
A testNumericKeysAreNotShown() 0 7 1
A testNestedArrayValuesAreDisplayedAsJson() 0 7 1
A testFindStaticsForSubclassesOfObjectOnly() 0 10 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 SilverLeague\Console\Tests\Command\Config;
4
5
use SilverLeague\Console\Tests\Command\AbstractCommandTest;
6
use SilverStripe\Core\Config\Config;
7
8
/**
9
 * @coversDefaultClass \SilverLeague\Console\Command\Config\DumpCommand
10
 * @package silverstripe-console
11
 * @author  Robbie Averill <[email protected]>
12
 */
13
class DumpCommandTest extends AbstractCommandTest
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected function getTestCommand()
19
    {
20
        return 'config:dump';
21
    }
22
23
    /**
24
     * Ensure that the InputOptions exist
25
     *
26
     * @covers ::configure
27
     */
28
    public function testConfigure()
29
    {
30
        $this->assertTrue($this->command->getDefinition()->hasArgument('type'));
31
        $this->assertTrue($this->command->getDefinition()->hasOption('filter'));
32
    }
33
34
    /**
35
     * Ensure that passing an invalid "type" argument throws an exception
36
     *
37
     * @covers ::execute
38
     * @expectedException \InvalidArgumentException
39
     * @expectedExceptionMessage foo is not a valid config type, options: all, yaml, static
40
     */
41
    public function testInvalidTypeThrowsException()
42
    {
43
        $this->executeTest(['type' => 'foo']);
44
    }
45
46
    /**
47
     * Test that the command can successfully be executed
48
     *
49
     * @covers ::execute
50
     */
51
    public function testExecute()
52
    {
53
        $result = $this->executeTest()->getDisplay();
54
        $this->assertContains('SilverStripe\\Control\\Director', $result);
55
        $this->assertContains('SilverStripe\\Core\\Injector\\Injector', $result);
56
    }
57
58
    /**
59
     * Test that the results can be filtered
60
     *
61
     * @covers ::execute
62
     * @covers ::filterOutput
63
     */
64
    public function testExecuteWithFilteredResults()
65
    {
66
        $result = $this->executeTest(['--filter' => 'SilverStripe\\Control\\Director'])->getDisplay();
67
        $this->assertContains('SilverStripe\\Control\\Director', $result);
68
        $this->assertNotContains('SilverStripe\\Core\\Injector\\Injector', $result);
69
    }
70
71
    /**
72
     * Ensure that the filter is applied to any column of the data
73
     *
74
     * @covers ::filterOutput
75
     */
76
    public function testFilterOnAnyColumn()
77
    {
78
        $result = $this->executeTest(['--filter' => '%$DisplayErrorHandler'])->getDisplay();
79
        $this->assertContains('pushHandler', $result);
80
81
        $result = $this->executeTest(['--filter' => 'pushHandler'])->getDisplay();
82
        $this->assertContains('%$DisplayErrorHandler', $result);
83
    }
84
85
    /**
86
     * Test that the source data can be set to YAML, static, overrides or "all"
87
     *
88
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getConfig
89
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getPropertyValue
90
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getStaticConfig
91
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getConfigOverrides
92
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getYamlConfig
93
     */
94 View Code Duplication
    public function testAllDataContainsBothYamlAndStatic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
95
    {
96
        Config::inst()->update('Gorilla', 'warfare', 'magpie fairy bread');
97
        $result = $this->executeTest(['type' => 'all'])->getDisplay();
98
        $this->assertContains('has_one', $result);
99
        $this->assertContains('%$DisplayErrorHandler', $result);
100
        $this->assertContains('magpie fairy bread', $result);
101
    }
102
103
    /**
104
     * Ensure that the configuration source can be set to only YAML file data
105
     *
106
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getYamlConfig
107
     */
108
    public function testGetOnlyYamlConfiguration()
109
    {
110
        $result = $this->executeTest(['type' => 'yaml'])->getDisplay();
111
        $this->assertNotContains('has_one', $result);
112
        $this->assertContains('%$DisplayErrorHandler', $result);
113
    }
114
115
    /**
116
     * Ensure that the configuration source can be set to only private statics
117
     *
118
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getStaticConfig
119
     */
120
    public function testGetOnlyStaticConfiguration()
121
    {
122
        $result = $this->executeTest(['type' => 'static'])->getDisplay();
123
        $this->assertContains('has_one', $result);
124
        $this->assertNotContains('%$DisplayErrorHandler', $result);
125
    }
126
127
    /**
128
     * Test that override configuration only can be returned
129
     *
130
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getConfigOverrides
131
     */
132 View Code Duplication
    public function testGetOnlyOverrideConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
133
    {
134
        Config::inst()->update('Bookcase', 'dresser', 'drawers');
135
        $result = $this->executeTest(['type' => 'overrides'])->getDisplay();
136
        $this->assertContains('Bookcase', $result);
137
        $this->assertNotContains('Injector', $result);
138
    }
139
140
    /**
141
     * Test that the "all" type is treated the same as not providing one (i.e. default)
142
     *
143
     * @covers ::getSourceData
144
     */
145
    public function testAllIsDefaultType()
146
    {
147
        $typeAll = $this->executeTest(['type' => 'all'])->getDisplay();
148
        $typeNone = $this->executeTest()->getDisplay();
149
150
        $this->assertSame($typeAll, $typeNone);
151
    }
152
153
    /**
154
     * Ensure that numeric property keys are replaced with nada
155
     *
156
     * @covers ::getParsedOutput
157
     */
158
    public function testNumericKeysAreNotShown()
159
    {
160
        Config::inst()->update('FooBar', 'my_property', [1 => 'baz', 'bar' => 'banter']);
161
        $result = $this->executeTest(['type' => 'overrides', '--filter' => 'FooBar'])->getDisplay();
162
        $this->assertNotContains('1', $result);
163
        $this->assertContains('bar', $result);
164
    }
165
166
    /**
167
     * Ensure that nested array values for properties are displayed as JSON. Since it crosses multiple lines,
168
     * we can't assert it exactly.
169
     *
170
     * @covers ::getParsedOutput
171
     */
172
    public function testNestedArrayValuesAreDisplayedAsJson()
173
    {
174
        $input = ['brands' => ['good' => 'Heatings R Us', 'great' => 'Never-B-Cold', 'best' => 'Luv-Fyre']];
175
        Config::inst()->update('HeatingSupplies', 'brands', $input);
176
        $result = $this->executeTest()->getDisplay();
177
        $this->assertContains('"great": "Never-B-Cold",', $result);
178
    }
179
180
    /**
181
     * While this behaviour is not desireable, it is what it is. For now, test that the private statics
182
     * are gathered from children of Object only.
183
     *
184
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getStaticConfig
185
     */
186
    public function testFindStaticsForSubclassesOfObjectOnly()
187
    {
188
        $result = $this->executeTest(['type' => 'static'])->getDisplay();
189
        $this->assertNotContains(
190
            'Injector',
191
            $result,
192
            'Injector has a private static property, but does not extend Object - should not be displayed'
193
        );
194
        $this->assertContains('SilverStripe\\ORM\\DataObject', $result, 'DataObject should definitely be displayed.');
195
    }
196
}
197