Passed
Pull Request — master (#75)
by Alexander
03:54
created

ViewTest::testPlaceholderSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Tests;
6
7
use hiqdev\composer\config\Builder;
8
use Yiisoft\Di\Container;
9
use Yiisoft\Files\FileHelper;
10
use Yiisoft\View\Theme;
11
use Yiisoft\View\View;
12
13
/**
14
 * ViewTest.
15
 */
16
final class ViewTest extends \Yiisoft\View\Tests\TestCase
17
{
18
    /**
19
     * @var string path for the test files.
20
     */
21
    private $testViewPath = '';
22
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false);
28
29
        FileHelper::createDirectory($this->testViewPath);
30
    }
31
32
    public function tearDown(): void
33
    {
34
        parent::tearDown();
35
        FileHelper::removeDirectory($this->testViewPath);
36
    }
37
38
    /**
39
     * @see https://github.com/yiisoft/yii2/issues/13058
40
     */
41
    public function testExceptionOnRenderFile(): void
42
    {
43
        $view = $this->createView($this->testViewPath);
44
45
        $exceptionViewFile = $this->testViewPath . DIRECTORY_SEPARATOR . 'exception.php';
46
        file_put_contents($exceptionViewFile, <<<'PHP'
47
<h1>Exception</h1>
48
<?php throw new Exception('Test Exception'); ?>
49
PHP
50
        );
51
        $normalViewFile = $this->testViewPath . DIRECTORY_SEPARATOR . 'no-exception.php';
52
        file_put_contents($normalViewFile, <<<'PHP'
53
<h1>No Exception</h1>
54
PHP
55
        );
56
57
        $obInitialLevel = ob_get_level();
58
59
        try {
60
            $view->renderFile($exceptionViewFile);
61
        } catch (\Exception $e) {
62
            // shutdown exception
63
        }
64
        $view->renderFile($normalViewFile);
65
66
        $this->assertEquals($obInitialLevel, ob_get_level());
67
    }
68
69
    public function testRelativePathInView(): void
70
    {
71
        $themePath = $this->testViewPath . '/theme1';
72
        FileHelper::createDirectory($themePath);
73
74
        $baseView = "{$this->testViewPath}/theme1/base.php";
75
        file_put_contents($baseView, <<<'PHP'
76
<?= $this->render("sub") ?>
77
PHP
78
        );
79
80
        $subView = "{$this->testViewPath}/sub.php";
81
        $subViewContent = 'subviewcontent';
82
        file_put_contents($subView, $subViewContent);
83
84
        $view = $this->createView(
85
            $this->testViewPath,
86
            new Theme([
87
                $this->testViewPath => $themePath,
88
            ])
89
        );
90
91
        $this->assertSame($subViewContent, $view->render('//base'));
92
    }
93
94
    public function testLocalizedDirectory(): void
95
    {
96
        $view = $this->createView($this->testViewPath);
97
        $this->createFileStructure([
98
            'views' => [
99
                'faq.php' => 'English FAQ',
100
                'de-DE' => [
101
                    'faq.php' => 'German FAQ',
102
                ],
103
            ],
104
        ], $this->testViewPath);
105
        $viewFile = $this->testViewPath . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'faq.php';
106
        $sourceLanguage = 'en-US';
107
108
        // Source language and target language are same. The view path should be unchanged.
109
        $currentLanguage = $sourceLanguage;
110
        $this->assertSame($viewFile, $view->localize($viewFile, $currentLanguage, $sourceLanguage));
111
112
        // Source language and target language are different. The view path should be changed.
113
        $currentLanguage = 'de-DE';
114
        $this->assertSame(
115
            $this->testViewPath . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $currentLanguage . DIRECTORY_SEPARATOR . 'faq.php',
116
            $view->localize($viewFile, $currentLanguage, $sourceLanguage)
117
        );
118
    }
119
120
    /**
121
     * Creates test files structure.
122
     * @param string $baseDirectory base directory path.
123
     * @param array $items file system objects to be created in format: objectName => objectContent
124
     * Arrays specifies directories, other values - files.
125
     */
126
    private function createFileStructure(array $items, string $baseDirectory = null): void
127
    {
128
        foreach ($items as $name => $content) {
129
            $itemName = $baseDirectory . '/' . $name;
130
            if (\is_array($content)) {
131
                if (isset($content[0], $content[1]) && $content[0] === 'symlink') {
132
                    symlink($baseDirectory . DIRECTORY_SEPARATOR . $content[1], $itemName);
133
                } else {
134
                    if (!mkdir($itemName, 0777, true) && !is_dir($itemName)) {
135
                        throw new \RuntimeException(sprintf('Directory "%s" was not created', $itemName));
136
                    }
137
                    $this->createFileStructure($content, $itemName);
138
                }
139
            } else {
140
                file_put_contents($itemName, $content);
141
            }
142
        }
143
    }
144
145
    public function testDefaultParameterIsPassedToView(): void
146
    {
147
        $config = require Builder::path('tests');
148
149
        $container = new Container($config);
150
        $view = $container->get(View::class);
151
        $view->setDefaultParameters(['parameter' => 'default_parameter']);
152
        $output = $view->render('//parameters');
153
        $this->assertStringContainsString('default_parameter', $output);
154
    }
155
156
    public function testDefaultParameterIsOverwrittenByLocalParameter(): void
157
    {
158
        $config = require Builder::path('tests');
159
160
        $container = new Container($config);
161
        $view = $container->get(View::class);
162
        $view->setDefaultParameters(['parameter' => 'default_parameter']);
163
        $output = $view->render('//parameters', [
164
            'parameter' => 'local_parameter',
165
        ]);
166
        $this->assertStringContainsString('local_parameter', $output);
167
    }
168
169
    public function testPlaceholderSalt(): void
170
    {
171
        $config = require Builder::path('tests');
172
173
        $container = new Container($config);
174
        $view = $container->get(View::class);
175
        $view->setPlaceholderSalt('apple');
176
        $this->assertSame(dechex(crc32('apple')), $view->getPlaceholderSignature());
177
    }
178
}
179