Completed
Pull Request — master (#52)
by Alexander
03:31
created

ContentDecoratorTest::testShouldTriggerInitEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 36
rs 9.536
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\ContentDecorator;
8
use Yiisoft\Widget\Event\BeforeRun;
9
10
/**
11
 * ContentDecoratorTest.
12
 */
13
class ContentDecoratorTest extends TestCase
14
{
15
    /**
16
     * @see https://github.com/yiisoft/yii2/issues/15536
17
     */
18
    public function testShouldTriggerInitEvent(): void
19
    {
20
        $initTriggered = false;
21
22
        // adding some listeners
23
        $this->listenerProvider->attach(static function (BeforeRun $event) use (&$initTriggered) {
0 ignored issues
show
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

23
        $this->listenerProvider->/** @scrutinizer ignore-call */ 
24
                                 attach(static function (BeforeRun $event) use (&$initTriggered) {
Loading history...
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

23
        $this->listenerProvider->attach(static 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...
24
            $initTriggered = true;
25
        });
26
27
        ob_start();
28
        ob_implicit_flush(0);
29
30
        ContentDecorator::begin()
31
            ->viewFile($this->aliases->get('@view/layout.php'))
0 ignored issues
show
Bug introduced by
It seems like $this->aliases->get('@view/layout.php') can also be of type boolean; however, parameter $value of Yiisoft\Widget\ContentDecorator::viewFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
            ->viewFile(/** @scrutinizer ignore-type */ $this->aliases->get('@view/layout.php'))
Loading history...
32
            ->params([])
33
            ->init();
34
35
        echo "\t\t<div class='left-column'>\n";
36
        echo "\t\t\t<p>This is a left bar!</p>\n";
37
        echo "\t\t</div>\n\n";
38
        echo "\t\t<div class='right-column'>\n";
39
        echo "\t\t\t<p>This is a right bar!</p>\n";
40
        echo "\t\t</div>\n";
41
42
        ContentDecorator::end();
43
44
        $this->assertTrue($initTriggered);
45
46
        $expected = "\t\t<div class='left-column'>\n" .
47
                    "\t\t\t<p>This is a left bar!</p>\n" .
48
                    "\t\t</div>\n\n" .
49
                    "\t\t<div class='right-column'>\n" .
50
                    "\t\t\t<p>This is a right bar!</p>\n" .
51
                    "\t\t</div>\n";
52
53
        $this->assertStringContainsString($expected, ob_get_clean());
54
    }
55
}
56