‪TYPO3CMS  ‪main
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 
21 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪StorageRepositoryTest extends UnitTestCase
28 {
33  {
34  $mockedDriver = $this->createMock(DriverInterface::class);
35  $driverFixtureClass = get_class($mockedDriver);
36  $registry = new ‪DriverRegistry();
37  $registry->registerDriverClass($driverFixtureClass);
38  $subject = $this->getAccessibleMock(
39  StorageRepository::class,
40  null,
41  [
43  $registry,
44  ]
45  );
46  $obj = $subject->_call('getDriverObject', $driverFixtureClass, []);
47  self::assertInstanceOf(DriverInterface::class, $obj);
48  }
49 
50  public static function ‪storageDetectionDataProvider(): array
51  {
52  $asRelativePathClosure = fn($value) => new ‪LocalPath($value, ‪LocalPath::TYPE_RELATIVE);
53  return [
54  'NoLocalStoragesReturnDefaultStorage' => [
55  [],
56  'my/dummy/Image.png',
57  0,
58  ],
59  'NoMatchReturnsDefaultStorage' => [
60  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'fileadmin2/public/']),
61  'my/dummy/Image.png',
62  0,
63  ],
64  'MatchReturnsTheMatch' => [
65  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'other/public/']),
66  'fileadmin/dummy/Image.png',
67  1,
68  ],
69  'TwoFoldersWithSameStartReturnsCorrect' => [
70  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
71  'fileadmin/dummy/Image.png',
72  1,
73  ],
74  'NestedStorageReallyReturnsTheBestMatching' => [
75  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
76  'fileadmin/public/Image.png',
77  2,
78  ],
79  'CommonPrefixButWrongPath' => [
80  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'uploads/test/']),
81  'uploads/bogus/dummy.png',
82  0,
83  ],
84  'CommonPrefixRightPath' => [
85  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'uploads/test/']),
86  'uploads/test/dummy.png',
87  2,
88  ],
89  'FindStorageFromWindowsPath' => [
90  array_map($asRelativePathClosure, [1 => 'fileadmin/', 2 => 'uploads/test/']),
91  'uploads\\test\\dummy.png',
92  2,
93  ],
94  ];
95  }
96 
101  public function ‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, string $path, int $expectedStorageId): void
102  {
103  $subject = new ‪StorageRepository(
105  $this->createMock(DriverRegistry::class)
106  );
107  $mock = \Closure::bind(static function (‪StorageRepository $storageRepository) use (&$path, $storageConfiguration) {
108  $storageRepository->localDriverStorageCache = $storageConfiguration;
109  return $storageRepository->findBestMatchingStorageByLocalPath($path);
110  }, null, StorageRepository::class);
111  self::assertSame($expectedStorageId, $mock($subject));
112  }
113 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\storageDetectionDataProvider
‪static storageDetectionDataProvider()
Definition: StorageRepositoryTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\getDriverObjectAcceptsDriverClassName
‪getDriverObjectAcceptsDriverClassName()
Definition: StorageRepositoryTest.php:32
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:30
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest
Definition: StorageRepositoryTest.php:28
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry
Definition: DriverRegistry.php:24
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:28
‪TYPO3\CMS\Core\Tests\Unit\Resource\StorageRepositoryTest\findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound
‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, string $path, int $expectedStorageId)
Definition: StorageRepositoryTest.php:101