1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\DebugBar\Test\Extension; |
4
|
|
|
|
5
|
|
|
use DebugBar\DataCollector\TimeDataCollector; |
6
|
|
|
use LeKoala\DebugBar\DebugBar; |
7
|
|
|
use LeKoala\DebugBar\Extension\ControllerExtension; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Dev\FunctionalTest; |
11
|
|
|
|
12
|
|
|
class ControllerExtensionTest extends FunctionalTest |
13
|
|
|
{ |
14
|
|
|
protected static $required_extensions = [ |
15
|
|
|
Controller::class => [ControllerExtension::class] |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Controller |
20
|
|
|
*/ |
21
|
|
|
protected $controller; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
$this->controller = Controller::curr(); |
28
|
|
|
// $this->controller->pushCurrent(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
DebugBar::$bufferingEnabled = false; |
31
|
|
|
DebugBar::initDebugBar(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testOnBeforeInit() |
35
|
|
|
{ |
36
|
|
|
$this->controller->extend('onBeforeInit'); |
37
|
|
|
|
38
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
39
|
|
|
$controller = $debugbar->getCollector('silverstripe')->getController(); |
|
|
|
|
40
|
|
|
$this->assertInstanceOf(Controller::class, $controller); |
41
|
|
|
$this->assertSame(Controller::curr(), $controller); |
42
|
|
|
|
43
|
|
|
$timeData = $debugbar->getCollector('time'); |
44
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
45
|
|
|
|
46
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('pre_request')); |
47
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('init')); |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testOnAfterInit() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->controller->extend('onAfterInit'); |
54
|
|
|
|
55
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
56
|
|
|
$timeData = $debugbar->getCollector('time'); |
57
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
58
|
|
|
|
59
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('cms_init')); |
60
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('init')); |
61
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('handle')); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testBeforeCallActionHandler() |
66
|
|
|
{ |
67
|
|
|
$request = new HTTPRequest('GET', '/'); |
68
|
|
|
$action = 'someaction'; |
69
|
|
|
$this->controller->extend('beforeCallActionHandler', $request, $action); |
70
|
|
|
|
71
|
|
View Code Duplication |
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
|
|
|
|
72
|
|
|
$timeData = $debugbar->getCollector('time'); |
73
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
74
|
|
|
|
75
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('handle')); |
76
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('action')); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($this->controller->beforeCallActionHandlerCalled); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testAfterCallActionHandler() |
83
|
|
|
{ |
84
|
|
|
$request = new HTTPRequest('GET', '/'); |
85
|
|
|
$action = 'someaction'; |
86
|
|
|
$result = null; |
87
|
|
|
$this->controller->extend('afterCallActionHandler', $request, $action, $result); |
88
|
|
|
|
89
|
|
View Code Duplication |
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
|
|
|
|
90
|
|
|
$timeData = $debugbar->getCollector('time'); |
91
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
92
|
|
|
|
93
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('action')); |
94
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('after_action')); |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.