8355a56334732269aaa09559bcfdb016fb1bded6
[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 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use \org\bovigo\vfs\vfsStream;
31
32 /**
33 * Basic test case for the \TYPO3\CMS\Core\Resource\File tests
34 *
35 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
36 */
37 abstract class BaseTestCase extends \TYPO3\CMS\Core\Tests\UnitTestCase {
38
39 /**
40 * @var string
41 */
42 protected $basedir = 'basedir';
43
44 protected $mountDir;
45
46 protected $vfsContents = array();
47
48 public function setUp() {
49 $this->mountDir = uniqid('mount-');
50 $this->basedir = uniqid('base-');
51 vfsStream::setup($this->basedir);
52 // Add an entry for the mount directory to the VFS contents
53 $this->vfsContents = array($this->mountDir => array());
54 }
55
56 protected function getMountRootUrl() {
57 return $this->getUrlInMount('');
58 }
59
60 protected function mergeToVfsContents($contents) {
61 $this->vfsContents = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->vfsContents, $contents);
62 }
63
64 protected function initializeVfs() {
65 if (is_callable('vfsStream::create') === FALSE) {
66 $this->markTestSkipped('vfsStream::create() does not exist');
67 }
68 vfsStream::create($this->vfsContents);
69 }
70
71 /**
72 * Adds the given directory structure to the mount folder in the VFS. Existing files will be overwritten!
73 *
74 * @param array $dirStructure
75 * @return void
76 */
77 protected function addToMount(array $dirStructure) {
78 $this->mergeToVfsContents(array($this->mountDir => $dirStructure));
79 }
80
81 /**
82 * Returns the URL for a path inside the mount directory
83 *
84 * @param $path
85 * @return string
86 */
87 protected function getUrlInMount($path) {
88 return vfsStream::url($this->basedir . '/' . $this->mountDir . '/' . ltrim($path, '/'));
89 }
90
91 /**
92 * Adds the given directory structure to the VFS. Existing files will be overwritten!
93 *
94 * @param array $dirStructure
95 * @return void
96 */
97 protected function addToVfs(array $dirStructure) {
98 $this->mergeToVfsContents($dirStructure);
99 }
100
101 /**
102 * Returns the URL for a path inside the VFS
103 *
104 * @param $path
105 * @return string
106 */
107 protected function getUrl($path) {
108 return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
109 }
110
111 /**
112 * Creates a file or folder mock. This should not be called directly, but only through getSimple{File,Folder}Mock()
113 *
114 * @param $type
115 * @param $identifier
116 * @param $mockedMethods
117 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
118 */
119 protected function _createFileFolderMock($type, $identifier, $mockedMethods) {
120 if (!empty($mockedMethods)) {
121 if (!in_array('getIdentifier', $mockedMethods)) {
122 $mockedMethods[] = 'getIdentifier';
123 }
124 if (!in_array('getName', $mockedMethods)) {
125 $mockedMethods[] = 'getName';
126 }
127 }
128 $mock = $this->getMock($type, $mockedMethods, array(), '', FALSE);
129 $mock->expects($this->any())->method('getIdentifier')->will($this->returnValue($identifier));
130 $mock->expects($this->any())->method('getName')->will($this->returnValue(basename($identifier)));
131 return $mock;
132 }
133
134 /**
135 * Returns a simple mock of a file object that just knows its identifier
136 *
137 * @param string $identifier
138 * @param array $mockedMethods the methods to mock
139 * @return \TYPO3\CMS\Core\Resource\File
140 */
141 protected function getSimpleFileMock($identifier, $mockedMethods = array()) {
142 return $this->_createFileFolderMock('TYPO3\\CMS\\Core\\Resource\\File', $identifier, $mockedMethods);
143 }
144
145 /**
146 * Returns a simple mock of a file object that just knows its identifier
147 *
148 * @param string $identifier
149 * @param array $mockedMethods the methods to mock
150 * @return \TYPO3\CMS\Core\Resource\Folder
151 */
152 protected function getSimpleFolderMock($identifier, $mockedMethods = array()) {
153 return $this->_createFileFolderMock('TYPO3\\CMS\\Core\\Resource\\Folder', $identifier, $mockedMethods);
154 }
155
156 /**
157 * Returns a mock of a folder object with subfolders and files.
158 *
159 * @param $identifier
160 * @param array $mockedMethods Methods to mock, in addition to getFiles and getSubfolders
161 * @param \TYPO3\CMS\Core\Resource\Folder[] $subfolders
162 * @param \TYPO3\CMS\Core\Resource\File[] $files
163 * @return \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder
164 */
165 protected function getFolderMock($identifier, $mockedMethods = array(), $subfolders = array(), $files = array()) {
166 $folder = $this->_createFileFolderMock('TYPO3\\CMS\\Core\\Resource\\Folder', $identifier, array_merge($mockedMethods, array('getFiles', 'getSubfolders')));
167 $folder->expects($this->any())->method('getSubfolders')->will($this->returnValue($subfolders));
168 $folder->expects($this->any())->method('getFiles')->will($this->returnValue($files));
169 return $folder;
170 }
171
172 }
173
174 ?>