‪TYPO3CMS  ‪main
FolderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪FolderTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
32  protected function ‪createFolderFixture($path, $name, $mockedStorage = null): ‪Folder
33  {
34  if ($mockedStorage === null) {
35  $mockedStorage = $this->createMock(ResourceStorage::class);
36  }
37  return new ‪Folder($mockedStorage, $path, $name);
38  }
39 
40  #[Test]
42  {
43  $path = ‪StringUtility::getUniqueId('path_');
44  $name = ‪StringUtility::getUniqueId('name_');
45  $mockedStorage = $this->createMock(ResourceStorage::class);
46  $fixture = $this->‪createFolderFixture($path, $name, $mockedStorage);
47  self::assertSame($mockedStorage, $fixture->getStorage());
48  self::assertStringStartsWith($path, $fixture->getIdentifier());
49  self::assertSame($name, $fixture->getName());
50  }
51 
52  #[Test]
53  public function ‪propertiesCanBeUpdated(): void
54  {
55  $fixture = $this->‪createFolderFixture('/somePath', 'someName');
56  $fixture->updateProperties(['identifier' => '/someOtherPath', 'name' => 'someNewName']);
57  self::assertSame('someNewName', $fixture->getName());
58  self::assertSame('/someOtherPath', $fixture->getIdentifier());
59  }
60 
61  #[Test]
63  {
64  $fixture = $this->‪createFolderFixture('/somePath/someName/', 'someName');
65  $fixture->updateProperties(['identifier' => '/someOtherPath']);
66  self::assertSame('someName', $fixture->getName());
67  }
68 
69  #[Test]
71  {
72  $mockedStorage = $this->createMock(ResourceStorage::class);
73  $mockedStorage->expects(self::once())->method('getFilesInFolder')->willReturn(
74  [
75  'somefile.png' => [
76  'name' => 'somefile.png',
77  ],
78  'somefile.jpg' => [
79  'name' => 'somefile.jpg',
80  ],
81  ]
82  );
83  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
84 
85  $fileList = $fixture->getFiles();
86 
87  self::assertSame(['somefile.png', 'somefile.jpg'], array_keys($fileList));
88  }
89 
90  #[Test]
92  {
93  $mockedStorage = $this->createMock(ResourceStorage::class);
94  $mockedStorage
95  ->expects(self::once())
96  ->method('getFilesInFolder')
97  ->with(self::anything(), self::anything(), self::anything(), self::anything(), false)
98  ->willReturn([]);
99 
100  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
101  $fixture->getFiles();
102  }
103 
104  #[Test]
106  {
107  $mockedStorage = $this->createMock(ResourceStorage::class);
108  $mockedStorage
109  ->expects(self::once())
110  ->method('getFilesInFolder')
111  ->with(self::anything(), self::anything(), self::anything(), self::anything(), true)
112  ->willReturn([]);
113 
114  $fixture = $this->‪createFolderFixture('/somePath', 'someName', $mockedStorage);
115  $fixture->getFiles(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true);
116  }
117 
118  #[Test]
120  {
121  $mockedStorage = $this->createMock(ResourceStorage::class);
122  $mockedStorage->expects(self::once())->method('hasFolderInFolder')->with(self::equalTo('someSubfolder'))->willReturn(true);
123 
124  $mockedFactory = $this->createMock(ResourceFactory::class);
125  $folderFixture = $this->‪createFolderFixture(
126  '/somePath/someFolder/',
127  'someFolder',
128  $mockedStorage
129  );
130  $subfolderFixture = $this->‪createFolderFixture(
131  '/somePath/someSubfolder/',
132  'someSubfolder',
133  $mockedStorage
134  );
135  $mockedStorage->expects(self::once())->method('getFolderInFolder')->willReturn($subfolderFixture);
136  GeneralUtility::setSingletonInstance(
137  ResourceFactory::class,
138  $mockedFactory
139  );
140  self::assertEquals($subfolderFixture, $folderFixture->getSubfolder('someSubfolder'));
141  }
142 
143  #[Test]
145  {
146  $parentIdentifier = '/parent/';
147  $currentIdentifier = '/parent/current/';
148 
149  $parentFolderFixture = $this->‪createFolderFixture($parentIdentifier, 'parent');
150  $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
151  ->onlyMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
152  ->disableOriginalConstructor()
153  ->getMock();
154  $mockedStorage->expects(self::once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->willReturn($parentIdentifier);
155  $mockedStorage->expects(self::once())->method('getFolder')->with($parentIdentifier)->willReturn($parentFolderFixture);
156 
157  $currentFolderFixture = $this->‪createFolderFixture($currentIdentifier, 'current', $mockedStorage);
158 
159  self::assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
160  }
161 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\constructorArgumentsAreAvailableAtRuntime
‪constructorArgumentsAreAvailableAtRuntime()
Definition: FolderTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getSubfolderCallsFactoryWithCorrectArguments
‪getSubfolderCallsFactoryWithCorrectArguments()
Definition: FolderTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\propertiesAreNotUpdatedIfNotSetInInput
‪propertiesAreNotUpdatedIfNotSetInInput()
Definition: FolderTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\propertiesCanBeUpdated
‪propertiesCanBeUpdated()
Definition: FolderTest.php:53
‪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:70
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getParentFolderGetsParentFolderFromStorage
‪getParentFolderGetsParentFolderFromStorage()
Definition: FolderTest.php:144
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesReturnsArrayWithFilenamesAsKeys
‪getFilesReturnsArrayWithFilenamesAsKeys()
Definition: FolderTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesHandsOverRecursiveFALSEifNotExplicitlySet
‪getFilesHandsOverRecursiveFALSEifNotExplicitlySet()
Definition: FolderTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\createFolderFixture
‪createFolderFixture($path, $name, $mockedStorage=null)
Definition: FolderTest.php:32
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:128
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest
Definition: FolderTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\getFilesHandsOverRecursiveTRUEifSet
‪getFilesHandsOverRecursiveTRUEifSet()
Definition: FolderTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Resource\FolderTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FolderTest.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57