1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\DebugBar\Test\Extension; |
4
|
|
|
|
5
|
|
|
use LeKoala\DebugBar\DebugBar; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Dev\FunctionalTest; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use DebugBar\DataCollector\TimeDataCollector; |
10
|
|
|
use LeKoala\DebugBar\Extension\ControllerExtension; |
11
|
|
|
use LeKoala\DebugBar\Collector\SilverStripeCollector; |
12
|
|
|
|
13
|
|
|
class ControllerExtensionTest extends FunctionalTest |
14
|
|
|
{ |
15
|
|
|
protected static $required_extensions = [ |
16
|
|
|
Controller::class => [ControllerExtension::class] |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Controller |
21
|
|
|
*/ |
22
|
|
|
protected $controller; |
23
|
|
|
|
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->controller = Controller::curr(); |
29
|
|
|
// $this->controller->pushCurrent(); |
30
|
|
|
|
31
|
|
|
DebugBar::$bufferingEnabled = false; |
32
|
|
|
DebugBar::initDebugBar(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testOnBeforeInit() |
36
|
|
|
{ |
37
|
|
|
$this->controller->extend('onBeforeInit'); |
38
|
|
|
|
39
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
40
|
|
|
/** @var SilverStripeCollector $ssCollector */ |
41
|
|
|
$ssCollector = $debugbar->getCollector('silverstripe'); |
42
|
|
|
$controller = $ssCollector->getController(); |
43
|
|
|
$this->assertInstanceOf(Controller::class, $controller); |
44
|
|
|
$this->assertSame(Controller::curr(), $controller); |
45
|
|
|
|
46
|
|
|
/** @var TimeDataCollector $timeData */ |
47
|
|
|
$timeData = $debugbar->getCollector('time'); |
48
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
49
|
|
|
|
50
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('pre_request')); |
51
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('init')); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testOnAfterInit() |
56
|
|
|
{ |
57
|
|
|
$this->controller->extend('onAfterInit'); |
58
|
|
|
|
59
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
60
|
|
|
/** @var TimeDataCollector $timeData */ |
61
|
|
|
$timeData = $debugbar->getCollector('time'); |
62
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
63
|
|
|
|
64
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('cms_init')); |
65
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('init')); |
66
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('handle')); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testBeforeCallActionHandler() |
71
|
|
|
{ |
72
|
|
|
$request = new HTTPRequest('GET', '/'); |
73
|
|
|
$action = 'someaction'; |
74
|
|
|
$this->controller->extend('beforeCallActionHandler', $request, $action); |
75
|
|
|
|
76
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
77
|
|
|
/** @var TimeDataCollector $timeData */ |
78
|
|
|
$timeData = $debugbar->getCollector('time'); |
79
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
80
|
|
|
|
81
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('handle')); |
82
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('action')); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testAfterCallActionHandler() |
87
|
|
|
{ |
88
|
|
|
$request = new HTTPRequest('GET', '/'); |
89
|
|
|
$action = 'someaction'; |
90
|
|
|
$result = null; |
91
|
|
|
$this->controller->extend('afterCallActionHandler', $request, $action, $result); |
92
|
|
|
|
93
|
|
|
DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
94
|
|
|
/** @var TimeDataCollector $timeData */ |
95
|
|
|
$timeData = $debugbar->getCollector('time'); |
96
|
|
|
$this->assertInstanceOf(TimeDataCollector::class, $timeData); |
97
|
|
|
|
98
|
|
|
$this->assertFalse($timeData->hasStartedMeasure('action')); |
99
|
|
|
$this->assertTrue($timeData->hasStartedMeasure('after_action')); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|