Passed
Push — master ( 099a64...e5dda6 )
by Alexander
03:36
created

WebViewTest::getCSRFTokenValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 3
c 2
b 0
f 1
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
namespace Yiisoft\View\Tests;
3
4
use Yiisoft\Files\FileHelper;
5
use Yiisoft\Tests\TestCase;
6
use Yiisoft\View\WebView;
7
8
final class WebViewTest extends TestCase
9
{
10
    private string $dataDir;
11
    private string $layoutPath;
12
13
    /**
14
     * @var string path for the test files.
15
     */
16
    private string $testViewPath = '';
17
18
    protected function setUp(): void
19
    {
20
        parent::setUp();
21
        $this->dataDir = dirname(__DIR__) . '/public/view';
22
        $this->layoutPath = $this->dataDir . '/layout.php';
23
        $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false);
24
25
        FileHelper::createDirectory($this->testViewPath);
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
        FileHelper::removeDirectory($this->testViewPath);
32
    }
33
34
    public function testRegisterJsVar(): void
35
    {
36
        $this->webView->registerJsVar('username', 'samdark');
37
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
38
        $this-> assertStringContainsString('<script>var username = "samdark";</script></head>', $html);
39
40
        $this->webView->registerJsVar('objectTest', [
41
            'number' => 42,
42
            'question' => 'Unknown',
43
        ]);
44
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
45
        $this->assertStringContainsString('<script>var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html);
46
    }
47
48
    public function testRegisterJsFileWithAlias(): void
49
    {
50
        $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

50
        $this->webView->registerJsFile(/** @scrutinizer ignore-type */ $this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]);
Loading history...
51
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
52
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></head>', $html);
53
54
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_BEGIN]);
55
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
56
        $this->assertStringContainsString('<body>' . PHP_EOL . '<script src="/baseUrl/js/somefile.js"></script>', $html);
57
58
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_END]);
59
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
60
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></body>', $html);
61
    }
62
63
    public function testRegisterCssFileWithAlias(): void
64
    {
65
        $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

65
        $this->webView->registerCssFile(/** @scrutinizer ignore-type */ $this->aliases->get('@web/css/somefile.css'));
Loading history...
66
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
67
        $this->assertStringContainsString('<link href="/baseUrl/css/somefile.css" rel="stylesheet"></head>', $html);
68
    }
69
}
70