Completed
Pull Request — master (#52)
by Wilmer
01:36
created

BlockTest::testShouldTriggerInitEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.9666
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Widget\Tests;
5
6
use Yiisoft\Tests\TestCase;
7
use Yiisoft\Widget\Block;
8
use Yiisoft\Widget\Event\BeforeRun;
9
10
/**
11
 * @group widgets
12
 */
13
class BlockTest extends TestCase
14
{
15
    public function testBlock(): void
16
    {
17
        Block::begin()
18
            ->id('testme')
19
            ->init();
20
21
        echo '<block-testme>';
22
23
        Block::end();
24
25
        $this->assertStringContainsString('<block-testme>', $this->webView->getBlock('testme'));
26
    }
27
28
    public function testBlockRenderInPlaceTrue(): void
29
    {
30
        ob_start();
31
        ob_implicit_flush(0);
32
33
        Block::begin()
34
            ->id('testme')
35
            ->renderInPlace(true)
36
            ->init();
37
38
        echo '<block-testme>';
39
40
        Block::end();
41
42
        $this->assertStringContainsString('<block-testme>', ob_get_clean());
43
    }
44
45
    public function testGetBlockException(): void
46
    {
47
        $this->expectException(\InvalidArgumentException::class);
48
        $this->webView->getBlock('notfound');
49
    }
50
51
    /**
52
     * @see https://github.com/yiisoft/yii2/issues/15536
53
     */
54
    public function testShouldTriggerInitEvent(): void
55
    {
56
        $initTriggered = false;
57
58
        // adding some listeners
59
        $this->listenerProvider->attach(function (BeforeRun $event) use (&$initTriggered) {
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

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

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...
60
            $initTriggered = true;
61
        });
62
63
        ob_start();
64
        ob_implicit_flush(0);
65
66
        Block::begin()->init();
67
        Block::end();
68
69
        ob_get_clean();
70
71
        $this->assertTrue($initTriggered);
72
    }
73
}
74