Passed
Pull Request — master (#83)
by Wilmer
28:01 queued 13:07
created

WebViewTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 36
c 1
b 0
f 0
dl 0
loc 70
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Tests;
6
7
use Yiisoft\Files\FileHelper;
8
use Yiisoft\View\Tests\Mocks\WebViewPlaceholderMock;
9
use Yiisoft\View\WebView;
10
11
final class WebViewTest extends \Yiisoft\View\Tests\TestCase
12
{
13
    private string $dataDir;
14
    private string $layoutPath;
15
16
    /**
17
     * @var string path for the test files.
18
     */
19
    private string $testViewPath = '';
20
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $this->dataDir = dirname(__DIR__) . '/tests/public/view';
26
        $this->layoutPath = $this->dataDir . '/layout.php';
27
        $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false);
28
29
        FileHelper::createDirectory($this->testViewPath);
30
    }
31
32
    protected function tearDown(): void
33
    {
34
        parent::tearDown();
35
        FileHelper::removeDirectory($this->testViewPath);
36
    }
37
38
    public function testRegisterJsVar(): void
39
    {
40
        $this->webView->registerJsVar('username', 'samdark');
41
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
42
        $this-> assertStringContainsString('<script>var username = "samdark";</script></head>', $html);
43
44
        $this->webView->registerJsVar('objectTest', [
45
            'number' => 42,
46
            'question' => 'Unknown',
47
        ]);
48
        $html = $this->webView->render('//layout.php', ['content' => 'content']);
49
        $this->assertStringContainsString('<script>var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html);
50
    }
51
52
    public function testRegisterJsFileWithAlias(): void
53
    {
54
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]);
55
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
56
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></head>', $html);
57
58
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_BEGIN]);
59
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
60
        $this->assertStringContainsString('<body>' . PHP_EOL . '<script src="/baseUrl/js/somefile.js"></script>', $html);
61
62
        $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_END]);
63
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
64
        $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></body>', $html);
65
    }
66
67
    public function testRegisterCssFileWithAlias(): void
68
    {
69
        $this->webView->registerCssFile($this->aliases->get('@web/css/somefile.css'));
70
        $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']);
71
        $this->assertStringContainsString('<link href="/baseUrl/css/somefile.css" rel="stylesheet"></head>', $html);
72
    }
73
74
    public function testPlaceholders(): void
75
    {
76
        $webView = $this->getContainer()->get(WebViewPlaceholderMock::class);
77
        $webView->setPlaceholderSalt('apple');
78
        $signature = $webView->getPlaceholderSignature();
79
        $html = $webView->renderFile($this->layoutPath, ['content' => 'content']);
80
        $this->assertStringContainsString($signature, $html);
81
    }
82
}
83