Passed
Pull Request — master (#77)
by
unknown
01:40
created

WebViewTest::testViewFileNullEndPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 2
b 1
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->expectExceptionMessage('Need to call beginPage() before endPage().');
53
        $this->webView->endPage();
54
    }
55
56
    public function testRegisterJsVar(): void
57
    {
58
        $this->webView->registerJsVar('username', 'samdark');
59
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
60
        $this-> assertStringContainsString('<script>var username = "samdark";</script></head>', $html);
61
62
        $this->webView->registerJsVar('objectTest', [
63
            'number' => 42,
64
            'question' => 'Unknown',
65
        ]);
66
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
67
        $this->assertStringContainsString('<script>var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html);
68
    }
69
70
    public function testRegisterJsFileWithAlias(): void
71
    {
72
        $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

72
        $this->webView->registerJsFile(/** @scrutinizer ignore-type */ $this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]);
Loading history...
73
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
74
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></head>', $html);
75
76
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_BEGIN]);
77
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
78
        $this->assertStringContainsString('<body>' . PHP_EOL . '<script src="/baseUrl/js/somefile.js"></script>', $html);
79
80
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_END]);
81
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
82
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></body>', $html);
83
    }
84
85
    public function testRegisterCssFileWithAlias(): void
86
    {
87
        $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

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