[TASK] Make TYPO3 Core PSR-2 standard compliant
[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\CMS\Core\Tests\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->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass);
31
32 $extractorRegistry = $this->getMockExtractorRegistry(array(array($extractorClass, $extractorObject)));
33
34 $extractorRegistry->registerExtractionService($extractorClass);
35 $this->assertContains($extractorObject, $extractorRegistry->getExtractors(), '', false, false);
36 }
37
38 /**
39 * @test
40 * @expectedException \InvalidArgumentException
41 * @expectedExceptionCode 1422705270
42 */
43 public function registerExtractorClassThrowsExceptionIfClassDoesNotExist()
44 {
45 $className = 'e1f9aa4e1cd3aa7ff05dcdccb117156a';
46 $extractorRegistry = $this->getMockExtractorRegistry();
47 $extractorRegistry->registerExtractionService($className);
48 }
49
50 /**
51 * @test
52 * @expectedException \InvalidArgumentException
53 * @expectedExceptionCode 1422705271
54 */
55 public function registerExtractorClassThrowsExceptionIfClassDoesNotImplementRightInterface()
56 {
57 $className = __CLASS__;
58 $extractorRegistry = $this->getMockExtractorRegistry();
59 $extractorRegistry->registerExtractionService($className);
60 }
61
62 /**
63 * @test
64 */
65 public function registerExtractorClassWithHighestPriorityIsFirstInResult()
66 {
67 $extractorClass1 = 'db76010e5c24658c35ea1605cce2391d';
68 $extractorObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass1);
69 $extractorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
70
71 $extractorClass2 = 'ad9195e2487eea33c8a2abd5cf33cba4';
72 $extractorObject2 = $this->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass2);
73 $extractorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(10));
74
75 $extractorClass3 = 'cef9aa4e1cd3aa7ff05dcdccb117156a';
76 $extractorObject3 = $this->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass3);
77 $extractorObject3->expects($this->any())->method('getPriority')->will($this->returnValue(2));
78
79 $createdExtractorInstances = array(
80 array($extractorClass1, $extractorObject1),
81 array($extractorClass2, $extractorObject2),
82 array($extractorClass3, $extractorObject3),
83 );
84
85 $extractorRegistry = $this->getMockExtractorRegistry($createdExtractorInstances);
86 $extractorRegistry->registerExtractionService($extractorClass1);
87 $extractorRegistry->registerExtractionService($extractorClass2);
88 $extractorRegistry->registerExtractionService($extractorClass3);
89
90 $extractorInstances = $extractorRegistry->getExtractors();
91
92 $this->assertTrue($extractorInstances[0] instanceof $extractorClass2);
93 $this->assertTrue($extractorInstances[1] instanceof $extractorClass3);
94 $this->assertTrue($extractorInstances[2] instanceof $extractorClass1);
95 }
96
97 /**
98 * @test
99 */
100 public function registeredExtractorClassWithSamePriorityAreAllReturned()
101 {
102 $extractorClass1 = 'b70551b2b2db62b6b15a9bbfcbd50614';
103 $extractorObject1 = $this->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass1);
104 $extractorObject1->expects($this->any())->method('getPriority')->will($this->returnValue(1));
105
106 $extractorClass2 = 'ac318f1659d278b79b38262f23a78d5d';
107 $extractorObject2 = $this->getMock(\TYPO3\CMS\Core\Resource\Index\ExtractorInterface::class, array(), array(), $extractorClass2);
108 $extractorObject2->expects($this->any())->method('getPriority')->will($this->returnValue(1));
109
110 $createdExtractorInstances = array(
111 array($extractorClass1, $extractorObject1),
112 array($extractorClass2, $extractorObject2),
113 );
114
115 $extractorRegistry = $this->getMockExtractorRegistry($createdExtractorInstances);
116 $extractorRegistry->registerExtractionService($extractorClass1);
117 $extractorRegistry->registerExtractionService($extractorClass2);
118
119 $extractorInstances = $extractorRegistry->getExtractors();
120 $this->assertContains($extractorObject1, $extractorInstances);
121 $this->assertContains($extractorObject2, $extractorInstances);
122 }
123
124 /**
125 * Initialize an ExtractorRegistry and mock createExtractorInstance()
126 *
127 * @param array $createsExtractorInstances
128 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Resource\Index\ExtractorRegistry
129 */
130 protected function getMockExtractorRegistry(array $createsExtractorInstances = array())
131 {
132 $extractorRegistry = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Index\ExtractorRegistry::class)
133 ->setMethods(array('createExtractorInstance'))
134 ->getMock();
135
136 if (!empty($createsExtractorInstances)) {
137 $extractorRegistry->expects($this->any())
138 ->method('createExtractorInstance')
139 ->will($this->returnValueMap($createsExtractorInstances));
140 }
141
142 return $extractorRegistry;
143 }
144 }