1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfBanners\Test\Unit\Service; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Extension "sf_banners" for TYPO3 CMS. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please read the |
8
|
|
|
* LICENSE.txt file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use DERHANSEN\SfBanners\Domain\Model\Banner; |
12
|
|
|
use DERHANSEN\SfBanners\Domain\Model\BannerDemand; |
13
|
|
|
use DERHANSEN\SfBanners\Service\BannerService; |
14
|
|
|
use Nimut\TestingFramework\TestCase\UnitTestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Test cases for the banner service |
18
|
|
|
*/ |
19
|
|
|
class BannerServiceTest extends UnitTestCase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \DERHANSEN\SfBanners\Service\BannerService |
24
|
|
|
*/ |
25
|
|
|
protected $bannerService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand |
29
|
|
|
*/ |
30
|
|
|
protected $demand; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set up |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
$this->bannerService = new BannerService(); |
41
|
|
|
$this->demand = new BannerDemand(); |
42
|
|
|
$this->demand->setDisplayMode('all'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tear down |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function tearDown() |
51
|
|
|
{ |
52
|
|
|
unset($this->bannerService, $this->demand); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test if additional css returns an empty string if banner has no margin |
57
|
|
|
* |
58
|
|
|
* @test |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function getAdditionalCssReturnsEmptyStringIfBannerHasNoMarginsTest() |
62
|
|
|
{ |
63
|
|
|
$result = $this->bannerService->getAdditionalCss([]); |
64
|
|
|
$this->assertEquals('', $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Test if additional css returns correct top margin |
69
|
|
|
* |
70
|
|
|
* @test |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function getAdditionalCssReturnsMarginTopIfBannerHasMarginTopTest() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$bannerUid = 100; |
76
|
|
|
$banner = $this->getMockBuilder(Banner::class)->getMock(); |
77
|
|
|
$banner->expects($this->any())->method('getMarginTop')->will($this->returnValue(10)); |
78
|
|
|
$banner->expects($this->any())->method('getMarginRight')->will($this->returnValue(0)); |
79
|
|
|
$banner->expects($this->any())->method('getMarginBottom')->will($this->returnValue(0)); |
80
|
|
|
$banner->expects($this->any())->method('getMarginLeft')->will($this->returnValue(0)); |
81
|
|
|
$banner->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid)); |
82
|
|
|
|
83
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
84
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
85
|
|
|
$banners->attach($banner); |
86
|
|
|
|
87
|
|
|
$expected = '.banner-' . $bannerUid . ' { margin: 10px 0px 0px 0px; }' . chr(10) . chr(13); |
88
|
|
|
$result = $this->bannerService->getAdditionalCss($banners); |
|
|
|
|
89
|
|
|
$this->assertEquals($expected, $result); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test if additional css returns correct right margin |
94
|
|
|
* |
95
|
|
|
* @test |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function getAdditionalCssReturnsMarginRightIfBannerHasMarginRightTest() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$bannerUid = 100; |
101
|
|
|
$banner = $this->getMockBuilder(Banner::class)->getMock(); |
102
|
|
|
$banner->expects($this->any())->method('getMarginTop')->will($this->returnValue(0)); |
103
|
|
|
$banner->expects($this->any())->method('getMarginRight')->will($this->returnValue(10)); |
104
|
|
|
$banner->expects($this->any())->method('getMarginBottom')->will($this->returnValue(0)); |
105
|
|
|
$banner->expects($this->any())->method('getMarginLeft')->will($this->returnValue(0)); |
106
|
|
|
$banner->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid)); |
107
|
|
|
|
108
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
109
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
110
|
|
|
$banners->attach($banner); |
111
|
|
|
|
112
|
|
|
$expected = '.banner-' . $bannerUid . ' { margin: 0px 10px 0px 0px; }' . chr(10) . chr(13); |
113
|
|
|
$result = $this->bannerService->getAdditionalCss($banners); |
|
|
|
|
114
|
|
|
$this->assertEquals($expected, $result); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test if additional css returns correct bottom margin |
119
|
|
|
* |
120
|
|
|
* @test |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function getAdditionalCssReturnsMarginBottomIfBannerHasMarginBottomTest() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$bannerUid = 100; |
126
|
|
|
$banner = $this->getMockBuilder(Banner::class)->getMock(); |
127
|
|
|
$banner->expects($this->any())->method('getMarginTop')->will($this->returnValue(0)); |
128
|
|
|
$banner->expects($this->any())->method('getMarginRight')->will($this->returnValue(0)); |
129
|
|
|
$banner->expects($this->any())->method('getMarginBottom')->will($this->returnValue(10)); |
130
|
|
|
$banner->expects($this->any())->method('getMarginLeft')->will($this->returnValue(0)); |
131
|
|
|
$banner->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid)); |
132
|
|
|
|
133
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
134
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
135
|
|
|
$banners->attach($banner); |
136
|
|
|
|
137
|
|
|
$expected = '.banner-' . $bannerUid . ' { margin: 0px 0px 10px 0px; }' . chr(10) . chr(13); |
138
|
|
|
$result = $this->bannerService->getAdditionalCss($banners); |
|
|
|
|
139
|
|
|
$this->assertEquals($expected, $result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Test if additional css returns correct left margin |
144
|
|
|
* |
145
|
|
|
* @test |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function getAdditionalCssReturnsMarginLeftIfBannerHasMarginLeftTest() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$bannerUid = 100; |
151
|
|
|
$banner = $this->getMockBuilder(Banner::class)->getMock(); |
152
|
|
|
$banner->expects($this->any())->method('getMarginTop')->will($this->returnValue(0)); |
153
|
|
|
$banner->expects($this->any())->method('getMarginRight')->will($this->returnValue(0)); |
154
|
|
|
$banner->expects($this->any())->method('getMarginBottom')->will($this->returnValue(0)); |
155
|
|
|
$banner->expects($this->any())->method('getMarginLeft')->will($this->returnValue(10)); |
156
|
|
|
$banner->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid)); |
157
|
|
|
|
158
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
159
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
160
|
|
|
$banners->attach($banner); |
161
|
|
|
|
162
|
|
|
$expected = '.banner-' . $bannerUid . ' { margin: 0px 0px 0px 10px; }' . chr(10) . chr(13); |
163
|
|
|
$result = $this->bannerService->getAdditionalCss($banners); |
|
|
|
|
164
|
|
|
$this->assertEquals($expected, $result); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test if additional css returns correct margins for multiple banners |
169
|
|
|
* |
170
|
|
|
* @test |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function getAdditionalCssReturnsCssForMultipleBannersTest() |
174
|
|
|
{ |
175
|
|
|
$bannerUid1 = 100; |
176
|
|
|
$bannerUid2 = 200; |
177
|
|
|
$banner1 = $this->getMockBuilder(Banner::class)->getMock(); |
178
|
|
|
$banner1->expects($this->any())->method('getMarginTop')->will($this->returnValue(0)); |
179
|
|
|
$banner1->expects($this->any())->method('getMarginRight')->will($this->returnValue(10)); |
180
|
|
|
$banner1->expects($this->any())->method('getMarginBottom')->will($this->returnValue(0)); |
181
|
|
|
$banner1->expects($this->any())->method('getMarginLeft')->will($this->returnValue(10)); |
182
|
|
|
$banner1->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid1)); |
183
|
|
|
$banner2 = $this->getMockBuilder(Banner::class)->getMock(); |
184
|
|
|
$banner2->expects($this->any())->method('getMarginTop')->will($this->returnValue(10)); |
185
|
|
|
$banner2->expects($this->any())->method('getMarginRight')->will($this->returnValue(0)); |
186
|
|
|
$banner2->expects($this->any())->method('getMarginBottom')->will($this->returnValue(10)); |
187
|
|
|
$banner2->expects($this->any())->method('getMarginLeft')->will($this->returnValue(0)); |
188
|
|
|
$banner2->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid2)); |
189
|
|
|
|
190
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
191
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
192
|
|
|
$banners->attach($banner1); |
193
|
|
|
$banners->attach($banner2); |
194
|
|
|
|
195
|
|
|
$expected = '.banner-' . $bannerUid1 . ' { margin: 0px 10px 0px 10px; }' . chr(10) . chr(13); |
196
|
|
|
$expected .= '.banner-' . $bannerUid2 . ' { margin: 10px 0px 10px 0px; }' . chr(10) . chr(13); |
197
|
|
|
$result = $this->bannerService->getAdditionalCss($banners); |
|
|
|
|
198
|
|
|
$this->assertEquals($expected, $result); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Test if no CSS file is returned if no banners given |
203
|
|
|
* |
204
|
|
|
* @test |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function getAdditionalCssFileReturnsEmptyStringIfNoBannersFoundTest() |
208
|
|
|
{ |
209
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
210
|
|
|
$result = $this->bannerService->getAdditionalCssFile($banners); |
|
|
|
|
211
|
|
|
$this->assertEmpty($result); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Test if returned file contains .css as extension |
216
|
|
|
* |
217
|
|
|
* @test |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function getAdditionalCssFileReturnsFilenameTest() |
221
|
|
|
{ |
222
|
|
|
$bannerUid = 100; |
223
|
|
|
$banner = $this->getMockBuilder(Banner::class)->getMock(); |
224
|
|
|
$banner->expects($this->any())->method('getMarginTop')->will($this->returnValue(0)); |
225
|
|
|
$banner->expects($this->any())->method('getMarginRight')->will($this->returnValue(0)); |
226
|
|
|
$banner->expects($this->any())->method('getMarginBottom')->will($this->returnValue(0)); |
227
|
|
|
$banner->expects($this->any())->method('getMarginLeft')->will($this->returnValue(10)); |
228
|
|
|
$banner->expects($this->once())->method('getUid')->will($this->returnValue($bannerUid)); |
229
|
|
|
|
230
|
|
|
/** @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage $banners */ |
231
|
|
|
$banners = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); |
232
|
|
|
$banners->attach($banner); |
233
|
|
|
|
234
|
|
|
$expected = '/\.css/'; |
235
|
|
|
$result = $this->bannerService->getAdditionalCssFile($banners); |
|
|
|
|
236
|
|
|
$this->assertRegExp($expected, $result); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.