5d45e70b3c140b720de6f56fed25ca2d6906101e
[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 $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 = [$this->mountDir => []];
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 */
65 protected function addToMount(array $dirStructure)
66 {
67 $this->mergeToVfsContents([$this->mountDir => $dirStructure]);
68 }
69
70 /**
71 * Returns the URL for a path inside the mount directory
72 *
73 * @param $path
74 * @return string
75 */
76 protected function getUrlInMount($path)
77 {
78 return vfsStream::url($this->basedir . '/' . $this->mountDir . '/' . ltrim($path, '/'));
79 }
80
81 /**
82 * Adds the given directory structure to the VFS. Existing files will be overwritten!
83 *
84 * @param array $dirStructure
85 */
86 protected function addToVfs(array $dirStructure)
87 {
88 $this->mergeToVfsContents($dirStructure);
89 }
90
91 /**
92 * Returns the URL for a path inside the VFS
93 *
94 * @param $path
95 * @return string
96 */
97 protected function getUrl($path)
98 {
99 return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
100 }
101
102 /**
103 * Creates a file or folder mock. This should not be called directly, but only through getSimple{File,Folder}Mock()
104 *
105 * @param $type
106 * @param $identifier
107 * @param $mockedMethods
108 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
109 */
110 protected function _createFileFolderMock($type, $identifier, $mockedMethods)
111 {
112 if (!empty($mockedMethods)) {
113 if (!in_array('getIdentifier', $mockedMethods)) {
114 $mockedMethods[] = 'getIdentifier';
115 }
116 if (!in_array('getName', $mockedMethods)) {
117 $mockedMethods[] = 'getName';
118 }
119 }
120 $mock = $this->getMockBuilder($type)
121 ->setMethods($mockedMethods)
122 ->disableOriginalConstructor()
123 ->getMock();
124 $mock->expects($this->any())->method('getIdentifier')->will($this->returnValue($identifier));
125 $mock->expects($this->any())->method('getName')->will($this->returnValue(basename($identifier)));
126 return $mock;
127 }
128
129 /**
130 * Returns a simple mock of a file object that just knows its identifier
131 *
132 * @param string $identifier
133 * @param array $mockedMethods the methods to mock
134 * @return \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
135 */
136 protected function getSimpleFileMock($identifier, $mockedMethods = [])
137 {
138 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\File::class, $identifier, $mockedMethods);
139 }
140
141 /**
142 * Returns a simple mock of a file object that just knows its identifier
143 *
144 * @param string $identifier
145 * @param array $mockedMethods the methods to mock
146 * @return \TYPO3\CMS\Core\Resource\Folder
147 */
148 protected function getSimpleFolderMock($identifier, $mockedMethods = [])
149 {
150 return $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, $mockedMethods);
151 }
152
153 /**
154 * Returns a mock of a folder object with subfolders and files.
155 *
156 * @param $identifier
157 * @param array $mockedMethods Methods to mock, in addition to getFiles and getSubfolders
158 * @param \TYPO3\CMS\Core\Resource\Folder[] $subfolders
159 * @param \TYPO3\CMS\Core\Resource\File[] $files
160 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
161 */
162 protected function getFolderMock($identifier, $mockedMethods = [], $subfolders = [], $files = [])
163 {
164 $folder = $this->_createFileFolderMock(\TYPO3\CMS\Core\Resource\Folder::class, $identifier, array_merge($mockedMethods, ['getFiles', 'getSubfolders']));
165 $folder->expects($this->any())->method('getSubfolders')->will($this->returnValue($subfolders));
166 $folder->expects($this->any())->method('getFiles')->will($this->returnValue($files));
167 return $folder;
168 }
169 }