[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Index / ExtractorRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Index;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Class ExtractorRegistryTest
21 */
22 class ExtractorRegistryTest extends UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function registeredExtractorClassCanBeRetrieved()
28 {
29 $extractorClass = 'a9f4d5e4ebb4b03547a2a6094e1170ac';
30 $extractorObject = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
31 ->setMockClassName($extractorClass)
32 ->getMock();
33
34 $extractorRegistry = $this->getMockExtractorRegistry([[$extractorClass, $extractorObject]]);
35
36 $extractorRegistry->registerExtractionService($extractorClass);
37 $this->assertContains($extractorObject, $extractorRegistry->getExtractors(), '', false, false);
38 }
39
40 /**
41 * @test
42 */
43 public function registerExtractorClassThrowsExceptionIfClassDoesNotExist()
44 {
45 $this->expectException(\InvalidArgumentException::class);
46 $this->expectExceptionCode(1422705270);
47
48 $className = 'e1f9aa4e1cd3aa7ff05dcdccb117156a';
49 $extractorRegistry = $this->getMockExtractorRegistry();
50 $extractorRegistry->registerExtractionService($className);
51 }
52
53 /**
54 * @test
55 */
56 public function registerExtractorClassThrowsExceptionIfClassDoesNotImplementRightInterface()
57 {
58 $this->expectException(\InvalidArgumentException::class);
59 $this->expectExceptionCode(1422705271);
60
61 $className = __CLASS__;
62 $extractorRegistry = $this->getMockExtractorRegistry();
63 $extractorRegistry->registerExtractionService($className);
64 }
65
66 /**
67 * @test
68 */
69 public function registerExtractorClassWithHighestPriorityIsFirstInResult()
70 {
71 $extractorClass1 = 'db76010e5c24658c35ea1605cce2391d';
72 $extractorObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
73 ->setMockClassName($extractorClass1)
74 ->getMock();
75 $extractorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
76
77 $extractorClass2 = 'ad9195e2487eea33c8a2abd5cf33cba4';
78 $extractorObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
79 ->setMockClassName($extractorClass2)
80 ->getMock();
81 $extractorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
82
83 $extractorClass3 = 'cef9aa4e1cd3aa7ff05dcdccb117156a';
84 $extractorObject3 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
85 ->setMockClassName($extractorClass3)
86 ->getMock();
87 $extractorObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
88
89 $createdExtractorInstances = [
90 [$extractorClass1, $extractorObject1],
91 [$extractorClass2, $extractorObject2],
92 [$extractorClass3, $extractorObject3],
93 ];
94
95 $extractorRegistry = $this->getMockExtractorRegistry($createdExtractorInstances);
96 $extractorRegistry->registerExtractionService($extractorClass1);
97 $extractorRegistry->registerExtractionService($extractorClass2);
98 $extractorRegistry->registerExtractionService($extractorClass3);
99
100 $extractorInstances = $extractorRegistry->getExtractors();
101
102 $this->assertTrue($extractorInstances[0] instanceof $extractorClass2);
103 $this->assertTrue($extractorInstances[1] instanceof $extractorClass3);
104 $this->assertTrue($extractorInstances[2] instanceof $extractorClass1);
105 }
106
107 /**
108 * @test
109 */
110 public function registeredExtractorClassWithSamePriorityAreAllReturned()
111 {
112 $extractorClass1 = 'b70551b2b2db62b6b15a9bbfcbd50614';
113 $extractorObject1 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
114 ->setMockClassName($extractorClass1)
115 ->getMock();
116 $extractorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
117
118 $extractorClass2 = 'ac318f1659d278b79b38262f23a78d5d';
119 $extractorObject2 = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class)
120 ->setMockClassName($extractorClass2)
121 ->getMock();
122 $extractorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
123
124 $createdExtractorInstances = [
125 [$extractorClass1, $extractorObject1],
126 [$extractorClass2, $extractorObject2],
127 ];
128
129 $extractorRegistry = $this->getMockExtractorRegistry($createdExtractorInstances);
130 $extractorRegistry->registerExtractionService($extractorClass1);
131 $extractorRegistry->registerExtractionService($extractorClass2);
132
133 $extractorInstances = $extractorRegistry->getExtractors();
134 $this->assertContains($extractorObject1, $extractorInstances);
135 $this->assertContains($extractorObject2, $extractorInstances);
136 }
137
138 /**
139 * Initialize an ExtractorRegistry and mock createExtractorInstance()
140 *
141 * @param array $createsExtractorInstances
142 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Resource\Index\ExtractorRegistry
143 */
144 protected function getMockExtractorRegistry(array $createsExtractorInstances = [])
145 {
146 $extractorRegistry = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorRegistry::class)
147 ->setMethods(['createExtractorInstance'])
148 ->getMock();
149
150 if (!empty($createsExtractorInstances)) {
151 $extractorRegistry->expects($this->any())
152 ->method('createExtractorInstance')
153 ->will($this->returnValueMap($createsExtractorInstances));
154 }
155
156 return $extractorRegistry;
157 }
158 }