|
| 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