[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / TextExtraction / TextExtractorRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\TextExtraction;
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\Resource\TextExtraction\TextExtractorInterface;
18 use TYPO3\CMS\Core\Resource\TextExtraction\TextExtractorRegistry;
19
20 /**
21 * Test cases for TextExtractorRegistry
22 */
23 class TextExtractorRegistryTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
24 {
25 /**
26 * Initialize a TextExtractorRegistry and mock createTextExtractorInstance()
27 *
28 * @param array $createsTextExtractorInstances
29 * @return \PHPUnit_Framework_MockObject_MockObject|TextExtractorRegistry
30 */
31 protected function getTextExtractorRegistry(array $createsTextExtractorInstances = [])
32 {
33 $textExtractorRegistry = $this->getMockBuilder(TextExtractorRegistry::class)
34 ->setMethods(['createTextExtractorInstance'])
35 ->getMock();
36
37 if (!empty($createsTextExtractorInstances)) {
38 $textExtractorRegistry->expects($this->any())
39 ->method('createTextExtractorInstance')
40 ->will($this->returnValueMap($createsTextExtractorInstances));
41 }
42
43 return $textExtractorRegistry;
44 }
45
46 /**
47 * @test
48 */
49 public function registeredTextExtractorClassCanBeRetrieved()
50 {
51 $textExtractorClass = $this->getUniqueId('myTextExtractor');
52 $textExtractorInstance = $this->getMockBuilder(TextExtractorInterface::class)
53 ->setMockClassName($textExtractorClass)
54 ->getMock();
55
56 $textExtractorRegistry = $this->getTextExtractorRegistry([[$textExtractorClass, $textExtractorInstance]]);
57
58 $textExtractorRegistry->registerTextExtractor($textExtractorClass);
59 $this->assertContains($textExtractorInstance, $textExtractorRegistry->getTextExtractorInstances(), '', false, false);
60 }
61
62 /**
63 * @test
64 */
65 public function registerTextExtractorThrowsExceptionIfClassDoesNotExist()
66 {
67 $this->expectException(\InvalidArgumentException::class);
68 $this->expectExceptionCode(1422906893);
69
70 $textExtractorRegistry = $this->getTextExtractorRegistry();
71 $textExtractorRegistry->registerTextExtractor($this->getUniqueId());
72 }
73
74 /**
75 * @test
76 */
77 public function registerTextExtractorThrowsExceptionIfClassDoesNotImplementRightInterface()
78 {
79 $this->expectException(\InvalidArgumentException::class);
80 $this->expectExceptionCode(1422771427);
81
82 $textExtractorRegistry = $this->getTextExtractorRegistry();
83 $textExtractorRegistry->registerTextExtractor(__CLASS__);
84 }
85 }