TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
FolderTest.php
Go to the documentation of this file.
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 
23 {
27  protected $singletonInstances = [];
28 
29  protected $basedir = 'basedir';
30 
31  protected function setUp()
32  {
34  vfsStream::setup($this->basedir);
35  }
36 
37  protected function tearDown()
38  {
40  parent::tearDown();
41  }
42 
43  protected function createFolderFixture($path, $name, $mockedStorage = null)
44  {
45  if ($mockedStorage === null) {
46  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
47  }
48  return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $path, $name, 0);
49  }
50 
55  {
56  $path = $this->getUniqueId();
57  $name = $this->getUniqueId();
58  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
59  $fixture = $this->createFolderFixture($path, $name, $mockedStorage);
60  $this->assertSame($mockedStorage, $fixture->getStorage());
61  $this->assertStringStartsWith($path, $fixture->getIdentifier());
62  $this->assertSame($name, $fixture->getName());
63  }
64 
68  public function propertiesCanBeUpdated()
69  {
70  $fixture = $this->createFolderFixture('/somePath', 'someName');
71  $fixture->updateProperties(['identifier' => '/someOtherPath', 'name' => 'someNewName']);
72  $this->assertSame('someNewName', $fixture->getName());
73  $this->assertSame('/someOtherPath', $fixture->getIdentifier());
74  }
75 
80  {
81  $fixture = $this->createFolderFixture('/somePath/someName/', 'someName');
82  $fixture->updateProperties(['identifier' => '/someOtherPath']);
83  $this->assertSame('someName', $fixture->getName());
84  }
85 
90  {
91  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
92  $mockedStorage->expects($this->once())->method('getFilesInFolder')->will($this->returnValue([
93  'somefile.png' => [
94  'name' => 'somefile.png'
95  ],
96  'somefile.jpg' => [
97  'name' => 'somefile.jpg'
98  ]
99  ]
100  ));
101  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
102 
103  $fileList = $fixture->getFiles();
104 
105  $this->assertSame(['somefile.png', 'somefile.jpg'], array_keys($fileList));
106  }
107 
112  {
113  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
114  $mockedStorage
115  ->expects($this->once())
116  ->method('getFilesInFolder')
117  ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), false)
118  ->will($this->returnValue([]));
119 
120  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
121  $fixture->getFiles();
122  }
123 
128  {
129  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
130  $mockedStorage
131  ->expects($this->once())
132  ->method('getFilesInFolder')
133  ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), true)
134  ->will($this->returnValue([]));
135 
136  $fixture = $this->createFolderFixture('/somePath', 'someName', $mockedStorage);
137  $fixture->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
138  }
139 
143  public function getSubfolderCallsFactoryWithCorrectArguments()
144  {
145  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
146  $mockedStorage->expects($this->once())->method('hasFolderInFolder')->with($this->equalTo('someSubfolder'))->will($this->returnValue(true));
148  $mockedFactory = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
149  $folderFixture = $this->createFolderFixture(
150  '/somePath/someFolder/',
151  'someFolder',
152  $mockedStorage
153  );
154  $subfolderFixture = $this->createFolderFixture(
155  '/somePath/someSubfolder/',
156  'someSubfolder',
157  $mockedStorage
158  );
159  $mockedStorage->expects($this->once())->method('getFolderInFolder')->will($this->returnValue($subfolderFixture));
161  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
162  $mockedFactory
163  );
164  $this->assertEquals($subfolderFixture, $folderFixture->getSubfolder('someSubfolder'));
165  }
166 
171  {
172  $parentIdentifier = '/parent/';
173  $currentIdentifier = '/parent/current/';
174 
175  $parentFolderFixture = $this->createFolderFixture($parentIdentifier, 'parent');
176  $mockedStorage = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\ResourceStorage::class)
177  ->setMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
178  ->disableOriginalConstructor()
179  ->getMock();
180  $mockedStorage->expects($this->once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->will($this->returnValue($parentIdentifier));
181  $mockedStorage->expects($this->once())->method('getFolder')->with($parentIdentifier)->will($this->returnValue($parentFolderFixture));
182 
183  $currentFolderFixture = $this->createFolderFixture($currentIdentifier, 'current', $mockedStorage);
184 
185  $this->assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
186  }
187 }
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)
createFolderFixture($path, $name, $mockedStorage=null)
Definition: FolderTest.php:43