ViewRendererTest.php 5.82 KB
Newer Older
1 2 3
<?php
namespace yiiunit\extensions\twig;

4
use yii\helpers\FileHelper;
5 6 7
use yii\web\AssetManager;
use yii\web\View;
use Yii;
8
use yiiunit\data\ar\Order;
9
use yiiunit\data\base\Singer;
10
use yiiunit\framework\db\DatabaseTestCase;
11 12

/**
13 14 15 16
 * Tests Twig view renderer
 *
 * @author Alexander Makarov <sam@rmcreative.ru>
 * @author Carsten Brandt <mail@cebe.cc>
17
 */
18
class ViewRendererTest extends DatabaseTestCase
19
{
20 21
    protected $driverName = 'sqlite';

22 23
    protected function setUp()
    {
24
        parent::setUp();
Qiang Xue committed
25
        $this->mockWebApplication();
26 27
    }

28 29 30 31 32 33
    protected function tearDown()
    {
        parent::tearDown();
        FileHelper::removeDirectory(Yii::getAlias('@runtime/assets'));
    }

34 35 36 37 38 39 40
    /**
     * https://github.com/yiisoft/yii2/issues/1755
     */
    public function testLayoutAssets()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/layout.twig');
41

Carsten Brandt committed
42
        $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);
43 44 45 46 47 48 49 50
    }

    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);
51
    }
52

53 54 55 56 57 58
    /**
     * https://github.com/yiisoft/yii2/issues/3877
     */
    public function testLexerOptions()
    {
        $view = $this->mockView();
59 60 61 62 63 64 65 66 67 68 69 70 71
        $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);
    }
72

73 74 75 76 77 78 79 80
    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);
81 82
    }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    public function testInheritance()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/twig/views/extends2.twig');
        $this->assertTrue(strpos($content, 'Hello, I\'m inheritance test!') !== false, 'Hello, I\'m inheritance test! should be there:' . $content);
        $this->assertTrue(strpos($content, 'extends2 block') !== false, 'extends2 block should be there:' . $content);
        $this->assertFalse(strpos($content, 'extends1 block') !== false, 'extends1 block should not be there:' . $content);

        $content = $view->renderFile('@yiiunit/extensions/twig/views/extends3.twig');
        $this->assertTrue(strpos($content, 'Hello, I\'m inheritance test!') !== false, 'Hello, I\'m inheritance test! should be there:' . $content);
        $this->assertTrue(strpos($content, 'extends3 block') !== false, 'extends3 block should be there:' . $content);
        $this->assertFalse(strpos($content, 'extends1 block') !== false, 'extends1 block should not be there:' . $content);
    }

    public function testChangeTitle()
    {
        $view = $this->mockView();
        $view->title = 'Original title';

        $content = $view->renderFile('@yiiunit/extensions/twig/views/changeTitle.twig');
        $this->assertTrue(strpos($content, 'New title') !== false, 'New title should be there:' . $content);
        $this->assertFalse(strpos($content, 'Original title') !== false, 'Original title should not be there:' . $content);
    }

107 108 109 110 111 112 113 114
    public function testNullsInAr()
    {
        $view = $this->mockView();
        $order = new Order();
        $order::$db = $this->getConnection();
        $view->renderFile('@yiiunit/extensions/twig/views/nulls.twig', ['order' => $order]);
    }

115 116 117 118
    /**
     * Mocks view instance
     * @return View
     */
119 120 121 122 123 124 125
    protected function mockView()
    {
        return new View([
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'options' => [
126
                        'cache' => false,
127 128 129
                    ],
                    'globals' => [
                        'html' => '\yii\helpers\Html',
130
                        'pos_begin' => View::POS_BEGIN,
131 132 133
                    ],
                    'functions' => [
                        't' => '\Yii::t',
134
                        'json_encode' => '\yii\helpers\Json::encode',
135 136 137
                    ],
                    'lexerOptions' => [
                        'tag_comment' => [ '{*', '*}' ],
138
                    ],
139 140 141 142 143
                ],
            ],
            'assetManager' => $this->mockAssetManager(),
        ]);
    }
144

145 146 147 148
    /**
     * Mocks asset manager
     * @return AssetManager
     */
149 150 151 152 153 154
    protected function mockAssetManager()
    {
        $assetDir = Yii::getAlias('@runtime/assets');
        if (!is_dir($assetDir)) {
            mkdir($assetDir, 0777, true);
        }
155

156 157 158 159 160
        return new AssetManager([
            'basePath' => $assetDir,
            'baseUrl' => '/assets',
        ]);
    }
AlexGx committed
161
}