findRecordsWithMaxImpressionsTest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace DERHANSEN\SfBanners\Tests\Functional\Repository;
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 DERHANSEN\SfBanners\Domain\Model\BannerDemand;
18
use DERHANSEN\SfBanners\Domain\Repository\BannerRepository;
19
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Object\ObjectManager;
22
23
/**
24
 * Test case for class \DERHANSEN\SfBanners\Domain\Model\Banner.
25
 */
26
class BannerRepositoryTest extends FunctionalTestCase
27
{
28
29
    /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager */
30
    protected $objectManager;
31
32
    /** @var \DERHANSEN\SfBanners\Domain\Repository\BannerRepository */
33
    protected $bannerRepository;
34
35
    /** @var array */
36
    protected $testExtensionsToLoad = ['typo3conf/ext/sf_banners'];
37
38
    /**
39
     * Setup
40
     *
41
     * @return void
42
     */
43
    public function setUp()
44
    {
45
        parent::setUp();
46
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
47
        $this->bannerRepository = $this->objectManager->get(BannerRepository::class);
48
49
        $this->importDataSet(__DIR__ . '/../Fixtures/sys_category.xml');
50
        $this->importDataSet(__DIR__ . '/../Fixtures/pages.xml');
51
        $this->importDataSet(__DIR__ . '/../Fixtures/tx_sfbanners_domain_model_banner.xml');
52
    }
53
54
    /**
55
     * Test if records are returned correctly with given startingpoints
56
     *
57
     * @test
58
     * @return void
59
     */
60
    public function findRecordsByStartingPointTest()
61
    {
62
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
63
        $demand = new BannerDemand();
64
65
        /* Simple starting point */
66
        $demand->setStartingPoint(55);
67
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
68
69
        /* Multiple starting points */
70
        $demand->setStartingPoint('56,57,58');
71
        $this->assertEquals(3, count($this->bannerRepository->findDemanded($demand)));
72
73
        /* Multiple starting points, including invalid value */
74
        $demand->setStartingPoint('57,58,?,59');
75
        $this->assertEquals(3, count($this->bannerRepository->findDemanded($demand)));
76
    }
77
78
    /**
79
     * Test if records are found by their catagory
80
     *
81
     * @test
82
     * @return void
83
     */
84
    public function findRecordsByCategoryTest()
85
    {
86
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
87
        $demand = new BannerDemand();
88
89
        /* Set starting point */
90
        $demand->setStartingPoint(10);
91
92
        /* Simple category test */
93
        $demand->setCategories('10');
94
        $this->assertEquals(4, count($this->bannerRepository->findDemanded($demand)));
95
96
        /* Multiple category test */
97
        $demand->setCategories('10,11');
98
        $this->assertEquals(4, count($this->bannerRepository->findDemanded($demand)));
99
100
        /* Multiple category test, including invalid value */
101
        $demand->setCategories('11,?,12');
102
        $this->assertEquals(3, count($this->bannerRepository->findDemanded($demand)));
103
104
        /* Non existing category test */
105
        $demand->setCategories('9999');
106
        $this->assertEquals(0, count($this->bannerRepository->findDemanded($demand)));
107
    }
108
109
    /**
110
     * Test is records are found by their displaymode
111
     *
112
     * @test
113
     * @return void
114
     */
115
    public function findRecordsWithDisplayModeTest()
116
    {
117
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
118
        $demand = new BannerDemand();
119
        $pid = 80;
120
        $uids = [
121
            1 => 20,
122
            2 => 21,
123
            3 => 22,
124
            4 => 23,
125
            5 => 24
126
        ];
127
128
        /* Set starting point */
129
        $demand->setStartingPoint($pid);
130
131
        /* All banners with default sorting respected */
132
        $demand->setDisplayMode('all');
133
        $this->assertEquals(5, count($this->bannerRepository->findDemanded($demand)));
134
        $returnedBanners = $this->bannerRepository->findDemanded($demand);
135
        $returnedUids = [];
136
        $count = 1;
137
        foreach ($returnedBanners as $returnedBanner) {
138
            $returnedUids[$count] = $returnedBanner->getUid();
139
            $count++;
140
        }
141
        $this->assertSame($uids, $returnedUids);
142
143
        /* Set starting point */
144
        $demand->setStartingPoint($pid);
145
146
        /* Random one banner */
147
        $demand->setDisplayMode('random');
148
        $this->assertEquals(1, count($this->bannerRepository->findDemanded($demand)));
149
150
        /* All banners with random diplay mode */
151
        $demand->setDisplayMode('allRandom');
152
        $this->assertEquals(5, count($this->bannerRepository->findDemanded($demand)));
153
154
        /* Find 100 times with demand, if returned UIDs are always the same, then they are not returned randomly */
155
        $matchCount = 0;
156
        for ($j = 1; $j <= 100; $j++) {
157
            $returnedBanners = $this->bannerRepository->findDemanded($demand);
158
            $returnedUids = [];
159
            $count = 1;
160
            foreach ($returnedBanners as $returnedBanner) {
161
                $returnedUids[$count] = $returnedBanner->getUid();
162
                $count++;
163
            }
164
            if ($uids === $returnedUids) {
165
                $matchCount += 1;
166
            }
167
        }
168
        $this->assertLessThan(100, $matchCount);
169
    }
170
171
    /**
172
     * Test if records are not returned on pages where they not should be shown
173
     *
174
     * @test
175
     * @return void
176
     */
177 View Code Duplication
    public function findRecordsForSpecialExcludePageUidTest()
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...
178
    {
179
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
180
        $demand = new BannerDemand();
181
        $pid = 95;
182
183
        /* Define PIDs */
184
        $pid1 = 4;
185
        $pid2 = 5;
186
        $pid3 = 6;
187
188
        /* Set starting point */
189
        $demand->setStartingPoint($pid);
190
191
        /* All banners, which not should be shown on the page with $pid1 */
192
        $demand->setCurrentPageUid($pid1);
193
        $this->assertEquals(1, count($this->bannerRepository->findDemanded($demand)));
194
195
        /* All banners, which not should be shown on page with $pid2 */
196
        $demand->setCurrentPageUid($pid2);
197
        $this->assertEquals(1, count($this->bannerRepository->findDemanded($demand)));
198
199
        /* All banners, which not should be shown on page with $pid3 */
200
        $demand->setCurrentPageUid($pid3);
201
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
202
203
        /* All banners, which not should be shown on page with a non existing pid */
204
        $demand->setCurrentPageUid(999);
205
        $this->assertEquals(3, count($this->bannerRepository->findDemanded($demand)));
206
    }
207
208
    /**
209
     * Test if records are not returned on pages recursively where they not should be shown
210
     *
211
     * @test
212
     * @return void
213
     */
214 View Code Duplication
    public function findRecordsForSpecialExcludeRecursivePageUidTest()
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...
215
    {
216
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
217
        $demand = new BannerDemand();
218
        $pid = 96;
219
220
        /* Define PIDs */
221
        $pid1 = 7;
222
        $pid2 = 8;
223
        $pid3 = 9;
224
        $pid4 = 10;
225
226
        /* Set starting point */
227
        $demand->setStartingPoint($pid);
228
229
        /* All banners, which not should be shown on the page with $pid1 */
230
        $demand->setCurrentPageUid($pid1);
231
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
232
233
        /* All banners, which not should be shown on page with $pid2 */
234
        $demand->setCurrentPageUid($pid2);
235
        $this->assertEquals(1, count($this->bannerRepository->findDemanded($demand)));
236
237
        /* All banners, which not should be shown on page with $pid3 */
238
        $demand->setCurrentPageUid($pid3);
239
        $this->assertEquals(0, count($this->bannerRepository->findDemanded($demand)));
240
241
        /* All banners, which not should be shown on page with $pid4 */
242
        $demand->setCurrentPageUid($pid4);
243
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
244
    }
245
246
    /**
247
     * Test if records are not returned, if max impressions reached
248
     *
249
     * @test
250
     * @return void
251
     */
252 View Code Duplication
    public function findRecordsWithMaxImpressionsTest()
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...
253
    {
254
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
255
        $demand = new BannerDemand();
256
        $pid = 100;
257
258
        /* Set starting point */
259
        $demand->setStartingPoint($pid);
260
261
        /* Verify, that 2 records are returned */
262
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
263
    }
264
265
    /**
266
     * Test if records are not returned, if max clicks reached
267
     *
268
     * @test
269
     * @return void
270
     */
271 View Code Duplication
    public function findRecordsWithMaxClicksTest()
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...
272
    {
273
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
274
        $demand = new BannerDemand();
275
        $pid = 101;
276
277
        /* Set starting point */
278
        $demand->setStartingPoint($pid);
279
280
        /* Verify, that 2 records are returned */
281
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
282
    }
283
284
    /**
285
     * Test if records are not returned, if max clicks and/or max impressions reached
286
     *
287
     * @test
288
     * @return void
289
     */
290 View Code Duplication
    public function findRecordsWithMaxImpressionsAndMaxClicksTest()
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...
291
    {
292
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
293
        $demand = new BannerDemand();
294
        $pid = 102;
295
296
        /* Set starting point */
297
        $demand->setStartingPoint($pid);
298
299
        /* Verify, that 1 record are returned */
300
        $this->assertEquals(1, count($this->bannerRepository->findDemanded($demand)));
301
    }
302
303
    /**
304
     * Test if expected amount of records are returned, if a mex result is set
305
     *
306
     * @test
307
     * @return void
308
     */
309
    public function findRecordsWithMaxResultsTest()
310
    {
311
        /** @var \DERHANSEN\SfBanners\Domain\Model\BannerDemand $demand */
312
        $demand = new BannerDemand();
313
        $pid = 103;
314
315
        /* Set starting point */
316
        $demand->setStartingPoint($pid);
317
318
        /* Verify, that 5 record are returned */
319
        $this->assertEquals(5, count($this->bannerRepository->findDemanded($demand)));
320
321
        $demand->setMaxResults(2);
322
        $this->assertEquals(2, count($this->bannerRepository->findDemanded($demand)));
323
    }
324
}
325