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