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

ThemeTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Tests;
6
7
use Yiisoft\Files\FileHelper;
8
use Yiisoft\View\Theme;
9
10
/**
11
 * ThemeTest.
12
 */
13
final class ThemeTest extends \Yiisoft\View\Tests\TestCase
14
{
15
    /**
16
     * @var string path for the test files.
17
     */
18
    protected $testViewPath;
19
20
    public function setUp(): void
21
    {
22
        parent::setUp();
23
24
        $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false);
25
    }
26
27
    public function tearDown(): void
28
    {
29
        parent::tearDown();
30
        FileHelper::removeDirectory($this->testViewPath);
31
    }
32
33
    public function testGetUrlWithoutBaseUrl(): void
34
    {
35
        $theme = new Theme([], '', '');
36
        $result = $theme->getUrl('/test');
37
        $this->assertSame('/test', $result);
38
    }
39
40
    public function testGetUrlWithBaseUrl(): void
41
    {
42
        $theme = new Theme([], '', 'https://yiiframework.com/');
43
        $result = $theme->getUrl('/test');
44
        $this->assertSame('https://yiiframework.com/test', $result);
45
    }
46
47
    public function testGetPathWithoutBasePath(): void
48
    {
49
        $theme = new Theme([], '', '');
50
        $result = $theme->getPath('/test');
51
        $this->assertSame('/test', $result);
52
    }
53
54
    public function testGetPathWithBasePath(): void
55
    {
56
        $theme = new Theme([], '/var/www/yiiframework.com', '');
57
        $result = $theme->getPath('test');
58
        $this->assertSame('/var/www/yiiframework.com/test', $result);
59
    }
60
61
    /**
62
     * If there is no map, return path passed
63
     */
64
    public function testApplyToWithoutMap(): void
65
    {
66
        $theme = new Theme([]);
67
        $path = $theme->applyTo('test');
68
        $this->assertSame('test', $path);
69
    }
70
71
    public function testApplyToBasicMapping(): void
72
    {
73
        $appPath = $this->getPath('/app/views');
74
        $themePath = $this->getPath('/app/themes/basic');
75
76
        $this->touch($themePath . '/test.php');
77
78
        $theme = new Theme([
79
            $appPath => $themePath,
80
        ]);
81
82
        $path = $theme->applyTo($appPath . '/test.php');
83
        $this->assertSameIgnoringSlash($themePath . '/test.php', $path);
84
    }
85
86
    /**
87
     * Fall back to next "to" path from the map if there is no corresponding file in the current path
88
     */
89
    public function testApplyToWithMultipleTos(): void
90
    {
91
        $appPath = $this->getPath('/app/views');
92
        $firstThemePath = $this->getPath('/app/themes/christmas');
93
        $secondThemePath = $this->getPath('/app/themes/basic');
94
95
        $this->touch($firstThemePath . '/banner.php');
96
        $this->touch($secondThemePath . '/test.php');
97
98
        $theme = new Theme([
99
            $appPath => [
100
                $firstThemePath,
101
                $secondThemePath,
102
            ]
103
        ]);
104
105
        $path = $theme->applyTo($appPath . '/test.php');
106
        $this->assertSameIgnoringSlash($secondThemePath . '/test.php', $path);
107
108
        $path = $theme->applyTo($appPath . '/banner.php');
109
        $this->assertSameIgnoringSlash($firstThemePath . '/banner.php', $path);
110
    }
111
112
    /**
113
     * If there is no file in the theme, fall back to path passed to applyTo()
114
     */
115
    public function testApplyToFallback(): void
116
    {
117
        $theme = new Theme([
118
            '/app/views' => '/app/themes/basic'
119
        ]);
120
121
        $path = $theme->applyTo('/app/views/non-existing.php');
122
        $this->assertSameIgnoringSlash('/app/views/non-existing.php', $path);
123
    }
124
125
    private function getPath(string $path): string
126
    {
127
        return $this->testViewPath . $path;
128
    }
129
}
130