Passed
Push — master ( 0074b5...431277 )
by Alexander
02:14
created

WidgetTest::testWidgetArrayConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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();
0 ignored issues
show
Bug introduced by
The method id() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of said class. However, the method does not exist in Yiisoft\Widget\Tests\Stubs\TestInjectionWidget. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        $output = TestWidget::widget()->/** @scrutinizer ignore-call */ id('w0')->run();
Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
        $this->listenerProvider->attach(static function (/** @scrutinizer ignore-unused */ BeforeRun $event) use (&$triggered) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
The method attach() does not exist on Psr\EventDispatcher\ListenerProviderInterface. It seems like you code against a sub-type of Psr\EventDispatcher\ListenerProviderInterface such as Yiisoft\EventDispatcher\Provider\Aggregate or Yiisoft\EventDispatcher\Provider\Provider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $this->listenerProvider->/** @scrutinizer ignore-call */ 
69
                                 attach(static function (BeforeRun $event) use (&$triggered) {
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getInjectable() does not exist on Yiisoft\Widget\Widget. It seems like you code against a sub-type of Yiisoft\Widget\Widget such as Yiisoft\Widget\Tests\Stubs\TestInjectionWidget. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $this->assertInstanceOf(Injectable::class, $widget->/** @scrutinizer ignore-call */ getInjectable());
Loading history...
97
    }
98
}
99