[FEATURE] Add spinning feature for icons
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Imaging / IconFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Imaging;
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 Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Imaging\IconProvider\FontawesomeIconProvider;
22
23 /**
24 * Testcase for \TYPO3\CMS\Core\Imaging\IconFactory
25 */
26 class IconFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
27
28 /**
29 * @var \TYPO3\CMS\Core\Imaging\IconFactory
30 */
31 protected $subject = NULL;
32
33 /**
34 * @var string
35 */
36 protected $notRegisteredIconIdentifier = 'my-super-unregistered-identifier';
37
38 /**
39 * @var string
40 */
41 protected $registeredIconIdentifier = 'actions-document-close';
42
43 /**
44 * @var string
45 */
46 protected $registeredSpinningIconIdentifier = 'spinning-icon';
47
48 /**
49 * @var \TYPO3\CMS\Core\Imaging\IconRegistry
50 */
51 protected $iconRegistryMock;
52
53 /**
54 * Set up
55 *
56 * @return void
57 */
58 protected function setUp() {
59 $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
60 $this->subject = new IconFactory($this->iconRegistryMock->reveal());
61
62 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(TRUE);
63 $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn([
64 'provider' => FontawesomeIconProvider::class,
65 'options' => array(
66 'name' => 'times',
67 'additionalClasses' => 'fa-fw'
68 )
69 ]);
70 }
71
72 /**
73 * DataProvider for icon sizes
74 *
75 * @return array
76 */
77 public function differentSizesDataProvider() {
78 return [
79 ['size ' . Icon::SIZE_SMALL => ['input' => Icon::SIZE_SMALL, 'expected' => Icon::SIZE_SMALL]],
80 ['size ' . Icon::SIZE_DEFAULT => ['input' => Icon::SIZE_DEFAULT, 'expected' => Icon::SIZE_DEFAULT]],
81 ['size ' . Icon::SIZE_LARGE => ['input' => Icon::SIZE_LARGE, 'expected' => Icon::SIZE_LARGE]]
82 ];
83 }
84
85 /**
86 * @test
87 */
88 public function getIconReturnsIconWithCorrectMarkupWrapperIfRegisteredIconIdentifierIsUsed() {
89 $this->assertContains('<span class="icon-markup">',
90 $this->subject->getIcon($this->registeredIconIdentifier)->render());
91 }
92
93 /**
94 * @test
95 */
96 public function getIconByIdentifierReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed() {
97 $this->assertContains('<span class="icon icon-size-default icon-actions-document-close">',
98 $this->subject->getIcon($this->registeredIconIdentifier)->render());
99 }
100
101 /**
102 * @test
103 * @dataProvider differentSizesDataProvider
104 */
105 public function getIconByIdentifierAndSizeReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed($size) {
106 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-actions-document-close">',
107 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'])->render());
108 }
109
110 /**
111 * @test
112 * @dataProvider differentSizesDataProvider
113 */
114 public function getIconByIdentifierAndSizeAndWithOverlayReturnsIconWithCorrectOverlayMarkupIfRegisteredIconIdentifierIsUsed($size) {
115 $this->assertContains('<span class="icon-overlay icon-overlay-read-only">',
116 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'], 'overlay-read-only')->render());
117 }
118
119 /**
120 * @test
121 */
122 public function getIconReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed() {
123 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
124 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
125 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
126 'provider' => FontawesomeIconProvider::class,
127 'options' => array(
128 'name' => 'times-circle',
129 'additionalClasses' => 'fa-fw'
130 )
131 ]);
132 $this->assertContains('<span class="icon icon-size-default icon-default-not-found">',
133 $this->subject->getIcon($this->notRegisteredIconIdentifier)->render());
134 }
135
136 /**
137 * @test
138 * @dataProvider differentSizesDataProvider
139 */
140 public function getIconByIdentifierAndSizeReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
141 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
142 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
143 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
144 'provider' => FontawesomeIconProvider::class,
145 'options' => array(
146 'name' => 'times-circle',
147 'additionalClasses' => 'fa-fw'
148 )
149 ]);
150 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-default-not-found">',
151 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'])->render());
152 }
153
154 /**
155 * @test
156 */
157 public function getIconReturnsCorrectMarkupIfIconIsRegisteredAsSpinningIcon() {
158 $this->iconRegistryMock->getIconConfigurationByIdentifier($this->registeredSpinningIconIdentifier)->willReturn([
159 'provider' => FontawesomeIconProvider::class,
160 'options' => array(
161 'name' => 'times-circle',
162 'additionalClasses' => 'fa-fw',
163 'spinning' => TRUE
164 )
165 ]);
166 $this->assertContains('<span class="icon icon-size-default icon-' . $this->registeredSpinningIconIdentifier . ' icon-spin">',
167 $this->subject->getIcon($this->registeredSpinningIconIdentifier)->render());
168 }
169
170 /**
171 * @test
172 * @dataProvider differentSizesDataProvider
173 * @param string $size
174 */
175 public function getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
176 $this->assertContains('<span class="icon-overlay icon-overlay-read-only">',
177 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'], 'overlay-read-only')->render());
178 }
179
180 /**
181 * @test
182 */
183 public function getIconThrowsExceptionIfInvalidSizeIsGiven() {
184 $this->setExpectedException('InvalidArgumentException');
185 $this->subject->getIcon($this->registeredIconIdentifier, 'foo')->render();
186 }
187
188 }