[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Driver / DriverRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Driver;
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 /**
18 * Testcase for the FAL driver registry.
19 */
20 class DriverRegistryTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Resource\Driver\DriverRegistry
24 */
25 protected $subject;
26
27 protected function setUp()
28 {
29 $this->initializeSubject();
30 }
31
32 protected function initializeSubject()
33 {
34 $this->subject = new \TYPO3\CMS\Core\Resource\Driver\DriverRegistry();
35 }
36
37 /**
38 * @test
39 */
40 public function registeredDriverClassesCanBeRetrieved()
41 {
42 $className = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class));
43 $this->subject->registerDriverClass($className, 'foobar');
44 $returnedClassName = $this->subject->getDriverClass('foobar');
45 $this->assertEquals($className, $returnedClassName);
46 }
47
48 /**
49 * @test
50 */
51 public function registerDriverClassThrowsExceptionIfClassDoesNotExist()
52 {
53 $this->expectException(\InvalidArgumentException::class);
54 $this->expectExceptionCode(1314979197);
55 $this->subject->registerDriverClass($this->getUniqueId());
56 }
57
58 /**
59 * @test
60 */
61 public function registerDriverClassThrowsExceptionIfShortnameIsAlreadyTakenByAnotherDriverClass()
62 {
63 $this->expectException(\InvalidArgumentException::class);
64 $this->expectExceptionCode(1314979451);
65 $className = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class));
66 $className2 = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\DriverInterface::class));
67 $this->subject->registerDriverClass($className, 'foobar');
68 $this->subject->registerDriverClass($className2, 'foobar');
69 }
70
71 /**
72 * @test
73 */
74 public function getDriverClassThrowsExceptionIfClassIsNotRegistered()
75 {
76 $this->expectException(\InvalidArgumentException::class);
77 $this->expectExceptionCode(1314085990);
78 $this->subject->getDriverClass($this->getUniqueId());
79 }
80
81 /**
82 * @test
83 */
84 public function getDriverClassAcceptsClassNameIfClassIsRegistered()
85 {
86 $className = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class));
87 $this->subject->registerDriverClass($className, 'foobar');
88 $this->assertEquals($className, $this->subject->getDriverClass($className));
89 }
90
91 /**
92 * @test
93 */
94 public function driverRegistryIsInitializedWithPreconfiguredDrivers()
95 {
96 $className = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class));
97 $shortName = $this->getUniqueId();
98 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'] = [
99 $shortName => [
100 'class' => $className
101 ]
102 ];
103 $this->initializeSubject();
104 $this->assertEquals($className, $this->subject->getDriverClass($shortName));
105 }
106
107 /**
108 * @test
109 */
110 public function driverExistsReturnsTrueForAllExistingDrivers()
111 {
112 $className = get_class($this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class));
113 $shortName = $this->getUniqueId();
114 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'] = [
115 $shortName => [
116 'class' => $className
117 ]
118 ];
119 $this->initializeSubject();
120 $this->assertTrue($this->subject->driverExists($shortName));
121 $this->assertFalse($this->subject->driverExists($this->getUniqueId()));
122 }
123
124 /**
125 * @test
126 */
127 public function driverExistsReturnsFalseIfDriverDoesNotExist()
128 {
129 $GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredDrivers'] = [
130 ];
131 $this->initializeSubject();
132 $this->assertFalse($this->subject->driverExists($this->getUniqueId()));
133 }
134 }