Completed
Push — master ( 216ca8...2dc9c5 )
by Robbie
11s
created

testNestedArrayValuesAreDisplayedAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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, overrides
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
     * @covers ::getMergedData
94
     */
95 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...
96
    {
97
        Config::inst()->update('Gorilla', 'warfare', 'magpie fairy bread');
98
        $result = $this->executeTest(['type' => 'all'])->getDisplay();
99
        $this->assertContains('has_one', $result);
100
        $this->assertContains('%$DisplayErrorHandler', $result);
101
        $this->assertContains('magpie fairy bread', $result);
102
    }
103
104
    /**
105
     * Ensure that the configuration source can be set to only YAML file data
106
     *
107
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getYamlConfig
108
     */
109
    public function testGetOnlyYamlConfiguration()
110
    {
111
        $result = $this->executeTest(['type' => 'yaml'])->getDisplay();
112
        $this->assertNotContains('has_one', $result);
113
        $this->assertContains('%$DisplayErrorHandler', $result);
114
    }
115
116
    /**
117
     * Ensure that the configuration source can be set to only private statics
118
     *
119
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getStaticConfig
120
     */
121
    public function testGetOnlyStaticConfiguration()
122
    {
123
        $result = $this->executeTest(['type' => 'static'])->getDisplay();
124
        $this->assertContains('has_one', $result);
125
        $this->assertNotContains('%$DisplayErrorHandler', $result);
126
    }
127
128
    /**
129
     * Test that override configuration only can be returned
130
     *
131
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getConfigOverrides
132
     */
133 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...
134
    {
135
        Config::inst()->update('Bookcase', 'dresser', 'drawers');
136
        $result = $this->executeTest(['type' => 'overrides'])->getDisplay();
137
        $this->assertContains('Bookcase', $result);
138
        $this->assertNotContains('Injector', $result);
139
    }
140
141
    /**
142
     * Test that the "all" type is treated the same as not providing one (i.e. default)
143
     *
144
     * @covers ::getSourceData
145
     */
146
    public function testAllIsDefaultType()
147
    {
148
        $typeAll = $this->executeTest(['type' => 'all'])->getDisplay();
149
        $typeNone = $this->executeTest()->getDisplay();
150
151
        $this->assertSame($typeAll, $typeNone);
152
    }
153
154
    /**
155
     * Ensure that numeric property keys are replaced with nada
156
     *
157
     * @covers ::getParsedOutput
158
     */
159
    public function testNumericKeysAreNotShown()
160
    {
161
        Config::inst()->update('FooBar', 'my_property', [1 => 'baz', 'bar' => 'banter']);
162
        $result = $this->executeTest(['type' => 'overrides', '--filter' => 'FooBar'])->getDisplay();
163
        $this->assertNotContains('1', $result);
164
        $this->assertContains('bar', $result);
165
    }
166
167
    /**
168
     * Ensure that nested array values for properties are displayed as JSON. Since it crosses multiple lines,
169
     * we can't assert it exactly.
170
     *
171
     * @covers ::getParsedOutput
172
     */
173
    public function testNestedArrayValuesAreDisplayedAsJson()
174
    {
175
        $input = ['brands' => ['good' => 'Heatings R Us', 'great' => 'Never-B-Cold', 'best' => 'Luv-Fyre']];
176
        Config::inst()->update('HeatingSupplies', 'brands', $input);
177
        $result = $this->executeTest()->getDisplay();
178
        $this->assertContains('"great": "Never-B-Cold",', $result);
179
    }
180
181
    /**
182
     * While this behaviour is not desireable, it is what it is. For now, test that the private statics
183
     * are gathered from children of Object only.
184
     *
185
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getStaticConfig
186
     */
187
    public function testFindStaticsForSubclassesOfObjectOnly()
188
    {
189
        $result = $this->executeTest(['type' => 'static'])->getDisplay();
190
        $this->assertNotContains(
191
            'Injector',
192
            $result,
193
            'Injector has a private static property, but does not extend Object - should not be displayed'
194
        );
195
        $this->assertContains('SilverStripe\\ORM\\DataObject', $result, 'DataObject should definitely be displayed.');
196
    }
197
198
    /**
199
     * Ensure that the ConfigManifest is returned
200
     *
201
     * @covers \SilverLeague\Console\Command\Config\AbstractConfigCommand::getConfigManifest
202
     */
203
    public function testGetConfigManifest()
204
    {
205
        $result = $this->command->getConfigManifest();
0 ignored issues
show
Bug introduced by
The method getConfigManifest() does not exist on SilverLeague\Console\Command\SilverStripeCommand. Did you maybe mean getConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
206
        $this->assertInstanceOf('SilverStripe\\Core\\Manifest\\ConfigManifest', $result);
207
    }
208
}
209