‪TYPO3CMS  11.5
StorageRepositoryTest.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 Prophecy\PhpUnit\ProphecyTrait;
21 use Psr\EventDispatcher\EventDispatcherInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪StorageRepositoryTest extends UnitTestCase
32 {
33  use ProphecyTrait;
34 
35  /**********************************
36  * Drivers
37  **********************************/
41  public function ‪getDriverObjectAcceptsDriverClassName(): void
42  {
43  $mockedDriver = $this->getMockForAbstractClass(AbstractDriver::class);
44  $driverFixtureClass = get_class($mockedDriver);
45  $registry = new ‪DriverRegistry();
46  $registry->registerDriverClass($driverFixtureClass);
47  $subject = $this->getAccessibleMock(
48  StorageRepository::class,
49  ['dummy'],
50  [
51  $this->prophesize(EventDispatcherInterface::class)->reveal(),
52  $registry,
53  ]
54  );
55  $obj = $subject->_call('getDriverObject', $driverFixtureClass, []);
56  self::assertInstanceOf(AbstractDriver::class, $obj);
57  }
58 
59  /***********************************
60  * Storage AutoDetection
61  ***********************************/
62 
70  public function ‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId): void
71  {
72  $subject = new ‪StorageRepository(
73  $this->prophesize(EventDispatcherInterface::class)->reveal(),
74  $this->prophesize(DriverRegistry::class)->reveal()
75  );
76  $mock = \Closure::bind(static function (‪StorageRepository $storageRepository) use (&$path, $storageConfiguration) {
77  $storageRepository->localDriverStorageCache = $storageConfiguration;
78  return $storageRepository->‪findBestMatchingStorageByLocalPath($path);
79  }, null, StorageRepository::class);
80  self::assertSame($expectedStorageId, $mock($subject));
81  }
82 
86  public function ‪storageDetectionDataProvider(): array
87  {
88  return [
89  'NoLocalStoragesReturnDefaultStorage' => [
90  [],
91  'my/dummy/Image.png',
92  0,
93  ],
94  'NoMatchReturnsDefaultStorage' => [
95  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin2/public/']),
96  'my/dummy/Image.png',
97  0,
98  ],
99  'MatchReturnsTheMatch' => [
100  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'other/public/']),
101  'fileadmin/dummy/Image.png',
102  1,
103  ],
104  'TwoFoldersWithSameStartReturnsCorrect' => [
105  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
106  'fileadmin/dummy/Image.png',
107  1,
108  ],
109  'NestedStorageReallyReturnsTheBestMatching' => [
110  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
111  'fileadmin/public/Image.png',
112  2,
113  ],
114  'CommonPrefixButWrongPath' => [
115  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
116  'uploads/bogus/dummy.png',
117  0,
118  ],
119  'CommonPrefixRightPath' => [
120  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
121  'uploads/test/dummy.png',
122  2,
123  ],
124  'FindStorageFromWindowsPath' => [
125  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
126  'uploads\\test\\dummy.png',
127  2,
128  ],
129  ];
130  }
131 
132  private function ‪asRelativePath(string $value): ‪LocalPath
133  {
134  return new ‪LocalPath($value, ‪LocalPath::TYPE_RELATIVE);
135  }
136 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\getDriverObjectAcceptsDriverClassName
‪getDriverObjectAcceptsDriverClassName()
Definition: StorageRepositoryTest.php:40
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Resource\StorageRepository\findBestMatchingStorageByLocalPath
‪int findBestMatchingStorageByLocalPath(&$localPath)
Definition: StorageRepository.php:390
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest
Definition: StorageRepositoryTest.php:32
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry
Definition: DriverRegistry.php:24
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\storageDetectionDataProvider
‪array storageDetectionDataProvider()
Definition: StorageRepositoryTest.php:85
‪TYPO3\CMS\Core\Resource\Driver\AbstractDriver
Definition: AbstractDriver.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\asRelativePath
‪asRelativePath(string $value)
Definition: StorageRepositoryTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound
‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
Definition: StorageRepositoryTest.php:69