‪TYPO3CMS  11.5
BaseTestCase.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 org\bovigo\vfs\vfsStream;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 abstract class ‪BaseTestCase extends UnitTestCase
31 {
32  protected string ‪$basedir = 'basedir';
33  protected ?string ‪$mountDir;
34  protected array ‪$vfsContents = [];
35 
36  protected function ‪setUp(): void
37  {
38  parent::setUp();
39  $this->mountDir = ‪StringUtility::getUniqueId('mount-');
40  $this->basedir = ‪StringUtility::getUniqueId('base-');
41  vfsStream::setup($this->basedir);
42  // Add an entry for the mount directory to the VFS contents
43  $this->vfsContents = [$this->mountDir => []];
44  }
45 
46  protected function ‪getMountRootUrl(): string
47  {
48  return $this->‪getUrlInMount('');
49  }
50 
51  protected function ‪mergeToVfsContents($contents): void
52  {
53  ‪ArrayUtility::mergeRecursiveWithOverrule($this->vfsContents, $contents);
54  }
55 
56  protected function ‪initializeVfs(): void
57  {
58  vfsStream::create($this->vfsContents);
59  }
60 
66  protected function ‪addToMount(array $dirStructure): void
67  {
68  $this->‪mergeToVfsContents([$this->mountDir => $dirStructure]);
69  }
70 
74  protected function ‪getUrlInMount(string $path): string
75  {
76  return vfsStream::url($this->basedir . '/' . $this->mountDir . '/' . ltrim($path, '/'));
77  }
78 
84  protected function ‪addToVfs(array $dirStructure): void
85  {
86  $this->‪mergeToVfsContents($dirStructure);
87  }
88 
92  protected function ‪getUrl(string $path): string
93  {
94  return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
95  }
96 
101  protected function ‪_createFileFolderMock(string $type, string $identifier, array $mockedMethods)
102  {
103  if (!in_array('getIdentifier', $mockedMethods, true)) {
104  $mockedMethods[] = 'getIdentifier';
105  }
106  if (!in_array('getName', $mockedMethods, true)) {
107  $mockedMethods[] = 'getName';
108  }
109 
110  $mock = $this->getMockBuilder($type)
111  ->onlyMethods($mockedMethods)
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $mock->method('getIdentifier')->willReturn($identifier);
116  $mock->method('getName')->willReturn(basename($identifier));
117  return $mock;
118  }
119 
127  protected function ‪getSimpleFileMock(string $identifier, array $mockedMethods = [])
128  {
129  return $this->‪_createFileFolderMock(File::class, $identifier, $mockedMethods);
130  }
131 
139  protected function ‪getSimpleFolderMock(string $identifier, array $mockedMethods = [])
140  {
141  return $this->‪_createFileFolderMock(Folder::class, $identifier, $mockedMethods);
142  }
143 
153  protected function ‪getFolderMock(string $identifier, array $mockedMethods = [], array $subfolders = [], array $files = [])
154  {
155  $folder = $this->‪_createFileFolderMock(Folder::class, $identifier, array_merge($mockedMethods, ['getFiles', 'getSubfolders']));
156  $folder->method('getSubfolders')->willReturn($subfolders);
157  $folder->method('getFiles')->willReturn($files);
158  return $folder;
159  }
160 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getFolderMock
‪TYPO3 CMS Core Resource File TYPO3 CMS Core Resource Folder getFolderMock(string $identifier, array $mockedMethods=[], array $subfolders=[], array $files=[])
Definition: BaseTestCase.php:153
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$vfsContents
‪array $vfsContents
Definition: BaseTestCase.php:34
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase
Definition: BaseTestCase.php:31
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\addToVfs
‪addToVfs(array $dirStructure)
Definition: BaseTestCase.php:84
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getSimpleFolderMock
‪TYPO3 CMS Core Resource Folder getSimpleFolderMock(string $identifier, array $mockedMethods=[])
Definition: BaseTestCase.php:139
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getSimpleFileMock
‪TYPO3 CMS Core Resource File PHPUnit Framework MockObject MockObject getSimpleFileMock(string $identifier, array $mockedMethods=[])
Definition: BaseTestCase.php:127
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$basedir
‪string $basedir
Definition: BaseTestCase.php:32
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\_createFileFolderMock
‪TYPO3 CMS Core Resource File TYPO3 CMS Core Resource Folder _createFileFolderMock(string $type, string $identifier, array $mockedMethods)
Definition: BaseTestCase.php:101
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getMountRootUrl
‪getMountRootUrl()
Definition: BaseTestCase.php:46
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$mountDir
‪string $mountDir
Definition: BaseTestCase.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\setUp
‪setUp()
Definition: BaseTestCase.php:36
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\mergeToVfsContents
‪mergeToVfsContents($contents)
Definition: BaseTestCase.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getUrlInMount
‪getUrlInMount(string $path)
Definition: BaseTestCase.php:74
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\addToMount
‪addToMount(array $dirStructure)
Definition: BaseTestCase.php:66
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\initializeVfs
‪initializeVfs()
Definition: BaseTestCase.php:56
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getUrl
‪getUrl(string $path)
Definition: BaseTestCase.php:92