[BUGFIX] require_once of vfsStream triggers travis fail
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / FolderTest.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 /**
31 * Testcase for the storage collection class of the TYPO3 FAL
32 *
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 */
35 class FolderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 private $basedir = 'basedir';
43
44 public function setUp() {
45 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
46 \vfsStream::setup($this->basedir);
47 }
48
49 protected function tearDown() {
50 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
51 }
52
53 protected function createFolderFixture($path, $name, $mockedStorage = NULL) {
54 if ($mockedStorage === NULL) {
55 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
56 }
57 return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
58 }
59
60 /**
61 * @test
62 */
63 public function constructorArgumentsAreAvailableAtRuntime() {
64 $path = uniqid();
65 $name = uniqid();
66 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
67 $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
68 $this->assertSame($mockedStorage, $fixture->getStorage());
69 $this->assertStringStartsWith($path, $fixture->getIdentifier());
70 $this->assertEquals($name, $fixture->getName());
71 }
72
73 /**
74 * @test
75 */
76 public function propertiesCanBeUpdated() {
77 $fixture = $this->createFolderFixture('/somePath', 'someName');
78 $fixture->updateProperties(array('identifier' => '/someOtherPath', 'name' => 'someNewName'));
79 $this->assertEquals('someNewName', $fixture->getName());
80 $this->assertEquals('/someOtherPath', $fixture->getIdentifier());
81 }
82
83 /**
84 * @test
85 */
86 public function propertiesAreNotUpdatedIfNotSetInInput() {
87 $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
88 $fixture->updateProperties(array('identifier' => '/someOtherPath'));
89 $this->assertEquals('someName', $fixture->getName());
90 }
91
92 /**
93 * @test
94 */
95 public function getFilesReturnsArrayWithFilenamesAsKeys() {
96 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
97 $mockedStorage->expects($this->once())->method('getFileList')->will($this->returnValue(array(
98 'somefile.png' => array(
99 'name' => 'somefile.png'
100 ),
101 'somefile.jpg' => array(
102 'name' => 'somefile.jpg'
103 )
104 )
105 ));
106 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
107
108 $fileList = $fixture->getFiles();
109
110 $this->assertEquals(array('somefile.png', 'somefile.jpg'), array_keys($fileList));
111 }
112
113 /**
114 * @test
115 */
116 public function getFilesHandsOverRecursiveFALSEifNotExplicitlySet() {
117 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
118 $mockedStorage
119 ->expects($this->once())
120 ->method('getFileList')
121 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
122 ->will($this->returnValue(array()));
123
124 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
125 $fixture->getFiles();
126 }
127
128 /**
129 * @test
130 */
131 public function getFilesHandsOverRecursiveTRUEifSet() {
132 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
133 $mockedStorage
134 ->expects($this->once())
135 ->method('getFileList')
136 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
137 ->will($this->returnValue(array()));
138
139 $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
140 $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, TRUE);
141 }
142
143 /**
144 * @test
145 */
146 public function getSubfolderCallsFactoryWithCorrectArguments() {
147 $mockedStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
148 $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(TRUE));
149 $mockedFactory = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
150 $mockedFactory->expects($this->once())->method('createFolderObject')->with($mockedStorage, '/somePath/someFolder/someSubfolder/', 'someSubfolder');
151 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $mockedFactory);
152 $fixture = $this->createFolderFixture('/somePath/someFolder/', 'someFolder', $mockedStorage);
153 $fixture->getSubfolder('someSubfolder');
154 }
155
156 }
157
158 ?>