ViewRendererTest.php 1.85 KB
Newer Older
1 2
<?php
/**
3 4
 *
 *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\extensions\twig;

use yii\web\AssetManager;
use yii\web\JqueryAsset;
use yii\web\View;
use Yii;
use yiiunit\TestCase;

/**
 * @group twig
 */
class ViewRendererTest extends TestCase
{
21 22 23 24 25 26 27 28 29 30 31 32 33
    protected function setUp()
    {
        $this->mockApplication();
    }

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

35 36
        $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);
    }
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    protected function mockView()
    {
        return new View([
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    //'cachePath' => '@runtime/Twig/cache',
                    'options' => [
                        'cache' => false
                    ],
                    'globals' => [
                        'html' => '\yii\helpers\Html',
                        'pos_begin' => View::POS_BEGIN
                    ],
                    'functions' => [
                        't' => '\Yii::t',
                        'json_encode' => '\yii\helpers\Json::encode'
                    ]
                ],
            ],
            'assetManager' => $this->mockAssetManager(),
        ]);
    }
61

62 63 64 65 66 67
    protected function mockAssetManager()
    {
        $assetDir = Yii::getAlias('@runtime/assets');
        if (!is_dir($assetDir)) {
            mkdir($assetDir, 0777, true);
        }
68

69 70 71 72 73
        return new AssetManager([
            'basePath' => $assetDir,
            'baseUrl' => '/assets',
        ]);
    }
AlexGx committed
74
}