[BUGFIX] Repair relative path resolvement in ResourceCompressor
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / BaseTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use org\bovigo\vfs\vfsStream;
18
19 /**
20 * Basic test case for the \TYPO3\CMS\Core\Resource\File tests
21 */
22 abstract class BaseTestCase extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * @var string
26 */
27 protected $basedir = 'basedir';
28
29 protected $mountDir;
30
31 protected $vfsContents = [];
32
33 protected function setUp()
34 {
35 parent::setUp();
36 $this->mountDir = $this->getUniqueId('mount-');
37 $this->basedir = $this->getUniqueId('base-');
38 vfsStream::setup($this->basedir);
39 // Add an entry for the mount directory to the VFS contents
40 $this->vfsContents = [$this->mountDir => []];
41 }
42
43 protected function getMountRootUrl()
44 {
45 return $this->getUrlInMount('');
46 }
47
48 protected function mergeToVfsContents($contents)
49 {
50 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->vfsContents, $contents);
51 }
52
53 protected function initializeVfs()
54 {
55 if (is_callable('org\\bovigo\\vfs\\vfsStream::create') === false) {
56 $this->markTestSkipped('vfsStream::create() does not exist');
57 }
58 vfsStream::create($this->vfsContents);
59 }
60
61 /**
62 * Adds the given directory structure to the mount folder in the VFS. Existing files will be overwritten!
63 *
64 * @param array $dirStructure
65 */
66 protected function addToMount(array $dirStructure)
67 {
68 $this->mergeToVfsContents([$this->mountDir => $dirStructure]);
69 }
70
71 /**
72 * Returns the URL for a path inside the mount directory
73 *
74 * @param $path
75 * @return string
76 */
77 protected function getUrlInMount($path)
78 {
79 return vfsStream::url($this->basedir . '/' . $this->mountDir . '/' . ltrim($path, '/'));
80 }
81
82 /**
83 * Adds the given directory structure to the VFS. Existing files will be overwritten!
84 *
85 * @param array $dirStructure
86 */
87 protected function addToVfs(array $dirStructure)
88 {
89 $this->mergeToVfsContents($dirStructure);
90 }
91
92 /**
93 * Returns the URL for a path inside the VFS
94 *
95 * @param $path
96 * @return string
97 */
98 protected function getUrl($path)
99 {
100 return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
101 }
102
103 /**
104 * Creates a file or folder mock. This should not be called directly, but only through getSimple{File,Folder}Mock()
105 *
106 * @param $type
107 * @param $identifier
108 * @param $mockedMethods
109 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
110 */
111 protected function _createFileFolderMock($type, $identifier, $mockedMethods)
112 {
113 if (!empty($mockedMethods)) {
114 if (!in_array('getIdentifier', $mockedMethods)) {
115 $mockedMethods[] = 'getIdentifier';
116 }
117 if (!in_array('getName', $mockedMethods)) {
118 $mockedMethods[] = 'getName';
119 }
120 }
121 $mock = $this->getMockBuilder($type)
122 ->setMethods($mockedMethods)
123 ->disableOriginalConstructor()
124 ->getMock();
125 $mock->expects($this->any())->method('getIdentifier')->will($this->returnValue($identifier));
126 $mock->expects($this->any())->method('getName')->will($this->returnValue(basename($identifier)));
127 return $mock;
128 }
129
130 /**
131 * Returns a simple mock of a file object that just knows its identifier
132 *
133 * @param string $identifier
134 * @param array $mockedMethods the methods to mock
135 * @return \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
136 */
137 protected function getSimpleFileMock($identifier, $mockedMethods = [])
138 {
139 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\File::class, $identifier, $mockedMethods);
140 }
141
142 /**
143 * Returns a simple mock of a file object that just knows its identifier
144 *
145 * @param string $identifier
146 * @param array $mockedMethods the methods to mock
147 * @return \TYPO3\CMS\Core\Resource\Folder
148 */
149 protected function getSimpleFolderMock($identifier, $mockedMethods = [])
150 {
151 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, $mockedMethods);
152 }
153
154 /**
155 * Returns a mock of a folder object with subfolders and files.
156 *
157 * @param $identifier
158 * @param array $mockedMethods Methods to mock, in addition to getFiles and getSubfolders
159 * @param \TYPO3\CMS\Core\Resource\Folder[] $subfolders
160 * @param \TYPO3\CMS\Core\Resource\File[] $files
161 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
162 */
163 protected function getFolderMock($identifier, $mockedMethods = [], $subfolders = [], $files = [])
164 {
165 $folder = $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, array_merge($mockedMethods, ['getFiles', 'getSubfolders']));
166 $folder->expects($this->any())->method('getSubfolders')->will($this->returnValue($subfolders));
167 $folder->expects($this->any())->method('getFiles')->will($this->returnValue($files));
168 return $folder;
169 }
170 }