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() |
|
|
|
|
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
|
|
|
|
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.