ViewRendererTest.php 3.82 KB
Newer Older
1 2 3 4 5 6
<?php
namespace yiiunit\extensions\twig;

use yii\web\AssetManager;
use yii\web\View;
use Yii;
7
use yiiunit\data\base\Singer;
8 9 10
use yiiunit\TestCase;

/**
11 12 13 14
 * Tests Twig view renderer
 *
 * @author Alexander Makarov <sam@rmcreative.ru>
 * @author Carsten Brandt <mail@cebe.cc>
15 16 17
 */
class ViewRendererTest extends TestCase
{
18 19 20 21 22 23 24 25 26 27 28 29
    protected function setUp()
    {
        $this->mockApplication();
    }

    /**
     * https://github.com/yiisoft/yii2/issues/1755
     */
    public function testLayoutAssets()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/layout.twig');
30

31 32 33 34 35 36 37 38 39
        $this->assertEquals(1, preg_match('#<script src="/assets/[0-9a-z]+/jquery\\.js"></script>\s*</body>#', $content), 'Content does not contain the jquery js:' . $content);
    }

    public function testAppGlobal()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/layout.twig');

        $this->assertEquals(1, preg_match('#<meta charset="' . Yii::$app->charset . '"/>#', $content), 'Content does not contain charset:' . $content);
40
    }
41

42 43 44 45 46 47
    /**
     * https://github.com/yiisoft/yii2/issues/3877
     */
    public function testLexerOptions()
    {
        $view = $this->mockView();
48 49 50 51 52 53 54 55 56 57 58 59 60
        $content = $view->renderFile('@yiiunit/extensions/twig/views/comments.twig');

        $this->assertFalse(strpos($content, 'CUSTOM_LEXER_TWIG_COMMENT'), 'Custom comment lexerOptions were not applied: ' . $content);
        $this->assertTrue(strpos($content, 'DEFAULT_TWIG_COMMENT') !== false, 'Default comment style was not modified via lexerOptions:' . $content);
    }

    public function testForm()
    {
        $view = $this->mockView();
        $model = new Singer();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/form.twig', ['model' => $model]);
        $this->assertEquals(1, preg_match('#<form id="login-form" class="form-horizontal" action="/form-handler" method="post">.*?</form>#s', $content), 'Content does not contain form:' . $content);
    }
61

62 63 64 65 66 67 68 69
    public function testCalls()
    {
        $view = $this->mockView();
        $model = new Singer();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/calls.twig', ['model' => $model]);
        $this->assertFalse(strpos($content, 'silence'), 'silence should not be echoed when void() used: ' . $content);
        $this->assertTrue(strpos($content, 'echo') !== false, 'echo should be there:' . $content);
        $this->assertTrue(strpos($content, 'variable') !== false, 'variable should be there:' . $content);
70 71
    }

72 73 74 75
    /**
     * Mocks view instance
     * @return View
     */
76 77 78 79 80 81 82
    protected function mockView()
    {
        return new View([
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'options' => [
83
                        'cache' => false,
84 85 86
                    ],
                    'globals' => [
                        'html' => '\yii\helpers\Html',
87
                        'pos_begin' => View::POS_BEGIN,
88 89 90
                    ],
                    'functions' => [
                        't' => '\Yii::t',
91
                        'json_encode' => '\yii\helpers\Json::encode',
92 93 94
                    ],
                    'lexerOptions' => [
                        'tag_comment' => [ '{*', '*}' ],
95
                    ],
96 97 98 99 100
                ],
            ],
            'assetManager' => $this->mockAssetManager(),
        ]);
    }
101

102 103 104 105
    /**
     * Mocks asset manager
     * @return AssetManager
     */
106 107 108 109 110 111
    protected function mockAssetManager()
    {
        $assetDir = Yii::getAlias('@runtime/assets');
        if (!is_dir($assetDir)) {
            mkdir($assetDir, 0777, true);
        }
112

113 114 115 116 117
        return new AssetManager([
            'basePath' => $assetDir,
            'baseUrl' => '/assets',
        ]);
    }
AlexGx committed
118
}