Completed
Pull Request — master (#52)
by Wilmer
01:31
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
3
namespace Yiisoft\Widget\Tests;
4
5
use Yiisoft\Tests\TestCase;
6
use Yiisoft\Widget\Block;
7
use Yiisoft\Widget\Event\BeforeRun;
8
9
/**
10
 * @group widgets
11
 */
12
class BlockTest extends TestCase
13
{
14
    public function testBlock()
15
    {
16
        Block::begin()
17
            ->id('testme')
18
            ->init();
19
20
        echo '<block-testme>';
21
22
        Block::end();
23
24
        $this->assertStringContainsString('<block-testme>', $this->webView->getBlock('testme'));
25
    }
26
27
    public function testBlockRenderInPlaceTrue()
28
    {
29
        ob_start();
30
        ob_implicit_flush(0);
31
32
        Block::begin()
33
            ->id('testme')
34
            ->renderInPlace(true)
35
            ->init();
36
37
        echo '<block-testme>';
38
39
        Block::end();
40
41
        $this->assertStringContainsString('<block-testme>', ob_get_clean());
42
    }
43
44
    public function testGetBlockException()
45
    {
46
        $this->expectException(\InvalidArgumentException::class);
47
        $this->webView->getBlock('notfound');
48
    }
49
50
    /**
51
     * @see https://github.com/yiisoft/yii2/issues/15536
52
     */
53
    public function testShouldTriggerInitEvent()
54
    {
55
        $initTriggered = false;
56
57
        // adding some listeners
58
        $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

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