[BUGFIX] Better positionName extraction in executePositionedStringInsertion
[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\CMS\Core\Tests\UnitTestCase
23 {
24 /**
25 * @var string
26 */
27 protected $basedir = 'basedir';
28
29 protected $mountDir;
30
31 protected $vfsContents = array();
32
33 protected function setUp()
34 {
35 $this->mountDir = $this->getUniqueId('mount-');
36 $this->basedir = $this->getUniqueId('base-');
37 vfsStream::setup($this->basedir);
38 // Add an entry for the mount directory to the VFS contents
39 $this->vfsContents = array($this->mountDir => array());
40 }
41
42 protected function getMountRootUrl()
43 {
44 return $this->getUrlInMount('');
45 }
46
47 protected function mergeToVfsContents($contents)
48 {
49 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->vfsContents, $contents);
50 }
51
52 protected function initializeVfs()
53 {
54 if (is_callable('org\\bovigo\\vfs\\vfsStream::create') === false) {
55 $this->markTestSkipped('vfsStream::create() does not exist');
56 }
57 vfsStream::create($this->vfsContents);
58 }
59
60 /**
61 * Adds the given directory structure to the mount folder in the VFS. Existing files will be overwritten!
62 *
63 * @param array $dirStructure
64 * @return void
65 */
66 protected function addToMount(array $dirStructure)
67 {
68 $this->mergeToVfsContents(array($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 * @return void
87 */
88 protected function addToVfs(array $dirStructure)
89 {
90 $this->mergeToVfsContents($dirStructure);
91 }
92
93 /**
94 * Returns the URL for a path inside the VFS
95 *
96 * @param $path
97 * @return string
98 */
99 protected function getUrl($path)
100 {
101 return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
102 }
103
104 /**
105 * Creates a file or folder mock. This should not be called directly, but only through getSimple{File,Folder}Mock()
106 *
107 * @param $type
108 * @param $identifier
109 * @param $mockedMethods
110 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
111 */
112 protected function _createFileFolderMock($type, $identifier, $mockedMethods)
113 {
114 if (!empty($mockedMethods)) {
115 if (!in_array('getIdentifier', $mockedMethods)) {
116 $mockedMethods[] = 'getIdentifier';
117 }
118 if (!in_array('getName', $mockedMethods)) {
119 $mockedMethods[] = 'getName';
120 }
121 }
122 $mock = $this->getMock($type, $mockedMethods, array(), '', false);
123 $mock->expects($this->any())->method('getIdentifier')->will($this->returnValue($identifier));
124 $mock->expects($this->any())->method('getName')->will($this->returnValue(basename($identifier)));
125 return $mock;
126 }
127
128 /**
129 * Returns a simple mock of a file object that just knows its identifier
130 *
131 * @param string $identifier
132 * @param array $mockedMethods the methods to mock
133 * @return \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
134 */
135 protected function getSimpleFileMock($identifier, $mockedMethods = array())
136 {
137 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\File::class, $identifier, $mockedMethods);
138 }
139
140 /**
141 * Returns a simple mock of a file object that just knows its identifier
142 *
143 * @param string $identifier
144 * @param array $mockedMethods the methods to mock
145 * @return \TYPO3\CMS\Core\Resource\Folder
146 */
147 protected function getSimpleFolderMock($identifier, $mockedMethods = array())
148 {
149 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, $mockedMethods);
150 }
151
152 /**
153 * Returns a mock of a folder object with subfolders and files.
154 *
155 * @param $identifier
156 * @param array $mockedMethods Methods to mock, in addition to getFiles and getSubfolders
157 * @param \TYPO3\CMS\Core\Resource\Folder[] $subfolders
158 * @param \TYPO3\CMS\Core\Resource\File[] $files
159 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
160 */
161 protected function getFolderMock($identifier, $mockedMethods = array(), $subfolders = array(), $files = array())
162 {
163 $folder = $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, array_merge($mockedMethods, array('getFiles', 'getSubfolders')));
164 $folder->expects($this->any())->method('getSubfolders')->will($this->returnValue($subfolders));
165 $folder->expects($this->any())->method('getFiles')->will($this->returnValue($files));
166 return $folder;
167 }
168 }