Completed
Pull Request — master (#78)
by Franco
01:49
created

SilverStripeCollectorTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 178
Duplicated Lines 5.62 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 10
loc 178
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testCollectorExists() 0 4 1
A testCollect() 0 17 1
A testShowRequirements() 0 8 1
A testShowRequestParameters() 0 22 1
A testGetSessionData() 0 6 1
A testGetConfigData() 0 8 1
B testGetWidgets() 0 79 1
A testGetAssets() 10 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 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\Dev\SapphireTest;
11
use SilverStripe\Security\Member;
12
use SilverStripe\SiteConfig\SiteConfig;
13
use SilverStripe\View\Requirements;
14
15
class SilverStripeCollectorTest extends SapphireTest
16
{
17
    /**
18
     * @var DebugBarSilverStripeCollector
19
     */
20
    protected $collector;
21
22
    protected $usesDatabase = true;
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        DebugBar::initDebugBar();
28
        $this->collector = DebugBar::getDebugBar()->getCollector('silverstripe');
29
    }
30
31
    public function testCollectorExists()
32
    {
33
        $this->assertInstanceOf(SilverStripeCollector::class, $this->collector);
34
    }
35
36
    public function testCollect()
37
    {
38
        $data = $this->collector->collect();
39
40
        $this->assertArrayHasKey('debug', $data);
41
        $this->assertArrayHasKey('locale', $data);
42
        $this->assertArrayHasKey('parameters', $data);
43
        $this->assertArrayHasKey('templates', $data);
44
        $this->assertContains('Framework', $data['version']);
45
        $this->assertSame(SiteConfig::class, $data['config']['ClassName']);
46
        $this->assertSame('User, ADMIN', $data['user']);
47
        $this->assertCount(0, $data['requirements']);
48
49
        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...
50
        $data = $this->collector->collect();
51
        $this->assertSame('Not logged in', $data['user']);
52
    }
53
54
    public function testShowRequirements()
55
    {
56
        Requirements::css('debugbar/assets/debugbar.css');
57
        $data = $this->collector->collect();
58
        $this->assertArrayHasKey('requirements', $data);
59
        $this->assertNotEmpty($data['requirements']);
60
        $this->assertContains('assets/debugbar.css', $data['requirements'][0]);
61
    }
62
63
    public function testShowRequestParameters()
64
    {
65
        $controller = new Controller;
66
        $controller->doInit();
67
        $controller->setRequest(
68
            new HTTPRequest(
69
                'GET',
70
                '/',
71
                array('getvar' => 'value', 'foo' => 'bar'),
72
                array('postvar' => 'value', 'bar' => 'baz')
73
            )
74
        );
75
        $controller->getRequest()->setRouteParams(array('something' => 'here'));
76
77
        $this->collector->setController($controller);
78
        $this->assertSame($controller, $this->collector->getController());
79
80
        $result = SilverStripeCollector::getRequestParameters();
81
        $this->assertSame('value', $result['GET - getvar']);
82
        $this->assertSame('baz', $result['POST - bar']);
83
        $this->assertSame('here', $result['ROUTE - something']);
84
    }
85
86
    public function testGetSessionData()
87
    {
88
        Controller::curr()->getRequest()->getSession()->set('DebugBarTesting', 'test value');
89
        $result = SilverStripeCollector::getSessionData();
90
        $this->assertSame('test value', $result['DebugBarTesting']);
91
    }
92
93
    public function testGetConfigData()
94
    {
95
        $result = SilverStripeCollector::getConfigData();
96
        $this->assertSame(SiteConfig::class, $result['ClassName']);
97
        $this->assertArrayHasKey('Title', $result);
98
        $this->assertArrayHasKey('ID', $result);
99
        $this->assertArrayHasKey('Created', $result);
100
    }
101
102
    public function testGetWidgets()
103
    {
104
        $this->collector->collect();
105
        $result = $this->collector->getWidgets();
106
        // Stub out the dynamic data
107
        $result['version']['tooltip'] = 'Stub';
108
        $result['locale']['tooltip'] = 'Stub';
109
        $result['user']['tooltip'] = 'Current member';
110
111
        $expected = array(
112
            'user' => array(
113
                'icon' => 'user',
114
                'tooltip' => 'Current member',
115
                'default' => '',
116
            ),
117
            'version' => array(
118
                'icon' => 'desktop',
119
                'tooltip' => 'Stub',
120
                'default' => '',
121
            ),
122
            'locale' => array(
123
                'icon' => 'globe',
124
                'tooltip' => 'Stub',
125
                'default' => '',
126
            ),
127
            'session' => array(
128
                'icon' => 'archive',
129
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
130
                'map' => 'silverstripe.session',
131
                'default' => '{}',
132
            ),
133
            'cookies' => array(
134
                'icon' => 'asterisk',
135
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
136
                'map' => 'silverstripe.cookies',
137
                'default' => '{}',
138
            ),
139
            'parameters' => array(
140
                'icon' => 'arrow-right',
141
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
142
                'map' => 'silverstripe.parameters',
143
                'default' => '{}',
144
            ),
145
            'SiteConfig' => array(
146
                'icon' => 'gear',
147
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
148
                'map' => 'silverstripe.config',
149
                'default' => '{}',
150
            ),
151
            'requirements' => array(
152
                'icon' => 'file-o ',
153
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
154
                'map' => 'silverstripe.requirements',
155
                'default' => '{}',
156
            ),
157
            'templates' => array(
158
                'icon' => 'edit',
159
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
160
                'map' => "silverstripe.templates.templates",
161
                'default' => '{}'
162
            ),
163
            'templates:badge' => array(
164
                'map' => 'silverstripe.templates.count',
165
                'default' => 0
166
            ),
167
            'templateCache' => array(
168
                'icon' => 'asterisk',
169
                'widget' => 'PhpDebugBar.Widgets.ListWidget',
170
                'map' => "silverstripe.templateCache.cache",
171
                'default' => '{}'
172
            ),
173
            'templateCache:badge' => array(
174
                'map' => 'silverstripe.templateCache.count',
175
                'default' => 0
176
            )
177
        );
178
179
        $this->assertSame($expected, $result);
180
    }
181
182 View Code Duplication
    public function testGetAssets()
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...
183
    {
184
        $expected = array(
185
            'base_path' => '/debugbar/javascript',
186
            'base_url' => 'debugbar/javascript',
187
            'css' => array(),
188
            'js' => 'widgets.js',
189
        );
190
        $this->assertSame($expected, $this->collector->getAssets());
191
    }
192
}
193