Completed
Pull Request — master (#91)
by Andrew
01:42
created

SilverStripeCollectorTest::testGetWidgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 9.2083
c 0
b 0
f 0
cc 1
eloc 53
nc 1
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 LeKoala\DebugBar\Test\Collector;
4
5
use LeKoala\DebugBar\DebugBar;
6
use LeKoala\DebugBar\Collector\SilverStripeCollector;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\Session;
10
use SilverStripe\Core\Manifest\Module;
11
use SilverStripe\Core\Manifest\ModuleLoader;
12
use SilverStripe\Core\Manifest\ModuleResource;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Security\Member;
15
use SilverStripe\SiteConfig\SiteConfig;
16
use SilverStripe\VendorPlugin\VendorModule;
17
use SilverStripe\View\Requirements;
18
19
class SilverStripeCollectorTest extends SapphireTest
20
{
21
    /**
22
     * @var DebugBarSilverStripeCollector
23
     */
24
    protected $collector;
25
26
    protected $usesDatabase = true;
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
        DebugBar::initDebugBar();
32
        $this->collector = DebugBar::getDebugBar()->getCollector('silverstripe');
33
    }
34
35
    public function testCollectorExists()
36
    {
37
        $this->assertInstanceOf(SilverStripeCollector::class, $this->collector);
38
    }
39
40
    public function testCollect()
41
    {
42
        $data = $this->collector->collect();
43
44
        $this->assertArrayHasKey('debug', $data);
45
        $this->assertArrayHasKey('locale', $data);
46
        $this->assertArrayHasKey('parameters', $data);
47
        $this->assertArrayHasKey('templates', $data);
48
        $this->assertContains('Framework', $data['version']);
49
        $this->assertSame(SiteConfig::class, $data['config']['ClassName']);
50
        $this->assertSame('User, ADMIN', $data['user']);
51
        $this->assertCount(0, $data['requirements']);
52
53
        Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

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...
54
        $data = $this->collector->collect();
55
        $this->assertSame('Not logged in', $data['user']);
56
    }
57
58
    public function testShowRequirements()
59
    {
60
        Requirements::css('debugbar/assets/debugbar.css');
61
        $data = $this->collector->collect();
62
        $this->assertArrayHasKey('requirements', $data);
63
        $this->assertNotEmpty($data['requirements']);
64
        $this->assertContains('assets/debugbar.css', $data['requirements'][0]);
65
    }
66
67
    public function testShowRequestParameters()
68
    {
69
        $controller = new Controller;
70
        $controller->doInit();
71
        $controller->setRequest(
72
            new HTTPRequest(
73
                'GET',
74
                '/',
75
                array('getvar' => 'value', 'foo' => 'bar'),
76
                array('postvar' => 'value', 'bar' => 'baz')
77
            )
78
        );
79
        $controller->getRequest()->setRouteParams(array('something' => 'here'));
80
81
        $this->collector->setController($controller);
82
        $this->assertSame($controller, $this->collector->getController());
83
84
        $result = SilverStripeCollector::getRequestParameters();
85
        $this->assertSame('value', $result['GET - getvar']);
86
        $this->assertSame('baz', $result['POST - bar']);
87
        $this->assertSame('here', $result['ROUTE - something']);
88
    }
89
90
    public function testGetSessionData()
91
    {
92
        Controller::curr()->getRequest()->getSession()->set('DebugBarTesting', 'test value');
93
        $result = SilverStripeCollector::getSessionData();
94
        $this->assertSame('test value', $result['DebugBarTesting']);
95
    }
96
97
    public function testGetConfigData()
98
    {
99
        $result = SilverStripeCollector::getConfigData();
100
        $this->assertSame(SiteConfig::class, $result['ClassName']);
101
        $this->assertArrayHasKey('Title', $result);
102
        $this->assertArrayHasKey('ID', $result);
103
        $this->assertArrayHasKey('Created', $result);
104
    }
105
106
    public function testGetWidgets()
107
    {
108
        $this->collector->collect();
109
        $result = $this->collector->getWidgets();
110
        // Stub out the dynamic data
111
        $result['version']['tooltip'] = 'Stub';
112
        $result['locale']['tooltip'] = 'Stub';
113
        $result['user']['tooltip'] = 'Current member';
114
115
        $expected = array(
116
            'user' => array(
117
                'icon' => 'user',
118
                'tooltip' => 'Current member',
119
                'default' => '',
120
            ),
121
            'version' => array(
122
                'icon' => 'desktop',
123
                'tooltip' => 'Stub',
124
                'default' => '',
125
            ),
126
            'locale' => array(
127
                'icon' => 'globe',
128
                'tooltip' => 'Stub',
129
                'default' => '',
130
            ),
131
            'session' => array(
132
                'icon' => 'archive',
133
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
134
                'map' => 'silverstripe.session',
135
                'default' => '{}',
136
            ),
137
            'cookies' => array(
138
                'icon' => 'asterisk',
139
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
140
                'map' => 'silverstripe.cookies',
141
                'default' => '{}',
142
            ),
143
            'parameters' => array(
144
                'icon' => 'arrow-right',
145
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
146
                'map' => 'silverstripe.parameters',
147
                'default' => '{}',
148
            ),
149
            'SiteConfig' => array(
150
                'icon' => 'gear',
151
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
152
                'map' => 'silverstripe.config',
153
                'default' => '{}',
154
            ),
155
            'requirements' => array(
156
                'icon' => 'file-o ',
157
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
158
                'map' => 'silverstripe.requirements',
159
                'default' => '{}',
160
            ),
161
            'templates' => array(
162
                'icon' => 'edit',
163
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
164
                'map' => "silverstripe.templates.templates",
165
                'default' => '{}'
166
            ),
167
            'templates:badge' => array(
168
                'map' => 'silverstripe.templates.count',
169
                'default' => 0
170
            )
171
        );
172
173
        $this->assertSame($expected, $result);
174
    }
175
176
    public function testGetAssets()
177
    {
178
        $mockedModule = $this->createConfiguredMock(Module::class,
179
            [
180
                'getName' => 'lekoala/silverstripe-debugbar',
181
                'getPath' => 'resources/lekoala/silverstripe-debugbar',
182
            ]
183
        );
184
        $resource = $this->createConfiguredMock(ModuleResource::class,
0 ignored issues
show
Unused Code introduced by
$resource is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
            [
186
                'getModule' => $mockedModule,
187
                'getRelativePath' => 'javascript',
188
            ]
189
        );
190
191
        $vendorModule = $this->createConfiguredMock(VendorModule::class,
0 ignored issues
show
Unused Code introduced by
$vendorModule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
192
            [
193
                'getPath' => 'lekoala/silverstripe-debugbar',
194
                'getName' => 'lekoala/silverstripe-debugbar',
195
            ]
196
        );
197
        $expected = array(
198
            'base_path' => '/resources/lekoala/silverstripe-debugbar/javascript',
199
            'base_url' => 'resources/lekoala/silverstripe-debugbar/javascript',
200
            'css' => array(),
201
            'js' => 'widgets.js',
202
        );
203
        $this->assertSame($expected, $this->collector->getAssets());
204
    }
205
}
206