ViewRendererTest::testTwigRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-js-params
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\test\unit\jsParams;
9
10
use Yii;
11
12
use yii\smarty\ViewRenderer as SmartyRenderer;
13
use yii\twig\ViewRenderer as TwigRenderer;
14
15
/**
16
 * Class ViewRendererTest
17
 *
18
 * @author Vuong Minh <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ViewRendererTest extends TestCase
22
{
23
24
    public function testPHPRenderer()
25
    {
26
        $view = Yii::$app->getView();
27
        $result = $view->renderFile(__DIR__ . '/view-test.php', [
28
            'jsParams' => [
29
                'a' => 123
30
            ]
31
        ]);
32
        $this->assertRegExp('/window.serverParams = \{"a":123\}/', $result);
33
        $this->assertFalse(isset($view->renderers['php']));
34
    }
35
36 View Code Duplication
    public function testSmartyRenderer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $view = Yii::$app->getView();
39
        $result = $view->renderFile(__DIR__ . '/view-test.tpl', [
40
            'jsParams' => [
41
                'a' => 123
42
            ]
43
        ]);
44
        $this->assertRegExp('/window.serverParams = \{"a":123\}/', $result);
45
        $this->assertTrue($view->renderers['tpl'] instanceof SmartyRenderer);
46
    }
47
48 View Code Duplication
    public function testTwigRenderer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $view = Yii::$app->getView();
51
        $result = $view->renderFile(__DIR__ . '/view-test.twig', [
52
            'jsParams' => [
53
                'a' => 123
54
            ]
55
        ]);
56
        $this->assertRegExp('/window.serverParams = \{"a":123\}/', $result);
57
        $this->assertTrue($view->renderers['twig'] instanceof TwigRenderer);
58
    }
59
60
    public function testGlobalParams()
61
    {
62
        $view = Yii::$app->getView();
63
        $view->params['jsParams']['a'] = 123;
64
        $result = $view->renderFile(__DIR__ . '/view-test.php');
65
        $this->assertRegExp('/window.serverParams = \{"a":123\}/', $result);
66
67
        $view->params['jsParams'] = function ($_view) use ($view) {
68
            $this->assertEquals($_view, $view);
69
70
            return ['a' => 123];
71
        };
72
        $result = $view->renderFile(__DIR__ . '/view-test.php');
73
        $this->assertRegExp('/window.serverParams = \{"a":123\}/', $result);
74
    }
75
}
76