ViewRendererTest.php 4.16 KB
Newer Older
1 2
<?php
/**
3 4
 *
 *
5 6 7 8 9
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\extensions\smarty;

10
use yii\helpers\FileHelper;
11 12 13
use yii\web\AssetManager;
use yii\web\View;
use Yii;
14
use yiiunit\data\base\Singer;
15 16 17 18 19 20 21
use yiiunit\TestCase;

/**
 * @group smarty
 */
class ViewRendererTest extends TestCase
{
22 23
    protected function setUp()
    {
24
        parent::setUp();
Qiang Xue committed
25
        $this->mockWebApplication();
26
    }
27

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

35 36 37 38 39 40 41
    /**
     * https://github.com/yiisoft/yii2/issues/2265
     */
    public function testNoParams()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/smarty/views/simple.tpl');
42

43 44
        $this->assertEquals('simple view without parameters.', $content);
    }
45

46 47 48 49
    public function testRender()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/smarty/views/view.tpl', ['param' => 'Hello World!']);
50

51 52
        $this->assertEquals('test view Hello World!.', $content);
    }
53

54 55 56 57 58
    public function testLayoutAssets()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/smarty/views/layout.tpl');

Carsten Brandt committed
59
        $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);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    }


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

        $content = $view->renderFile('@yiiunit/extensions/smarty/views/changeTitle.tpl');
        $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);
    }

    public function testForm()
    {
        $view = $this->mockView();
        $model = new Singer();
        $content = $view->renderFile('@yiiunit/extensions/smarty/views/form.tpl', ['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);
    }

    public function testInheritance()
    {
        $view = $this->mockView();
        $content = $view->renderFile('@yiiunit/extensions/smarty/views/extends2.tpl');
        $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/smarty/views/extends3.tpl');
        $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);
    }

95 96 97 98 99 100 101 102 103
    /**
     * @return View
     */
    protected function mockView()
    {
        return new View([
            'renderers' => [
                'tpl' => [
                    'class' => 'yii\smarty\ViewRenderer',
104 105 106
                    'options' => [
                        'force_compile' => true, // always recompile templates, don't do it in production
                    ],
107 108 109 110 111
                ],
            ],
            'assetManager' => $this->mockAssetManager(),
        ]);
    }
112

113 114 115 116 117 118 119 120 121 122 123 124
    protected function mockAssetManager()
    {
        $assetDir = Yii::getAlias('@runtime/assets');
        if (!is_dir($assetDir)) {
            mkdir($assetDir, 0777, true);
        }

        return new AssetManager([
            'basePath' => $assetDir,
            'baseUrl' => '/assets',
        ]);
    }
AlexGx committed
125
}