[BUGFIX] Fix the unit tests to work with PHPUnit 3.6
[Packages/TYPO3.CMS.git] / tests / t3lib / file / FileTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once 'vfsStream/vfsStream.php';
27
28 /**
29 * Testcase for the file class of the TYPO3 FAL
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 *
34 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
35 * @todo Many, many, many tests are skipped in this test case...
36 */
37 class t3lib_file_FileTest extends Tx_Phpunit_TestCase {
38 protected function tearDown() {
39 t3lib_div::purgeInstances();
40 }
41
42 /**
43 * @return t3lib_file_File
44 */
45 protected function prepareFixture() {
46 $fixture = new t3lib_file_File('testfile');
47
48 return $fixture;
49 }
50
51 /**
52 * @test
53 * @todo Old code getAvailableProperties() needs to be replaced by current behaviour
54 */
55 public function propertiesPassedToConstructorAreAvailableViaGenericGetter() {
56 $this->markTestSkipped('t3lib_file_File::getAvailableProperties() does not exist');
57
58 $properties = array(
59 uniqid() => uniqid(), uniqid() => uniqid(), 'uid' => 1
60 );
61 $fixture = new t3lib_file_File($properties);
62 $availablePropertiesBackup = t3lib_file_File::getAvailableProperties();
63 t3lib_file_File::setAvailableProperties(array_keys($properties));
64
65 foreach ($properties as $key => $value) {
66 $this->assertTrue($fixture->hasProperty($key));
67 $this->assertEquals($value, $fixture->getProperty($key));
68 }
69 $this->assertFalse($fixture->hasProperty(uniqid()));
70
71 t3lib_file_File::setAvailableProperties($availablePropertiesBackup);
72
73 $this->setExpectedException('InvalidArgumentException', '', 1314226805);
74 $fixture->getProperty(uniqid());
75 }
76
77 /**
78 * @test
79 */
80 public function commonPropertiesAreAvailableWithOwnGetters() {
81 $properties = array(
82 'name' => uniqid(),
83 'storage' => $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE),
84 'size' => 1024
85 );
86
87 $fixture = new t3lib_file_File($properties);
88 foreach ($properties as $key => $value) {
89 $this->assertEquals($value, call_user_func(array($fixture, 'get' . $key)));
90 }
91 }
92
93 /**
94 * @test
95 */
96 public function fileAsksRepositoryForIndexStatus() {
97 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
98 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array()));
99 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
100
101 $fixture = new t3lib_file_File(array());
102 $this->assertTrue($fixture->isIndexed());
103 }
104
105 /**
106 * Tests if a file is seen as indexed if the record has a uid
107 *
108 * @test
109 */
110 public function fileIndexStatusIsTrueIfUidIsSet() {
111 $fixture = new t3lib_file_File(array('uid' => 1));
112 $this->assertTrue($fixture->isIndexed());
113 }
114
115 /**
116 * @test
117 */
118 public function updatePropertiesUpdatesFileProperties() {
119 $identifier = '/' . uniqid();
120 $fixture = new t3lib_file_File(array('uid' => 1, 'identifier' => '/test'));
121 $fixture->updateProperties(array('identifier' => $identifier));
122
123 $this->assertEquals($identifier, $fixture->getIdentifier());
124 }
125
126 /**
127 * @test
128 */
129 public function updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties() {
130 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
131 $fixture->updateProperties(array('foo' => 'foobar'));
132
133 $this->assertEquals('/test', $fixture->getIdentifier());
134 $this->assertEquals('/test', $fixture->getProperty('identifier'));
135 }
136
137 /**
138 * @test
139 */
140 public function updatePropertiesDiscardsUidIfAlreadySet() {
141 $fixture = new t3lib_file_File(array('uid' => 1, 'identifier' => '/test'));
142 $fixture->updateProperties(array('uid' => 3));
143
144 $this->assertEquals(1, $fixture->getUid());
145 }
146
147 /**
148 * @test
149 */
150 public function updatePropertiesRecordsNamesOfChangedProperties() {
151 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'));
152 $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
153
154 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
155 }
156
157 /**
158 * @test
159 */
160 public function updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided() {
161 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'));
162 $fixture->updateProperties(array('foo' => 'asdf'));
163
164 $this->assertEmpty($fixture->getUpdatedProperties());
165 }
166
167 /**
168 * @test
169 */
170 public function updatePropertiesMarksPropertyAsChangedOnlyOnce() {
171 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'));
172 $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
173 $fixture->updateProperties(array('foo' => 'fdsw', 'baz' => 'asdf'));
174
175 $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
176 }
177
178 /**
179 * @test
180 */
181 public function updatePropertiesReloadsStorageObjectIfStorageChanges() {
182 $mockedNewStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
183 $mockedOldStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
184 $fixture = new t3lib_file_File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test', 'storage' => $mockedOldStorage));
185
186 $mockedRepository = $this->getMock('t3lib_file_Repository_StorageRepository');
187 $mockedRepository->expects($this->once())->method('findByUid')->with(2)->will($this->returnValue($mockedNewStorage));
188 t3lib_div::setSingletonInstance('t3lib_file_Repository_StorageRepository', $mockedRepository);
189
190 $fixture->updateProperties(array('storage' => 2));
191 $this->assertSame($mockedNewStorage, $fixture->getStorage());
192 }
193
194 /**
195 * @test
196 */
197 public function fetchingIndexedPropertyCausesFileObjectToLoadIndexRecord() {
198 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
199
200 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
201 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(array('uid' => 10)));
202 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
203
204 $this->assertEquals(10, $fixture->getProperty('uid'));
205 }
206
207 /**
208 * @test
209 */
210 public function isIndexedTriggersIndexingIfFileIsNotIndexedAlready() {
211 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
212
213 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
214 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
215 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
216 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
217
218 $fixture->isIndexed();
219 }
220
221 /**
222 * @test
223 */
224 public function fileIsAutomaticallyIndexedOnPropertyAccessIfNotAlreadyIndexed() {
225 $fixture = new t3lib_file_File(array('identifier' => '/test', 'storage' => 1));
226
227 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
228 $mockedRepository->expects($this->once())->method('getFileIndexRecord')->will($this->returnValue(FALSE));
229 $mockedRepository->expects($this->once())->method('addToIndex')->will($this->returnValue(array('uid' => 10)));
230 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
231
232 $this->assertEquals(10, $fixture->getProperty('uid'));
233 }
234
235 /**
236 * @test
237 */
238 public function copyToCallsCopyOperationOnTargetFolderStorage() {
239 $targetStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
240 $targetFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
241 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
242
243 $fixture = new t3lib_file_File(array());
244
245 $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
246 $fixture->copyTo($targetFolder);
247 }
248
249 /**
250 * @test
251 */
252 public function moveToCallsMoveOperationOnTargetFolderStorage() {
253 $targetStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
254 $targetFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
255 $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
256
257 $fixture = new t3lib_file_File(array());
258
259 $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
260 $fixture->moveTo($targetFolder);
261 }
262
263 /**
264 * @test
265 */
266 public function openCorrectlyOpensFileInDriver() {
267 $this->markTestSkipped();
268 $fixture = $this->prepareFixture();
269 $fileMode = 'invalidMode';
270
271 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
272 $mockDriver->expects($this->atLeastOnce())->method('getFileHandle')->with($this->equalTo($fixture), $this->equalTo($fileMode));
273
274 $fixture->setStorageDriver($mockDriver);
275 $fixture->open($fileMode);
276 }
277
278 /**
279 * @test
280 */
281 public function isOpenReturnsCorrectValuesForClosedAndOpenFile() {
282 $this->markTestSkipped();
283 $fixture = $this->prepareFixture();
284 $fileMode = 'r';
285
286 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
287 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
288 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
289 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
290
291 $fixture->setStorageDriver($mockDriver);
292
293 $this->assertFalse($fixture->isOpen());
294 $fixture->open($fileMode);
295 $this->assertTrue($fixture->isOpen());
296 }
297
298 /**
299 * @test
300 */
301 public function fileIsCorrectlyClosed() {
302 $this->markTestSkipped();
303 $fixture = $this->prepareFixture();
304 $fileMode = 'r';
305
306 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
307 $mockFileHandle->expects($this->once())->method('close');
308 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
309 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
310
311 $fixture->setStorageDriver($mockDriver);
312
313 $fixture->open($fileMode);
314 $fixture->close();
315 $this->assertFalse($fixture->isOpen());
316 }
317
318 /**
319 * @test
320 */
321 public function readReturnsRequestedContentsFromDriver() {
322 $this->markTestSkipped();
323 $fixture = $this->prepareFixture();
324 $fileMode = 'r';
325 $fileContents = 'Some random file contents.';
326 $bytesToRead = 10;
327
328 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
329 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
330 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
331 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
332 $mockDriver->expects($this->once())->method('readFromFile')->with($this->anything(), $this->equalTo($bytesToRead))
333 ->will($this->returnValue(substr($fileContents, 0, $bytesToRead)));
334
335 $fixture->setStorageDriver($mockDriver);
336
337 $fixture->open($fileMode);
338 $this->assertEquals(substr($fileContents, 0, $bytesToRead), $fixture->read($bytesToRead));
339 }
340
341 /**
342 * @test
343 */
344 public function readFailsIfFileIsClosed() {
345 $this->markTestSkipped();
346 $this->setExpectedException('RuntimeException', '', 1299863431);
347
348 $fixture = $this->prepareFixture();
349
350 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
351 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
352 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
353
354 $fixture->setStorageDriver($mockDriver);
355
356 $fixture->read(1);
357 }
358
359 /**
360 * @test
361 */
362 public function writePassesContentsToDriver() {
363 $this->markTestSkipped();
364 $fixture = $this->prepareFixture();
365 $fileMode = 'r+';
366 $fileContents = 'Some random file contents.';
367
368 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
369 $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
370 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
371 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
372 $mockDriver->expects($this->once())->method('writeToFile')->with($this->anything(), $this->equalTo($fileContents))
373 ->will($this->returnValue(TRUE));
374
375 $fixture->setStorageDriver($mockDriver);
376
377 $fixture->open($fileMode);
378 $this->assertTrue($fixture->write($fileContents));
379 }
380
381 /**
382 * @test
383 */
384 public function writeFailsIfFileIsClosed() {
385 $this->markTestSkipped();
386 $this->setExpectedException('RuntimeException', '', 1299863432);
387
388 $fixture = $this->prepareFixture();
389
390 $mockFileHandle = $this->getMock('t3lib_file_FileHandle', array(), array(), '', FALSE);
391 $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
392 $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
393
394 $fixture->setStorageDriver($mockDriver);
395
396 $fixture->write('asdf');
397 }
398 }
399 ?>