Passed
Pull Request — master (#83)
by Wilmer
28:01 queued 13:07
created

ViewTest::testLocalizedDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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