‪TYPO3CMS  10.4
BaseTestCase.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;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 abstract class ‪BaseTestCase extends UnitTestCase
29 {
33  protected ‪$basedir = 'basedir';
34 
35  protected ‪$mountDir;
36 
37  protected ‪$vfsContents = [];
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  $this->mountDir = ‪StringUtility::getUniqueId('mount-');
43  $this->basedir = ‪StringUtility::getUniqueId('base-');
44  vfsStream::setup($this->basedir);
45  // Add an entry for the mount directory to the VFS contents
46  $this->vfsContents = [$this->mountDir => []];
47  }
48 
49  protected function ‪getMountRootUrl()
50  {
51  return $this->‪getUrlInMount('');
52  }
53 
54  protected function ‪mergeToVfsContents($contents)
55  {
56  ‪ArrayUtility::mergeRecursiveWithOverrule($this->vfsContents, $contents);
57  }
58 
59  protected function ‪initializeVfs()
60  {
61  vfsStream::create($this->vfsContents);
62  }
63 
69  protected function ‪addToMount(array $dirStructure)
70  {
71  $this->‪mergeToVfsContents([$this->mountDir => $dirStructure]);
72  }
73 
80  protected function ‪getUrlInMount($path)
81  {
82  return vfsStream::url($this->basedir . '/' . $this->mountDir . '/' . ltrim($path, '/'));
83  }
84 
90  protected function ‪addToVfs(array $dirStructure)
91  {
92  $this->‪mergeToVfsContents($dirStructure);
93  }
94 
101  protected function ‪getUrl($path)
102  {
103  return vfsStream::url($this->basedir . '/' . ltrim($path, '/'));
104  }
105 
114  protected function ‪_createFileFolderMock($type, $identifier, $mockedMethods)
115  {
116  if (!empty($mockedMethods)) {
117  if (!in_array('getIdentifier', $mockedMethods)) {
118  $mockedMethods[] = 'getIdentifier';
119  }
120  if (!in_array('getName', $mockedMethods)) {
121  $mockedMethods[] = 'getName';
122  }
123  }
124  $mock = $this->getMockBuilder($type)
125  ->setMethods($mockedMethods)
126  ->disableOriginalConstructor()
127  ->getMock();
128  $mock->expects(self::any())->method('getIdentifier')->willReturn($identifier);
129  $mock->expects(self::any())->method('getName')->willReturn(basename($identifier));
130  return $mock;
131  }
132 
140  protected function ‪getSimpleFileMock($identifier, $mockedMethods = [])
141  {
142  return $this->‪_createFileFolderMock(File::class, $identifier, $mockedMethods);
143  }
144 
152  protected function ‪getSimpleFolderMock($identifier, $mockedMethods = [])
153  {
154  return $this->‪_createFileFolderMock(Folder::class, $identifier, $mockedMethods);
155  }
156 
166  protected function ‪getFolderMock($identifier, $mockedMethods = [], $subfolders = [], $files = [])
167  {
168  $folder = $this->‪_createFileFolderMock(Folder::class, $identifier, array_merge($mockedMethods, ['getFiles', 'getSubfolders']));
169  $folder->expects(self::any())->method('getSubfolders')->willReturn($subfolders);
170  $folder->expects(self::any())->method('getFiles')->willReturn($files);
171  return $folder;
172  }
173 }
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase
Definition: BaseTestCase.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getFolderMock
‪TYPO3 CMS Core Resource File TYPO3 CMS Core Resource Folder getFolderMock($identifier, $mockedMethods=[], $subfolders=[], $files=[])
Definition: BaseTestCase.php:165
‪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:89
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$mountDir
‪$mountDir
Definition: BaseTestCase.php:34
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\_createFileFolderMock
‪TYPO3 CMS Core Resource File TYPO3 CMS Core Resource Folder _createFileFolderMock($type, $identifier, $mockedMethods)
Definition: BaseTestCase.php:113
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$basedir
‪string $basedir
Definition: BaseTestCase.php:32
‪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:48
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\$vfsContents
‪$vfsContents
Definition: BaseTestCase.php:36
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getUrlInMount
‪string getUrlInMount($path)
Definition: BaseTestCase.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getSimpleFolderMock
‪TYPO3 CMS Core Resource Folder getSimpleFolderMock($identifier, $mockedMethods=[])
Definition: BaseTestCase.php:151
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\setUp
‪setUp()
Definition: BaseTestCase.php:38
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\mergeToVfsContents
‪mergeToVfsContents($contents)
Definition: BaseTestCase.php:53
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getUrl
‪string getUrl($path)
Definition: BaseTestCase.php:100
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\getSimpleFileMock
‪TYPO3 CMS Core Resource File PHPUnit Framework MockObject MockObject getSimpleFileMock($identifier, $mockedMethods=[])
Definition: BaseTestCase.php:139
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\addToMount
‪addToMount(array $dirStructure)
Definition: BaseTestCase.php:68
‪TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase\initializeVfs
‪initializeVfs()
Definition: BaseTestCase.php:58