Skip to content

Commit febc734

Browse files
author
Roman Ganin
committed
MAGETWO-38809: Optimize Magento\Framework\View\Element\Template for production mode
- unit tests
1 parent 04eb0b4 commit febc734

File tree

3 files changed

+216
-14
lines changed

3 files changed

+216
-14
lines changed

lib/internal/Magento/Framework/View/Element/Template/File/Validator.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,38 @@ class Validator
3333
protected $_filesystem;
3434

3535
/**
36+
* Allow symlinks flag
37+
*
3638
* @var bool
3739
*/
3840
protected $_isAllowSymlinks = false;
3941

4042
/**
43+
* Root directory
44+
*
4145
* @var bool
4246
*/
4347
protected $directory = null;
4448

49+
/**
50+
* Themes directory
51+
*
52+
* @var string
53+
*/
4554
protected $_themesDir;
4655

56+
/**
57+
* Application directory
58+
*
59+
* @var string
60+
*/
4761
protected $_appDir;
4862

63+
/**
64+
* Compiled templates directory
65+
*
66+
* @var string
67+
*/
4968
protected $_compiledDir;
5069

5170
/**
@@ -69,19 +88,6 @@ public function __construct(
6988
->getAbsolutePath();
7089
}
7190

72-
/**
73-
* Set allow symlinks for validation
74-
*
75-
* @param bool $flag
76-
*
77-
* @return $this
78-
*/
79-
public function setIsAllowSymlinks($flag = true)
80-
{
81-
$this->_isAllowSymlinks = $flag;
82-
return $this;
83-
}
84-
8591
/**
8692
* Checks whether the provided file can be rendered.
8793
*
@@ -129,7 +135,6 @@ protected function getRootDirectory()
129135
if (null === $this->directory) {
130136
$this->directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
131137
}
132-
133138
return $this->directory;
134139
}
135140
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\View\Test\Unit\Element\Template\File;
7+
8+
/**
9+
* Class ResolverTest
10+
* @package Magento\Framework\View\Test\Unit\Element\Template\File
11+
*/
12+
class ResolverTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* Resolver object
16+
*
17+
* @var \Magento\Framework\View\Element\Template\File\Resolver
18+
*/
19+
protected $_resolver;
20+
21+
/**
22+
* Mock for view file system
23+
*
24+
* @var \Magento\Framework\View\FileSystem|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $_viewFileSystemMock;
27+
28+
/**
29+
* Test Setup
30+
*
31+
* @return void
32+
*/
33+
public function setUp()
34+
{
35+
$this->_viewFileSystemMock = $this->getMock('\Magento\Framework\View\FileSystem', [], [], '', false);
36+
$this->_resolver = new \Magento\Framework\View\Element\Template\File\Resolver(
37+
$this->_viewFileSystemMock
38+
);
39+
}
40+
41+
/**
42+
* Resolver get template file name test
43+
*
44+
* @return void
45+
*/
46+
public function testGetTemplateFileName()
47+
{
48+
$template = 'template.phtml';
49+
$this->_viewFileSystemMock->expects($this->once())
50+
->method('getTemplateFileName')
51+
->with($template)
52+
->will($this->returnValue('path_to' . $template));
53+
$this->assertEquals('path_to' . $template, $this->_resolver->getTemplateFileName($template));
54+
$this->assertEquals('path_to' . $template, $this->_resolver->getTemplateFileName($template));
55+
}
56+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\View\Test\Unit\Element\Template\File;
7+
8+
use \Magento\Framework\App\Filesystem\DirectoryList;
9+
use \Magento\Framework\Filesystem\DriverPool;
10+
11+
/**
12+
* Class ValidatorTest
13+
* @package Magento\Framework\View\Test\Unit\Element\Template\File
14+
*/
15+
class ValidatorTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Resolver object
19+
*
20+
* @var \Magento\Framework\View\Element\Template\File\Validator
21+
*/
22+
protected $_validator;
23+
24+
/**
25+
* Mock for view file system
26+
*
27+
* @var \Magento\Framework\FileSystem|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $_fileSystemMock;
30+
31+
/**
32+
* Mock for scope config
33+
*
34+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $_scopeConfigMock;
37+
38+
/**
39+
* Mock for root directory reader
40+
*
41+
* @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $rootDirectoryMock;
44+
45+
/**
46+
* Mock for app directory reader
47+
*
48+
* @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
protected $appDirectoryMock;
51+
52+
/**
53+
* Mock for themes directory reader
54+
*
55+
* @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
protected $themesDirectoryMock;
58+
59+
/**
60+
* Mock for compiled directory reader
61+
*
62+
* @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
63+
*/
64+
protected $compiledDirectoryMock;
65+
66+
/**
67+
* Test Setup
68+
*
69+
* @return void
70+
*/
71+
public function setUp()
72+
{
73+
$this->_fileSystemMock = $this->getMock('\Magento\Framework\Filesystem', [], [], '', false);
74+
$this->_scopeConfigMock = $this->getMock('\Magento\Framework\App\Config\ScopeConfigInterface');
75+
$this->rootDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
76+
$this->appDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
77+
$this->themesDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
78+
$this->compiledDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
79+
80+
$this->_fileSystemMock->expects($this->any())
81+
->method('getDirectoryRead')
82+
->will($this->returnValueMap(
83+
[
84+
[DirectoryList::ROOT, DriverPool::FILE, $this->rootDirectoryMock],
85+
[DirectoryList::THEMES, DriverPool::FILE, $this->themesDirectoryMock],
86+
[DirectoryList::APP, DriverPool::FILE, $this->appDirectoryMock],
87+
[DirectoryList::TEMPLATE_MINIFICATION_DIR, DriverPool::FILE, $this->compiledDirectoryMock],
88+
]
89+
));
90+
91+
$this->compiledDirectoryMock->expects($this->any())
92+
->method('getAbsolutePath')
93+
->will($this->returnValue('/magento/var/compiled'));
94+
95+
$this->appDirectoryMock->expects($this->any())
96+
->method('getAbsolutePath')
97+
->will($this->returnValue('/magento/app'));
98+
99+
$this->themesDirectoryMock->expects($this->any())
100+
->method('getAbsolutePath')
101+
->will($this->returnValue('/magento/themes'));
102+
103+
$this->_validator = new \Magento\Framework\View\Element\Template\File\Validator(
104+
$this->_fileSystemMock,
105+
$this->_scopeConfigMock
106+
);
107+
}
108+
109+
/**
110+
* Test is file valid
111+
*
112+
* @param string $file
113+
* @param bool $expectedResult
114+
*
115+
* @dataProvider testIsValidDataProvider
116+
*
117+
* @return void
118+
*/
119+
public function testIsValid($file, $expectedResult)
120+
{
121+
122+
$this->rootDirectoryMock->expects($this->any())->method('isFile')->will($this->returnValue(true));
123+
$this->assertEquals($expectedResult, $this->_validator->isValid($file));
124+
}
125+
126+
/**
127+
* Data provider for testIsValid
128+
*
129+
* @return []
130+
*/
131+
public function testIsValidDataProvider()
132+
{
133+
return [
134+
'empty' => ['', false],
135+
'/magento/var/compiled/template.phtml' => ['/magento/var/compiled/template.phtml', true],
136+
'/magento/themes/default/template.phtml' => ['/magento/themes/default/template.phtml', true],
137+
'/magento/app/code/Some/Module/template.phtml' => ['/magento/app/code/Some/Module/template.phtml', true],
138+
'/magento/x' => ['/magento/x', false],
139+
];
140+
}
141+
}

0 commit comments

Comments
 (0)