Completed
Push — master ( 1df032...2ea341 )
by Robbie
11s queued 11s
created

DatabaseCollectorTest::testCollectorExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace LeKoala\DebugBar\Test\Collector;
4
5
use LeKoala\DebugBar\Collector\DatabaseCollector;
6
use LeKoala\DebugBar\DebugBar;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
10
class DatabaseCollectorTest extends SapphireTest
11
{
12
    /**
13
     * @var DebugBarSilverStripeCollector
14
     */
15
    protected $collector;
16
17
    protected $usesDatabase = true;
18
19
    public function setUp()
20
    {
21
        parent::setUp();
22
23
        DebugBar::initDebugBar();
24
        $this->collector = DebugBar::getDebugBar()->getCollector('db');
25
    }
26
27
    public function testCollectorExists()
28
    {
29
        $this->assertInstanceOf(DatabaseCollector::class, $this->collector);
30
    }
31
32
    public function testCollect()
33
    {
34
        // Deliberately high warning threshold
35
        Config::modify()->set(DebugBar::class, 'warn_dbqueries_threshold_seconds', 200);
36
        $result = $this->collector->collect();
37
38
        $this->assertGreaterThan(1, $result['nb_statements']);
39
        $this->assertEquals(0, $result['nb_failed_statements']);
40
        $this->assertCount($result['nb_statements'], $result['statements']);
41
42
        $statement = array_shift($result['statements']);
43
        $this->assertNotEmpty($statement['sql']);
44
        $this->assertEquals(1, $statement['is_success']);
45
        $this->assertContains('SilverStripe\Dev\SapphireTest', $statement['source']);
46
        $this->assertFalse($statement['warn']);
47
48
        // Deliberately low warning threshold
49
        Config::modify()->set(DebugBar::class, 'warn_dbqueries_threshold_seconds', 0.0000001);
50
        $result = $this->collector->collect();
51
52
        $this->assertNotEmpty($result['statements']);
53
        $statement = array_shift($result['statements']);
54
        $this->assertTrue($statement['warn']);
55
    }
56
57
    public function testGetWidgets()
58
    {
59
        $expected = array(
60
            'database' => array(
61
                'icon' => 'inbox',
62
                'widget' => 'PhpDebugBar.Widgets.SQLQueriesWidget',
63
                'map' => 'db',
64
                'default' => '[]'
65
            ),
66
            'database:badge' => array(
67
                'map' => 'db.nb_statements',
68
                'default' => 0
69
            )
70
        );
71
72
        $this->assertSame($expected, $this->collector->getWidgets());
73
    }
74
75 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...
76
    {
77
        $expected = array(
78
            'base_path' => '/debugbar/javascript',
79
            'base_url' => 'debugbar/javascript',
80
            'css' => 'sqlqueries/widget.css',
81
            'js' => 'sqlqueries/widget.js'
82
        );
83
84
        $this->assertSame($expected, $this->collector->getAssets());
85
    }
86
}
87