BannerServiceTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 220
Duplicated Lines 32.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 72
loc 220
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 4 1
A getAdditionalCssReturnsEmptyStringIfBannerHasNoMarginsTest() 0 5 1
A getAdditionalCssReturnsMarginTopIfBannerHasMarginTopTest() 18 18 1
A getAdditionalCssReturnsMarginRightIfBannerHasMarginRightTest() 18 18 1
A getAdditionalCssReturnsMarginBottomIfBannerHasMarginBottomTest() 18 18 1
A getAdditionalCssReturnsMarginLeftIfBannerHasMarginLeftTest() 18 18 1
A getAdditionalCssReturnsCssForMultipleBannersTest() 0 27 1
A getAdditionalCssFileReturnsEmptyStringIfNoBannersFoundTest() 0 6 1
A getAdditionalCssFileReturnsFilenameTest() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$banners is of type object<TYPO3\CMS\Extbase...sistence\ObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
236
        $this->assertRegExp($expected, $result);
237
    }
238
}
239