Completed
Push — correct-classname-values ( f9b487 )
by Sam
08:29
created

ThemeResourceLoaderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
use SilverStripe\View\ThemeResourceLoader;
4
use SilverStripe\View\ThemeManifest;
5
6
/**
7
 * Tests for the {@link TemplateLoader} class.
8
 *
9
 * @package framework
10
 * @subpackage tests
11
 */
12
class ThemeResourceLoaderTest extends SapphireTest {
13
14
	private $base;
15
16
	/**
17
	 * @var ThemeManifest
18
	 */
19
	private $manifest;
20
21
	/**
22
	 * @var ThemeResourceLoader
23
	 */
24
	private $loader;
25
26
	/**
27
	 * Set up manifest before each test
28
	 */
29
	public function setUp() {
30
		parent::setUp();
31
32
		// Fake project root
33
		$this->base = dirname(__FILE__) . '/fixtures/templatemanifest';
34
		// New ThemeManifest for that root
35
		$this->manifest = new ThemeManifest($this->base, 'myproject', false, true);
36
		// New Loader for that root
37
		$this->loader = new ThemeResourceLoader($this->base);
38
		$this->loader->addSet('$default', $this->manifest);
39
	}
40
41
	/**
42
	 * Test that 'main' and 'Layout' templates are loaded from module
43
	 */
44 View Code Duplication
	public function testFindTemplatesInModule() {
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...
45
		$this->assertEquals(
46
			"$this->base/module/templates/Page.ss",
47
			$this->loader->findTemplate('Page', ['$default'])
48
		);
49
50
		$this->assertEquals(
51
			"$this->base/module/templates/Layout/Page.ss",
52
			$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
53
		);
54
	}
55
56
	public function testFindNestedThemeTemplates() {
57
		// Without including the theme this template cannot be found
58
		$this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default']));
59
60
		// With a nested theme available then it is available
61
		$this->assertEquals(
62
			"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
63
			$this->loader->findTemplate('NestedThemePage', [
64
				'silverstripe/module:subtheme',
65
				'$default'
66
			])
67
		);
68
69
		// Can also be found if excluding $default theme
70
		$this->assertEquals(
71
			"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
72
			$this->loader->findTemplate('NestedThemePage', [
73
				'silverstripe/module:subtheme',
74
			])
75
		);
76
	}
77
78
	/**
79
	 * Test that 'main' and 'Layout' templates are loaded from set theme
80
	 */
81 View Code Duplication
	public function testFindTemplatesInTheme() {
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...
82
		$this->assertEquals(
83
			"$this->base/themes/theme/templates/Page.ss",
84
			$this->loader->findTemplate('Page', ['theme'])
85
		);
86
87
		$this->assertEquals(
88
			"$this->base/themes/theme/templates/Layout/Page.ss",
89
			$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme'])
90
		);
91
	}
92
93
	/**
94
	 * Test that 'main' and 'Layout' templates are loaded from project without a set theme
95
	 */
96
	public function testFindTemplatesInApplication() {
97
		// TODO: replace with one that doesn't create temporary files (so bad)
98
		$templates = array(
99
			$this->base . '/myproject/templates/Page.ss',
100
			$this->base . '/myproject/templates/Layout/Page.ss'
101
		);
102
		$this->createTestTemplates($templates);
103
104
		$this->assertEquals(
105
			"$this->base/myproject/templates/Page.ss",
106
			$this->loader->findTemplate('Page', ['$default'])
107
		);
108
109
		$this->assertEquals(
110
			"$this->base/myproject/templates/Layout/Page.ss",
111
			$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
112
		);
113
114
		$this->removeTestTemplates($templates);
115
	}
116
117
	/**
118
	 * Test that 'main' template is found in theme and 'Layout' is found in module
119
	 */
120 View Code Duplication
	public function testFindTemplatesMainThemeLayoutModule() {
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...
121
		$this->assertEquals(
122
			"$this->base/themes/theme/templates/CustomThemePage.ss",
123
			$this->loader->findTemplate('CustomThemePage', ['theme', '$default'])
124
		);
125
126
		$this->assertEquals(
127
			"$this->base/module/templates/Layout/CustomThemePage.ss",
128
			$this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default'])
129
		);
130
	}
131
132 View Code Duplication
	public function testFindThemedCSS() {
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...
133
		$this->assertEquals(
134
			"myproject/css/project.css",
135
			$this->loader->findThemedCSS('project', ['$default', 'theme'])
136
		);
137
		$this->assertEquals(
138
			"themes/theme/css/project.css",
139
			$this->loader->findThemedCSS('project', ['theme', '$default'])
140
		);
141
		$this->assertEmpty(
142
			$this->loader->findThemedCSS('nofile', ['theme', '$default'])
143
		);
144
		$this->assertEquals(
145
			'module/css/content.css',
146
			$this->loader->findThemedCSS('content', ['/module', 'theme'])
147
		);
148
		$this->assertEquals(
149
			'module/css/content.css',
150
			$this->loader->findThemedCSS('content', ['/module', 'theme', '$default'])
151
		);
152
		$this->assertEquals(
153
			'module/css/content.css',
154
			$this->loader->findThemedCSS('content', ['$default', '/module', 'theme'])
155
		);
156
	}
157
158 View Code Duplication
	public function testFindThemedJavascript() {
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...
159
		$this->assertEquals(
160
			"myproject/javascript/project.js",
161
			$this->loader->findThemedJavascript('project', ['$default', 'theme'])
162
		);
163
		$this->assertEquals(
164
			"themes/theme/javascript/project.js",
165
			$this->loader->findThemedJavascript('project', ['theme', '$default'])
166
		);
167
		$this->assertEmpty(
168
			$this->loader->findThemedJavascript('nofile', ['theme', '$default'])
169
		);
170
		$this->assertEquals(
171
			'module/javascript/content.js',
172
			$this->loader->findThemedJavascript('content', ['/module', 'theme'])
173
		);
174
		$this->assertEquals(
175
			'module/javascript/content.js',
176
			$this->loader->findThemedJavascript('content', ['/module', 'theme', '$default'])
177
		);
178
		$this->assertEquals(
179
			'module/javascript/content.js',
180
			$this->loader->findThemedJavascript('content', ['$default', '/module', 'theme'])
181
		);
182
	}
183
184
	protected function createTestTemplates($templates) {
185
		foreach ($templates as $template) {
186
			file_put_contents($template, '');
187
		}
188
	}
189
190
	protected function removeTestTemplates($templates) {
191
		foreach ($templates as $template) {
192
			unlink($template);
193
		}
194
	}
195
196
}
197