Passed
Pull Request — master (#77)
by
unknown
02:08
created

WebViewTest::testViewFileNullEndPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Tests;
6
7
use Yiisoft\Files\FileHelper;
8
use Yiisoft\View\WebView;
9
10
final class WebViewTest extends \Yiisoft\View\Tests\TestCase
11
{
12
    private string $dataDir;
13
    private string $layoutPath;
14
15
    /**
16
     * @var string path for the test files.
17
     */
18
    private string $testViewPath = '';
19
20
    protected function setUp(): void
21
    {
22
        parent::setUp();
23
24
        $this->dataDir = dirname(__DIR__) . '/tests/public/view';
25
        $this->layoutPath = $this->dataDir . '/layout.php';
26
        $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false);
27
28
        FileHelper::createDirectory($this->testViewPath);
29
    }
30
31
    protected function tearDown(): void
32
    {
33
        parent::tearDown();
34
        FileHelper::removeDirectory($this->testViewPath);
35
    }
36
37
    public function testViewFileNullBeginBody(): void
38
    {
39
        $this->expectException(\LogicException::class);
40
        $this->webView->beginBody();
41
    }
42
43
    public function testViewFileNullEndBody(): void
44
    {
45
        $this->expectException(\LogicException::class);
46
        $this->webView->endBody();
47
    }
48
49
    public function testViewFileNullEndPage(): void
50
    {
51
        $this->expectException(\LogicException::class);
52
        $this->webView->endPage();
53
    }
54
55
    public function testRegisterJsVar(): void
56
    {
57
        $this->webView->registerJsVar('username', 'samdark');
58
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
59
        $this-> assertStringContainsString('<script>var username = "samdark";</script></head>', $html);
60
61
        $this->webView->registerJsVar('objectTest', [
62
            'number' => 42,
63
            'question' => 'Unknown',
64
        ]);
65
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
66
        $this->assertStringContainsString('<script>var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html);
67
    }
68
69
    public function testRegisterJsFileWithAlias(): void
70
    {
71
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]);
0 ignored issues
show
Bug introduced by
It seems like $this->aliases->get('@web/js/somefile.js') can also be of type boolean; however, parameter $url of Yiisoft\View\WebView::registerJsFile() 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

71
        $this->webView->registerJsFile(/** @scrutinizer ignore-type */ $this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]);
Loading history...
72
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
73
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></head>', $html);
74
75
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_BEGIN]);
76
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
77
        $this->assertStringContainsString('<body>' . PHP_EOL . '<script src="/baseUrl/js/somefile.js"></script>', $html);
78
79
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_END]);
80
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
81
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></body>', $html);
82
    }
83
84
    public function testRegisterCssFileWithAlias(): void
85
    {
86
        $this->webView->registerCssFile($this->aliases->get('@web/css/somefile.css'));
0 ignored issues
show
Bug introduced by
It seems like $this->aliases->get('@web/css/somefile.css') can also be of type boolean; however, parameter $url of Yiisoft\View\WebView::registerCssFile() 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

86
        $this->webView->registerCssFile(/** @scrutinizer ignore-type */ $this->aliases->get('@web/css/somefile.css'));
Loading history...
87
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
88
        $this->assertStringContainsString('<link href="/baseUrl/css/somefile.css" rel="stylesheet"></head>', $html);
89
    }
90
}
91