‪TYPO3CMS  10.4
ExtractorServiceTest.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 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪ExtractorServiceTest extends UnitTestCase
31 {
36  {
37  $fileMock = $this->createMock(File::class);
38  $fileMock->expects(self::any())->method('getType')->willReturn(‪File::FILETYPE_TEXT);
39 
40  $extractorMock = $this->createMock(ExtractorInterface::class);
41  $extractorMock->expects(self::any())->method('getFileTypeRestrictions')->willReturn([‪File::FILETYPE_IMAGE]);
42 
43  $extractorService = new ‪ExtractorService();
44  $method = new \ReflectionMethod($extractorService, 'isFileTypeSupportedByExtractor');
45  $method->setAccessible(true);
46  $arguments = [
47  $fileMock,
48  $extractorMock
49  ];
50 
51  $result = $method->invokeArgs($extractorService, $arguments);
52  self::assertFalse($result);
53  }
54 
59  {
60  $fileMock = $this->createMock(File::class);
61  $fileMock->expects(self::any())->method('getType')->willReturn(‪File::FILETYPE_IMAGE);
62 
63  $extractorMock = $this->createMock(ExtractorInterface::class);
64  $extractorMock->expects(self::any())->method('getFileTypeRestrictions')->willReturn([‪File::FILETYPE_IMAGE]);
65 
66  $extractorService = new ‪ExtractorService();
67  $method = new \ReflectionMethod($extractorService, 'isFileTypeSupportedByExtractor');
68  $method->setAccessible(true);
69  $arguments = [
70  $fileMock,
71  $extractorMock
72  ];
73 
74  $result = $method->invokeArgs($extractorService, $arguments);
75  self::assertTrue($result);
76  }
77 
82  {
83  $fileMock = $this->createMock(File::class);
84  $fileMock->expects(self::any())->method('getType')->willReturn(‪File::FILETYPE_TEXT);
85 
86  $extractorMock = $this->createMock(ExtractorInterface::class);
87  $extractorMock->expects(self::any())->method('getFileTypeRestrictions')->willReturn([]);
88 
89  $extractorService = new ‪ExtractorService();
90  $method = new \ReflectionMethod($extractorService, 'isFileTypeSupportedByExtractor');
91  $method->setAccessible(true);
92  $arguments = [
93  $fileMock,
94  $extractorMock
95  ];
96 
97  $result = $method->invokeArgs($extractorService, $arguments);
98  self::assertTrue($result);
99  }
100 
105  {
106  $storageMock = $this->createMock(ResourceStorage::class);
107  $storageMock->method('getDriverType')->willReturn('Local');
108 
110  $subject = $this->getMockBuilder(ExtractorService::class)
111  ->setMethods(['getExtractorRegistry'])
112  ->getMock()
113  ;
114 
115  $fileMock = $this->createMock(File::class);
116  $fileMock->expects(self::any())->method('getUid')->willReturn(4711);
117  $fileMock->expects(self::any())->method('getType')->willReturn(‪File::FILETYPE_IMAGE);
118  $fileMock->expects(self::any())->method('getStorage')->willReturn($storageMock);
119 
120  $extractorClass1 = md5('1');
121  $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class)
122  ->setMockClassName($extractorClass1)
123  ->getMock();
124 
125  $extractorObject1->expects(self::any())->method('getPriority')->willReturn(10);
126  $extractorObject1->expects(self::any())->method('getExecutionPriority')->willReturn(10);
127  $extractorObject1->expects(self::any())->method('canProcess')->willReturn(true);
128  $extractorObject1->expects(self::any())->method('getFileTypeRestrictions')->willReturn([‪File::FILETYPE_IMAGE]);
129  $extractorObject1->expects(self::any())->method('getDriverRestrictions')->willReturn([$storageMock->getDriverType()]);
130  $extractorObject1->expects(self::any())->method('extractMetaData')->with($fileMock)->willReturn([
131  'width' => 800,
132  'height' => 600,
133  ]);
134 
135  $extractorClass2 = md5('2');
136  $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class)
137  ->setMockClassName($extractorClass2)
138  ->getMock();
139 
140  $extractorObject2->expects(self::any())->method('getPriority')->willReturn(20);
141  $extractorObject2->expects(self::any())->method('getExecutionPriority')->willReturn(20);
142  $extractorObject2->expects(self::any())->method('canProcess')->willReturn(true);
143  $extractorObject2->expects(self::any())->method('getFileTypeRestrictions')->willReturn([‪File::FILETYPE_IMAGE]);
144  $extractorObject2->expects(self::any())->method('getDriverRestrictions')->willReturn([$storageMock->getDriverType()]);
145  $extractorObject2->expects(self::any())->method('extractMetaData')->with($fileMock)->willReturn([
146  'keywords' => 'typo3, cms',
147  ]);
148 
150  $extractorRegistryMock = $this->getMockBuilder(ExtractorRegistry::class)
151  ->setMethods(['createExtractorInstance'])
152  ->getMock();
153 
154  $extractorRegistryMock->expects(self::any())->method('createExtractorInstance')->willReturnMap(
155  [
156  [$extractorClass1, $extractorObject1],
157  [$extractorClass2, $extractorObject2]
158  ]
159  );
160  $extractorRegistryMock->registerExtractionService($extractorClass1);
161  $extractorRegistryMock->registerExtractionService($extractorClass2);
162 
163  $subject->expects(self::any())->method('getExtractorRegistry')->willReturn($extractorRegistryMock);
164 
165  self::assertSame(['width' => 800, 'height' => 600, 'keywords' => 'typo3, cms'], $subject->extractMetaData($fileMock));
166  }
167 }
‪TYPO3\CMS\Core\Resource\Index\ExtractorInterface
Definition: ExtractorInterface.php:25
‪TYPO3\CMS\Core\Resource\Index\ExtractorRegistry
Definition: ExtractorRegistry.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service
Definition: ExtractorServiceTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service\ExtractorServiceTest
Definition: ExtractorServiceTest.php:31
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:78
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service\ExtractorServiceTest\isFileTypeSupportedByExtractorReturnsTrueForFileTypeImageAndExtractorLimitedToFileTypeImage
‪isFileTypeSupportedByExtractorReturnsTrueForFileTypeImageAndExtractorLimitedToFileTypeImage()
Definition: ExtractorServiceTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service\ExtractorServiceTest\extractMetaDataComposesDataByAvailableExtractors
‪extractMetaDataComposesDataByAvailableExtractors()
Definition: ExtractorServiceTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service\ExtractorServiceTest\isFileTypeSupportedByExtractorReturnsTrueForFileTypeTextAndExtractorHasNoFileTypeLimitation
‪isFileTypeSupportedByExtractorReturnsTrueForFileTypeTextAndExtractorHasNoFileTypeLimitation()
Definition: ExtractorServiceTest.php:81
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Service\ExtractorService
Definition: ExtractorService.php:28
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_TEXT
‪const FILETYPE_TEXT
Definition: AbstractFile.php:72
‪TYPO3\CMS\Core\Tests\Unit\Resource\Service\ExtractorServiceTest\isFileTypeSupportedByExtractorReturnsFalseForFileTypeTextAndExtractorLimitedToFileTypeImage
‪isFileTypeSupportedByExtractorReturnsFalseForFileTypeTextAndExtractorLimitedToFileTypeImage()
Definition: ExtractorServiceTest.php:35
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122