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

SilverStripeCollectorTest::testGetWidgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

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