1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Widget\Tests; |
5
|
|
|
|
6
|
|
|
use Yiisoft\Widget\Event\AfterRun; |
7
|
|
|
use Yiisoft\Widget\Event\BeforeRun; |
8
|
|
|
use Yiisoft\Widget\Tests\Stubs\Injectable; |
9
|
|
|
use Yiisoft\Widget\Tests\Stubs\TestInjectionWidget; |
10
|
|
|
use Yiisoft\Widget\Tests\Stubs\TestWidget; |
11
|
|
|
use Yiisoft\Widget\Tests\Stubs\TestWidgetA; |
12
|
|
|
use Yiisoft\Widget\Tests\Stubs\TestWidgetB; |
13
|
|
|
use Yiisoft\Widget\Widget; |
14
|
|
|
use Yiisoft\Widget\Exception\InvalidConfigException; |
15
|
|
|
|
16
|
|
|
final class WidgetTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testWidget(): void |
19
|
|
|
{ |
20
|
|
|
$output = TestWidget::widget()->id('w0')->run(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$this->assertSame('<run-w0>', $output); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testWidgetArrayConfig(): void |
26
|
|
|
{ |
27
|
|
|
$output = TestWidget::widget(['id()' => ['w0']])->run(); |
28
|
|
|
|
29
|
|
|
$this->assertSame('<run-w0>', $output); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testBeginEnd(): void |
33
|
|
|
{ |
34
|
|
|
$widget = TestWidgetA::begin()->id('test'); |
35
|
|
|
|
36
|
|
|
$this->assertInstanceOf(Widget::class, $widget); |
37
|
|
|
|
38
|
|
|
$output = TestWidgetA::end(); |
39
|
|
|
|
40
|
|
|
$this->assertSame('<run-test>', $output); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @depends testBeginEnd |
45
|
|
|
*/ |
46
|
|
|
public function testStackTracking(): void |
47
|
|
|
{ |
48
|
|
|
$this->expectException(InvalidConfigException::class); |
49
|
|
|
TestWidget::end(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @depends testBeginEnd |
54
|
|
|
*/ |
55
|
|
|
public function testStackTrackingDisorder(): void |
56
|
|
|
{ |
57
|
|
|
$this->expectException(InvalidConfigException::class); |
58
|
|
|
TestWidgetA::begin(); |
59
|
|
|
TestWidgetB::begin(); |
60
|
|
|
TestWidgetA::end(); |
61
|
|
|
TestWidgetB::end(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testShouldTriggerBeforeRun(): void |
65
|
|
|
{ |
66
|
|
|
$triggered = false; |
67
|
|
|
|
68
|
|
|
$this->listenerProvider->attach(static function (BeforeRun $event) use (&$triggered) { |
|
|
|
|
69
|
|
|
$triggered = true; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
TestWidgetA::begin()->id('test'); |
73
|
|
|
$output = TestWidgetA::end(); |
74
|
|
|
|
75
|
|
|
$this->assertTrue($triggered); |
76
|
|
|
$this->assertEquals('<run-test>', $output); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testShouldTriggerAfterRun(): void |
80
|
|
|
{ |
81
|
|
|
$output = null; |
82
|
|
|
|
83
|
|
|
$this->listenerProvider->attach(static function (AfterRun $event) use (&$output) { |
84
|
|
|
$output = $event->getResult(); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
TestWidgetA::begin()->id('test'); |
88
|
|
|
TestWidgetA::end(); |
89
|
|
|
|
90
|
|
|
$this->assertSame('<run-test>', $output); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testInjection(): void |
94
|
|
|
{ |
95
|
|
|
$widget = TestInjectionWidget::widget(); |
96
|
|
|
$this->assertInstanceOf(Injectable::class, $widget->getInjectable()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|