16378b85c299e0b7595800bdf6ad46863b831e7c
[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 \TYPO3\CMS\Core\Imaging\IconRegistry
45 */
46 protected $iconRegistryMock;
47
48 /**
49 * Set up
50 *
51 * @return void
52 */
53 protected function setUp() {
54 $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
55 $this->subject = new IconFactory($this->iconRegistryMock->reveal());
56
57 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(TRUE);
58 $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn([
59 'provider' => FontawesomeIconProvider::class,
60 'options' => array(
61 'name' => 'times',
62 'additionalClasses' => 'fa-fw'
63 )
64 ]);
65 }
66
67 /**
68 * DataProvider for icon sizes
69 *
70 * @return array
71 */
72 public function differentSizesDataProvider() {
73 return [
74 ['size ' . Icon::SIZE_SMALL => ['input' => Icon::SIZE_SMALL, 'expected' => Icon::SIZE_SMALL]],
75 ['size ' . Icon::SIZE_DEFAULT => ['input' => Icon::SIZE_DEFAULT, 'expected' => Icon::SIZE_DEFAULT]],
76 ['size ' . Icon::SIZE_LARGE => ['input' => Icon::SIZE_LARGE, 'expected' => Icon::SIZE_LARGE]]
77 ];
78 }
79
80 /**
81 * @test
82 */
83 public function getIconReturnsIconWithCorrectMarkupWrapperIfRegisteredIconIdentifierIsUsed() {
84 $this->assertContains('<span class="icon-markup">',
85 $this->subject->getIcon($this->registeredIconIdentifier)->render());
86 }
87
88 /**
89 * @test
90 */
91 public function getIconByIdentifierReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed() {
92 $this->assertContains('<span class="icon icon-size-default icon-actions-document-close">',
93 $this->subject->getIcon($this->registeredIconIdentifier)->render());
94 }
95
96 /**
97 * @test
98 * @dataProvider differentSizesDataProvider
99 */
100 public function getIconByIdentifierAndSizeReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed($size) {
101 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-actions-document-close">',
102 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'])->render());
103 }
104
105 /**
106 * @test
107 * @dataProvider differentSizesDataProvider
108 */
109 public function getIconByIdentifierAndSizeAndWithOverlayReturnsIconWithCorrectOverlayMarkupIfRegisteredIconIdentifierIsUsed($size) {
110 $this->assertContains('<span class="icon-overlay icon-overlay-read-only">',
111 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'], 'overlay-read-only')->render());
112 }
113
114 /**
115 * @test
116 */
117 public function getIconReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed() {
118 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
119 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
120 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
121 'provider' => FontawesomeIconProvider::class,
122 'options' => array(
123 'name' => 'times-circle',
124 'additionalClasses' => 'fa-fw'
125 )
126 ]);
127 $this->assertContains('<span class="icon icon-size-default icon-default-not-found">',
128 $this->subject->getIcon($this->notRegisteredIconIdentifier)->render());
129 }
130
131 /**
132 * @test
133 * @dataProvider differentSizesDataProvider
134 */
135 public function getIconByIdentifierAndSizeReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
136 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
137 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
138 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
139 'provider' => FontawesomeIconProvider::class,
140 'options' => array(
141 'name' => 'times-circle',
142 'additionalClasses' => 'fa-fw'
143 )
144 ]);
145 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-default-not-found">',
146 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'])->render());
147 }
148
149 /**
150 * @test
151 * @dataProvider differentSizesDataProvider
152 * @param string $size
153 */
154 public function getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
155 $this->assertContains('<span class="icon-overlay icon-overlay-read-only">',
156 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'], 'overlay-read-only')->render());
157 }
158
159 /**
160 * @test
161 */
162 public function getIconThrowsExceptionIfInvalidSizeIsGiven() {
163 $this->setExpectedException('InvalidArgumentException');
164 $this->subject->getIcon($this->registeredIconIdentifier, 'foo')->render();
165 }
166
167 }