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