‪TYPO3CMS  10.4
FolderTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use org\bovigo\vfs\vfsStream;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪FolderTest extends UnitTestCase
30 {
34  protected ‪$resetSingletonInstances = true;
35 
36  protected ‪$basedir = 'basedir';
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  vfsStream::setup($this->basedir);
42  }
43 
44  protected function ‪createFolderFixture($path, $name, $mockedStorage = null)
45  {
46  if ($mockedStorage === null) {
47  $mockedStorage = $this->createMock(ResourceStorage::class);
48  }
49  return new ‪Folder($mockedStorage, $path, $name);
50  }
51 
56  {
57  $path = ‪StringUtility::getUniqueId('path_');
58  $name = ‪StringUtility::getUniqueId('name_');
59  $mockedStorage = $this->createMock(ResourceStorage::class);
60  $fixture = $this->‪createFolderFixture($path, $name, $mockedStorage);
61  self::assertSame($mockedStorage, $fixture->getStorage());
62  self::assertStringStartsWith($path, $fixture->getIdentifier());
63  self::assertSame($name, $fixture->getName());
64  }
65 
69  public function ‪propertiesCanBeUpdated()
70  {
71  $fixture = $this->‪createFolderFixture('/somePath', 'someName');
72  $fixture->updateProperties(['identifier' => '/someOtherPath', 'name' => 'someNewName']);
73  self::assertSame('someNewName', $fixture->getName());
74  self::assertSame('/someOtherPath', $fixture->getIdentifier());
75  }
76 
81  {
82  $fixture = $this->‪createFolderFixture('/somePath/someName/', 'someName');
83  $fixture->updateProperties(['identifier' => '/someOtherPath']);
84  self::assertSame('someName', $fixture->getName());
85  }
86 
91  {
92  $mockedStorage = $this->createMock(ResourceStorage::class);
93  $mockedStorage->expects(self::once())->method('getFilesInFolder')->willReturn(
94  [
95  'somefile.png' => [
96  'name' => 'somefile.png'
97  ],
98  'somefile.jpg' => [
99  'name' => 'somefile.jpg'
100  ]
101  ]
102  );
103  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
104 
105  $fileList = $fixture->getFiles();
106 
107  self::assertSame(['somefile.png', 'somefile.jpg'], array_keys($fileList));
108  }
109 
114  {
115  $mockedStorage = $this->createMock(ResourceStorage::class);
116  $mockedStorage
117  ->expects(self::once())
118  ->method('getFilesInFolder')
119  ->with(self::anything(), self::anything(), self::anything(), self::anything(), false)
120  ->willReturn([]);
121 
122  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
123  $fixture->getFiles();
124  }
125 
130  {
131  $mockedStorage = $this->createMock(ResourceStorage::class);
132  $mockedStorage
133  ->expects(self::once())
134  ->method('getFilesInFolder')
135  ->with(self::anything(), self::anything(), self::anything(), self::anything(), true)
136  ->willReturn([]);
137 
138  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
139  $fixture->getFiles(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
140  }
141 
146  {
147  $mockedStorage = $this->createMock(ResourceStorage::class);
148  $mockedStorage->expects(self::once())->method('hasFolderInFolder')->with(self::equalTo('someSubfolder'))->willReturn(true);
150  $mockedFactory = $this->createMock(ResourceFactory::class);
151  $folderFixture = $this->‪createFolderFixture(
152  '/somePath/someFolder/',
153  'someFolder',
154  $mockedStorage
155  );
156  $subfolderFixture = $this->‪createFolderFixture(
157  '/somePath/someSubfolder/',
158  'someSubfolder',
159  $mockedStorage
160  );
161  $mockedStorage->expects(self::once())->method('getFolderInFolder')->willReturn($subfolderFixture);
162  GeneralUtility::setSingletonInstance(
163  ResourceFactory::class,
164  $mockedFactory
165  );
166  self::assertEquals($subfolderFixture, $folderFixture->getSubfolder('someSubfolder'));
167  }
168 
173  {
174  $parentIdentifier = '/parent/';
175  $currentIdentifier = '/parent/current/';
176 
177  $parentFolderFixture = $this->‪createFolderFixture($parentIdentifier, 'parent');
178  $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
179  ->setMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
180  ->disableOriginalConstructor()
181  ->getMock();
182  $mockedStorage->expects(self::once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->willReturn($parentIdentifier);
183  $mockedStorage->expects(self::once())->method('getFolder')->with($parentIdentifier)->willReturn($parentFolderFixture);
184 
185  $currentFolderFixture = $this->‪createFolderFixture($currentIdentifier, 'current', $mockedStorage);
186 
187  self::assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
188  }
189 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\constructorArgumentsAreAvailableAtRuntime
‪constructorArgumentsAreAvailableAtRuntime()
Definition: FolderTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getSubfolderCallsFactoryWithCorrectArguments
‪getSubfolderCallsFactoryWithCorrectArguments()
Definition: FolderTest.php:144
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\propertiesAreNotUpdatedIfNotSetInInput
‪propertiesAreNotUpdatedIfNotSetInInput()
Definition: FolderTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\propertiesCanBeUpdated
‪propertiesCanBeUpdated()
Definition: FolderTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:69
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getParentFolderGetsParentFolderFromStorage
‪getParentFolderGetsParentFolderFromStorage()
Definition: FolderTest.php:171
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesReturnsArrayWithFilenamesAsKeys
‪getFilesReturnsArrayWithFilenamesAsKeys()
Definition: FolderTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesHandsOverRecursiveFALSEifNotExplicitlySet
‪getFilesHandsOverRecursiveFALSEifNotExplicitlySet()
Definition: FolderTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\createFolderFixture
‪createFolderFixture($path, $name, $mockedStorage=null)
Definition: FolderTest.php:43
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\$basedir
‪$basedir
Definition: FolderTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\setUp
‪setUp()
Definition: FolderTest.php:37
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest
Definition: FolderTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesHandsOverRecursiveTRUEifSet
‪getFilesHandsOverRecursiveTRUEifSet()
Definition: FolderTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FolderTest.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22