TYPO3 CMS  TYPO3_6-2
FileTest.php
Go to the documentation of this file.
1 <?php
3 
17 
25 
29  protected $singletonInstances = array();
30 
34  protected $storageMock;
35 
36  public function setUp() {
38  $this->storageMock = $this->getMock('TYPO3\CMS\Core\Resource\ResourceStorage', array(), array(), '', FALSE);
39  $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
40 
41  $mockedMetaDataRepository = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
42  $mockedMetaDataRepository->expects($this->any())->method('findByFile')->will($this->returnValue(array('file' => 1)));
43  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', $mockedMetaDataRepository);
44  }
45 
46  public function tearDown() {
48  parent::tearDown();
49  }
50 
54  protected function prepareFixture() {
55  $fixture = new \TYPO3\CMS\Core\Resource\File(array('testfile'), $this->storageMock);
56  return $fixture;
57  }
58 
64  $this->markTestSkipped('TYPO3\\CMS\\Core\\Resource\\File::getAvailableProperties() does not exist');
65  $properties = array(
66  $this->getUniqueId() => $this->getUniqueId(),
67  $this->getUniqueId() => $this->getUniqueId(),
68  'uid' => 1
69  );
70  $fixture = new \TYPO3\CMS\Core\Resource\File($properties, $this->storageMock);
71  $availablePropertiesBackup = \TYPO3\CMS\Core\Resource\File::getAvailableProperties();
72  \TYPO3\CMS\Core\Resource\File::setAvailableProperties(array_keys($properties));
73  foreach ($properties as $key => $value) {
74  $this->assertTrue($fixture->hasProperty($key));
75  $this->assertEquals($value, $fixture->getProperty($key));
76  }
77  $this->assertFalse($fixture->hasProperty($this->getUniqueId()));
78  \TYPO3\CMS\Core\Resource\File::setAvailableProperties($availablePropertiesBackup);
79  $this->setExpectedException('InvalidArgumentException', '', 1314226805);
80  $fixture->getProperty($this->getUniqueId());
81  }
82 
87  $properties = array(
88  'name' => $this->getUniqueId(),
89  'storage' => $this->storageMock,
90  'size' => 1024
91  );
92  $fixture = new \TYPO3\CMS\Core\Resource\File($properties, $this->storageMock);
93  foreach ($properties as $key => $value) {
94  $this->assertEquals($value, call_user_func(array($fixture, 'get' . $key)));
95  }
96  }
97 
104  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1), $this->storageMock);
105  $this->assertTrue($fixture->isIndexed());
106  }
107 
112  $identifier = '/' . $this->getUniqueId();
113  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'identifier' => '/test'), $this->storageMock);
114  $fixture->updateProperties(array('identifier' => $identifier));
115  $this->assertEquals($identifier, $fixture->getIdentifier());
116  }
117 
122  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'), $this->storageMock);
123  $fixture->updateProperties(array('foo' => 'foobar'));
124  $this->assertEquals('/test', $fixture->getIdentifier());
125  $this->assertEquals('/test', $fixture->getProperty('identifier'));
126  }
127 
132  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'identifier' => '/test'), $this->storageMock);
133  $fixture->updateProperties(array('uid' => 3));
134  $this->assertEquals(1, $fixture->getUid());
135  }
136 
141  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'), $this->storageMock);
142  $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
143  $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
144  }
145 
150  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'), $this->storageMock);
151  $fixture->updateProperties(array('foo' => 'asdf'));
152  $this->assertEmpty($fixture->getUpdatedProperties());
153  }
154 
159  $fixture = new \TYPO3\CMS\Core\Resource\File(array('uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'), $this->storageMock);
160  $fixture->updateProperties(array('foo' => 'foobar', 'baz' => 'foobaz'));
161  $fixture->updateProperties(array('foo' => 'fdsw', 'baz' => 'asdf'));
162  $this->assertEquals(array('foo', 'baz'), $fixture->getUpdatedProperties());
163  }
164 
169  $fileProperties = array(
170  'uid' => 1,
171  'storage' => 'first',
172  );
173  $subject = $this->getMock(
174  'TYPO3\\CMS\\Core\\Resource\\File',
175  array('loadStorage'),
176  array($fileProperties, $this->storageMock)
177  );
178  $mockedNewStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
179  $mockedResourceFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
180  $mockedResourceFactory
181  ->expects($this->once())
182  ->method('getStorageObject')
183  ->will($this->returnValue($mockedNewStorage));
184  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $mockedResourceFactory);
185 
186  $subject->updateProperties(array('storage' => 'different'));
187  $this->assertSame($mockedNewStorage, $subject->getStorage());
188  }
189 
190 
191 
196  $targetStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
197  $targetFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
198  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
199  $fixture = new \TYPO3\CMS\Core\Resource\File(array(), $this->storageMock);
200  $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
201  $fixture->copyTo($targetFolder);
202  }
203 
208  $targetStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
209  $targetFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
210  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
211  $fixture = new \TYPO3\CMS\Core\Resource\File(array(), $this->storageMock);
212  $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
213  $fixture->moveTo($targetFolder);
214  }
215 
219  public function openCorrectlyOpensFileInDriver() {
220  $this->markTestSkipped();
221  $fixture = $this->prepareFixture();
222  $fileMode = 'invalidMode';
223  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
224  $mockDriver->expects($this->atLeastOnce())->method('getFileHandle')->with($this->equalTo($fixture), $this->equalTo($fileMode));
225  $fixture->setStorageDriver($mockDriver);
226  $fixture->open($fileMode);
227  }
228 
233  $this->markTestSkipped();
234  $fixture = $this->prepareFixture();
235  $fileMode = 'r';
236  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
237  $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
238  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
239  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
240  $fixture->setStorageDriver($mockDriver);
241  $this->assertFalse($fixture->isOpen());
242  $fixture->open($fileMode);
243  $this->assertTrue($fixture->isOpen());
244  }
245 
249  public function fileIsCorrectlyClosed() {
250  $this->markTestSkipped();
251  $fixture = $this->prepareFixture();
252  $fileMode = 'r';
253  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
254  $mockFileHandle->expects($this->once())->method('close');
255  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
256  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
257  $fixture->setStorageDriver($mockDriver);
258  $fixture->open($fileMode);
259  $fixture->close();
260  $this->assertFalse($fixture->isOpen());
261  }
262 
267  $this->markTestSkipped();
268  $fixture = $this->prepareFixture();
269  $fileMode = 'r';
270  $fileContents = 'Some random file contents.';
271  $bytesToRead = 10;
272  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
273  $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
274  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
275  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
276  $mockDriver->expects($this->once())->method('readFromFile')->with($this->anything(), $this->equalTo($bytesToRead))->will($this->returnValue(substr($fileContents, 0, $bytesToRead)));
277  $fixture->setStorageDriver($mockDriver);
278  $fixture->open($fileMode);
279  $this->assertEquals(substr($fileContents, 0, $bytesToRead), $fixture->read($bytesToRead));
280  }
281 
285  public function readFailsIfFileIsClosed() {
286  $this->markTestSkipped();
287  $this->setExpectedException('\\RuntimeException', '', 1299863431);
288  $fixture = $this->prepareFixture();
289  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
290  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
291  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
292  $fixture->setStorageDriver($mockDriver);
293  $fixture->read(1);
294  }
295 
299  public function writePassesContentsToDriver() {
300  $this->markTestSkipped();
301  $fixture = $this->prepareFixture();
302  $fileMode = 'r+';
303  $fileContents = 'Some random file contents.';
304  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
305  $mockFileHandle->expects($this->any())->method('isOpen')->will($this->returnValue(TRUE));
306  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
307  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
308  $mockDriver->expects($this->once())->method('writeToFile')->with($this->anything(), $this->equalTo($fileContents))->will($this->returnValue(TRUE));
309  $fixture->setStorageDriver($mockDriver);
310  $fixture->open($fileMode);
311  $this->assertTrue($fixture->write($fileContents));
312  }
313 
317  public function writeFailsIfFileIsClosed() {
318  $this->markTestSkipped();
319  $this->setExpectedException('\\RuntimeException', '', 1299863432);
320  $fixture = $this->prepareFixture();
321  $mockFileHandle = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileHandle', array(), array(), '', FALSE);
322  $mockDriver = $this->getMockForAbstractClass('t3lib_file_driver_Abstract');
323  $mockDriver->expects($this->any())->method('getFileHandle')->will($this->returnValue($mockFileHandle));
324  $fixture->setStorageDriver($mockDriver);
325  $fixture->write('asdf');
326  }
327 
328  public function filenameExtensionDataProvider() {
329  return array(
330  array('somefile.jpg', 'somefile', 'jpg'),
331  array('SomeFile.PNG', 'SomeFile', 'png'),
332  array('somefile', 'somefile', ''),
333  array('somefile.tar.gz', 'somefile.tar', 'gz'),
334  array('somefile.tar.bz2', 'somefile.tar', 'bz2'),
335  );
336  }
337 
342  public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename) {
343  $fixture = new \TYPO3\CMS\Core\Resource\File(array(
344  'name' => $originalFilename,
345  'identifier' => '/' . $originalFilename
346  ),
347  $this->storageMock);
348  $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
349  }
350 
355  public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension) {
356  $fixture = new \TYPO3\CMS\Core\Resource\File(array(
357  'name' => $originalFilename,
358  'identifier' => '/' . $originalFilename
359  ), $this->storageMock);
360  $this->assertSame($expectedExtension, $fixture->getExtension());
361  }
362 
367  $fixture = new \TYPO3\CMS\Core\Resource\File(array('testproperty' => 'testvalue'), $this->storageMock);
368  $this->assertTrue($fixture->hasProperty('testproperty'));
369  }
370 
375  $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\File', array('dummy'), array(array(), $this->storageMock));
376  $fixture->_set('metaDataLoaded', TRUE);
377  $fixture->_set('metaDataProperties', array('testproperty' => 'testvalue'));
378  $this->assertTrue($fixture->hasProperty('testproperty'));
379  }
380 }
getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:342
static setSingletonInstance($className, \TYPO3\CMS\Core\SingletonInterface $instance)
getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:355
static resetSingletonInstances(array $newSingletonInstances)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)