Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

URLServiceTest   C

Complexity

Total Complexity 28

Size/Duplication

Total Lines 725
Duplicated Lines 25.66 %

Coupling/Cohesion

Components 2
Dependencies 19

Importance

Changes 0
Metric Value
dl 186
loc 725
rs 6.9801
c 0
b 0
f 0
wmc 28
lcom 2
cbo 19

26 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 118 3
B testFindUrls() 30 30 1
A testFindUrlsUsingMatchNone() 0 7 1
A testFindUrlsUsingPatternCriterion() 0 15 1
B testFindUrlsUsingValidityCriterionValid() 29 29 1
A testFindUrlsUsingValidityCriterionInvalid() 0 11 1
B testFindUrlsUsingVisibleOnlyCriterion() 29 29 1
A testFindUrlsWithInvalidOffsetThrowsInvalidArgumentException() 14 14 1
A testFindUrlsWithInvalidLimitThrowsInvalidArgumentException() 14 14 1
A testFindUrlsWithOffset() 0 21 1
A testFindUrlsWithOffsetAndLimit() 0 15 1
A testFindUrlsWithLimitZero() 0 8 1
A testFindUrlsWithSorting() 0 8 1
B dataProviderForFindUrlsWithSorting() 0 35 1
B testUpdateUrl() 0 27 1
B testUpdateUrlStatus() 0 29 1
A testUpdateUrlWithNonUniqueUrl() 17 17 1
A testLoadById() 22 22 1
A testLoadByIdThrowsNotFoundException() 0 12 1
A testLoadByUrl() 22 22 1
A testLoadByUrlThrowsNotFoundException() 0 12 1
A testCreateUpdateStruct() 0 13 1
A testCreateUpdateStructValues() 0 8 1
A testFindUsages() 0 17 1
A dataProviderForFindUsages() 9 9 1
A testFindUsagesReturnsEmptySearchResults() 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
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\API\Repository\Tests;
8
9
use eZ\Publish\API\Repository\Values\URL\Query\Criterion as Criterion;
10
use eZ\Publish\API\Repository\Values\URL\Query\SortClause as SortClause;
11
use eZ\Publish\API\Repository\Values\URL\URL;
12
use eZ\Publish\API\Repository\Values\URL\URLQuery;
13
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
14
use DateTime;
15
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
16
17
/**
18
 * Test case for operations in the UserService using in memory storage.
19
 *
20
 * @see eZ\Publish\API\Repository\URLService
21
 * @group integration
22
 * @group url
23
 */
24
class URLServiceTest extends BaseURLServiceTest
25
{
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $urls = [
31
            [
32
                'name' => 'Twitter',
33
                'url' => 'http://twitter.com/',
34
                'published' => true,
35
            ],
36
            [
37
                'name' => 'Facebook',
38
                'url' => 'http://www.facebook.com/',
39
                'published' => true,
40
            ],
41
            [
42
                'name' => 'Google',
43
                'url' => 'http://www.google.com/',
44
                'published' => true,
45
            ],
46
            [
47
                'name' => 'Vimeo',
48
                'url' => 'http://vimeo.com/',
49
                'published' => true,
50
            ],
51
            [
52
                'name' => 'Facebook Sharer',
53
                'url' => 'http://www.facebook.com/sharer.php',
54
                'published' => true,
55
            ],
56
            [
57
                'name' => 'Youtube',
58
                'url' => 'http://www.youtube.com/',
59
                'published' => true,
60
            ],
61
            [
62
                'name' => 'Googel support',
63
                'url' => 'http://support.google.com/chrome/answer/95647?hl=es',
64
                'published' => true,
65
            ],
66
            [
67
                'name' => 'Instagram',
68
                'url' => 'http://instagram.com/',
69
                'published' => true,
70
            ],
71
            [
72
                'name' => 'Discuz',
73
                'url' => 'http://www.discuz.net/forum.php',
74
                'published' => true,
75
            ],
76
            [
77
                'name' => 'Google calendar',
78
                'url' => 'http://calendar.google.com/calendar/render',
79
                'published' => true,
80
            ],
81
            [
82
                'name' => 'Wikipedia',
83
                'url' => 'http://www.wikipedia.org/',
84
                'published' => true,
85
            ],
86
            [
87
                'name' => 'Google Analytics',
88
                'url' => 'http://www.google.com/analytics/',
89
                'published' => true,
90
            ],
91
            [
92
                'name' => 'nazwa.pl',
93
                'url' => 'http://www.nazwa.pl/',
94
                'published' => true,
95
            ],
96
            [
97
                'name' => 'Apache',
98
                'url' => 'http://www.apache.org/',
99
                'published' => true,
100
            ],
101
            [
102
                'name' => 'Nginx',
103
                'url' => 'http://www.nginx.com/',
104
                'published' => true,
105
            ],
106
            [
107
                'name' => 'Microsoft.com',
108
                'url' => 'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
109
                'published' => true,
110
            ],
111
            [
112
                'name' => 'Dropbox',
113
                'url' => 'http://www.dropbox.com/',
114
                'published' => false,
115
            ],
116
            [
117
                'name' => 'Google [DE]',
118
                'url' => 'http://www.google.de/',
119
                'published' => true,
120
            ],
121
        ];
122
123
        $repository = $this->getRepository();
124
125
        $parentLocationId = $this->generateId('location', 2);
126
127
        $contentService = $repository->getContentService();
128
        $locationService = $repository->getLocationService();
129
130
        $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('url');
131
        foreach ($urls as $data) {
132
            $struct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
133
            $struct->setField('name', $data['name']);
134
            $struct->setField('url', $data['url']);
135
136
            $location = $locationService->newLocationCreateStruct($parentLocationId);
137
138
            $draft = $contentService->createContent($struct, [$location]);
139
            if ($data['published']) {
140
                $contentService->publishVersion($draft->versionInfo);
141
            }
142
        }
143
    }
144
145
    /**
146
     * Test for URLService::findUrls() method.
147
     *
148
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
149
     */
150 View Code Duplication
    public function testFindUrls()
151
    {
152
        $expectedUrls = [
153
            'http://www.apache.org/',
154
            'http://calendar.google.com/calendar/render',
155
            'http://www.dropbox.com/',
156
            '/content/view/sitemap/2',
157
            'http://support.google.com/chrome/answer/95647?hl=es',
158
            'http://www.nazwa.pl/',
159
            'http://www.facebook.com/sharer.php',
160
            'http://www.wikipedia.org/',
161
            'http://www.google.de/',
162
            'http://www.google.com/',
163
            'http://www.nginx.com/',
164
            '/content/view/tagcloud/2',
165
            'http://www.youtube.com/',
166
            'http://vimeo.com/',
167
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
168
            'http://twitter.com/',
169
            'http://www.google.com/analytics/',
170
            'http://www.facebook.com/',
171
            'http://www.discuz.net/forum.php',
172
            'http://instagram.com/',
173
        ];
174
175
        $query = new URLQuery();
176
        $query->filter = new Criterion\MatchAll();
177
178
        $this->doTestFindUrls($query, $expectedUrls);
179
    }
180
181
    /**
182
     * Test for URLService::findUrls() method.
183
     *
184
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
185
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
186
     */
187
    public function testFindUrlsUsingMatchNone()
188
    {
189
        $query = new URLQuery();
190
        $query->filter = new Criterion\MatchNone();
191
192
        $this->doTestFindUrls($query, []);
193
    }
194
195
    /**
196
     * Test for URLService::findUrls() method.
197
     *
198
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
199
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
200
     */
201
    public function testFindUrlsUsingPatternCriterion()
202
    {
203
        $expectedUrls = [
204
            'http://www.google.de/',
205
            'http://www.google.com/',
206
            'http://support.google.com/chrome/answer/95647?hl=es',
207
            'http://calendar.google.com/calendar/render',
208
            'http://www.google.com/analytics/',
209
        ];
210
211
        $query = new URLQuery();
212
        $query->filter = new Criterion\Pattern('google');
213
214
        $this->doTestFindUrls($query, $expectedUrls);
215
    }
216
217
    /**
218
     * Test for URLService::findUrls() method.
219
     *
220
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
221
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
222
     */
223 View Code Duplication
    public function testFindUrlsUsingValidityCriterionValid()
224
    {
225
        $expectedUrls = [
226
            'http://www.google.com/',
227
            '/content/view/sitemap/2',
228
            'http://support.google.com/chrome/answer/95647?hl=es',
229
            'http://www.google.de/',
230
            'http://www.nginx.com/',
231
            'http://www.google.com/analytics/',
232
            'http://www.discuz.net/forum.php',
233
            'http://www.wikipedia.org/',
234
            'http://www.facebook.com/sharer.php',
235
            'http://twitter.com/',
236
            'http://www.nazwa.pl/',
237
            'http://instagram.com/',
238
            'http://www.apache.org/',
239
            'http://www.dropbox.com/',
240
            'http://www.facebook.com/',
241
            'http://www.youtube.com/',
242
            'http://calendar.google.com/calendar/render',
243
            'http://vimeo.com/',
244
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
245
        ];
246
247
        $query = new URLQuery();
248
        $query->filter = new Criterion\Validity(true);
249
250
        $this->doTestFindUrls($query, $expectedUrls);
251
    }
252
253
    /**
254
     * Test for URLService::findUrls() method.
255
     *
256
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
257
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
258
     */
259
    public function testFindUrlsUsingValidityCriterionInvalid()
260
    {
261
        $expectedUrls = [
262
            '/content/view/tagcloud/2',
263
        ];
264
265
        $query = new URLQuery();
266
        $query->filter = new Criterion\Validity(false);
267
268
        $this->doTestFindUrls($query, $expectedUrls);
269
    }
270
271
    /**
272
     * Test for URLService::findUrls() method.
273
     *
274
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
275
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
276
     */
277 View Code Duplication
    public function testFindUrlsUsingVisibleOnlyCriterion()
278
    {
279
        $expectedUrls = [
280
            'http://vimeo.com/',
281
            'http://calendar.google.com/calendar/render',
282
            'http://www.facebook.com/',
283
            'http://www.google.com/',
284
            'http://www.google.com/analytics/',
285
            'http://www.facebook.com/sharer.php',
286
            'http://www.apache.org/',
287
            'http://www.nginx.com/',
288
            'http://www.wikipedia.org/',
289
            'http://www.youtube.com/',
290
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
291
            'http://www.google.de/',
292
            'http://instagram.com/',
293
            'http://www.nazwa.pl/',
294
            '/content/view/tagcloud/2',
295
            'http://www.discuz.net/forum.php',
296
            'http://support.google.com/chrome/answer/95647?hl=es',
297
            'http://twitter.com/',
298
            '/content/view/sitemap/2',
299
        ];
300
301
        $query = new URLQuery();
302
        $query->filter = new Criterion\VisibleOnly();
303
304
        $this->doTestFindUrls($query, $expectedUrls);
305
    }
306
307
    /**
308
     * Test for URLService::findUrls() method.
309
     *
310
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
311
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
312
     */
313 View Code Duplication
    public function testFindUrlsWithInvalidOffsetThrowsInvalidArgumentException()
314
    {
315
        $query = new URLQuery();
316
        $query->filter = new Criterion\MatchAll();
317
        $query->offset = 'invalid!';
0 ignored issues
show
Documentation Bug introduced by
The property $offset was declared of type integer, but 'invalid!' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
318
319
        $repository = $this->getRepository();
320
321
        /* BEGIN: Use Case */
322
        $urlService = $repository->getURLService();
323
        // This call will fail with a InvalidArgumentException
324
        $urlService->findUrls($query);
325
        /* END: Use Case */
326
    }
327
328
    /**
329
     * Test for URLService::findUrls() method.
330
     *
331
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
332
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
333
     */
334 View Code Duplication
    public function testFindUrlsWithInvalidLimitThrowsInvalidArgumentException()
335
    {
336
        $query = new URLQuery();
337
        $query->filter = new Criterion\MatchAll();
338
        $query->limit = 'invalid!';
0 ignored issues
show
Documentation Bug introduced by
The property $limit was declared of type integer, but 'invalid!' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
339
340
        $repository = $this->getRepository();
341
342
        /* BEGIN: Use Case */
343
        $urlService = $repository->getURLService();
344
        // This call will fail with a InvalidArgumentException
345
        $urlService->findUrls($query);
346
        /* END: Use Case */
347
    }
348
349
    /**
350
     * Test for URLService::findUrls() method.
351
     *
352
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
353
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
354
     */
355
    public function testFindUrlsWithOffset()
356
    {
357
        $expectedUrls = [
358
            'http://www.discuz.net/forum.php',
359
            'http://calendar.google.com/calendar/render',
360
            'http://www.wikipedia.org/',
361
            'http://www.google.com/analytics/',
362
            'http://www.nazwa.pl/',
363
            'http://www.apache.org/',
364
            'http://www.nginx.com/',
365
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
366
            'http://www.dropbox.com/',
367
            'http://www.google.de/',
368
        ];
369
370
        $query = new URLQuery();
371
        $query->filter = new Criterion\MatchAll();
372
        $query->offset = 10;
373
374
        $this->doTestFindUrls($query, $expectedUrls, 20);
375
    }
376
377
    /**
378
     * Test for URLService::findUrls() method.
379
     *
380
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
381
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
382
     */
383
    public function testFindUrlsWithOffsetAndLimit()
384
    {
385
        $expectedUrls = [
386
            'http://www.discuz.net/forum.php',
387
            'http://calendar.google.com/calendar/render',
388
            'http://www.wikipedia.org/',
389
        ];
390
391
        $query = new URLQuery();
392
        $query->filter = new Criterion\MatchAll();
393
        $query->offset = 10;
394
        $query->limit = 3;
395
396
        $this->doTestFindUrls($query, $expectedUrls, 20);
397
    }
398
399
    /**
400
     * Test for URLService::findUrls() method.
401
     *
402
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
403
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
404
     */
405
    public function testFindUrlsWithLimitZero()
406
    {
407
        $query = new URLQuery();
408
        $query->filter = new Criterion\MatchAll();
409
        $query->limit = 0;
410
411
        $this->doTestFindUrls($query, [], 20);
412
    }
413
414
    /**
415
     * Test for URLService::findUrls() method.
416
     *
417
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
418
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
419
     * @dataProvider dataProviderForFindUrlsWithSorting
420
     */
421
    public function testFindUrlsWithSorting(SortClause $sortClause, array $expectedUrls)
422
    {
423
        $query = new URLQuery();
424
        $query->filter = new Criterion\MatchAll();
425
        $query->sortClauses = [$sortClause];
426
427
        $this->doTestFindUrls($query, $expectedUrls, null, false);
428
    }
429
430
    public function dataProviderForFindUrlsWithSorting()
431
    {
432
        $urlsSortedById = [
433
            '/content/view/sitemap/2',
434
            '/content/view/tagcloud/2',
435
            'http://twitter.com/',
436
            'http://www.facebook.com/',
437
            'http://www.google.com/',
438
            'http://vimeo.com/',
439
            'http://www.facebook.com/sharer.php',
440
            'http://www.youtube.com/',
441
            'http://support.google.com/chrome/answer/95647?hl=es',
442
            'http://instagram.com/',
443
            'http://www.discuz.net/forum.php',
444
            'http://calendar.google.com/calendar/render',
445
            'http://www.wikipedia.org/',
446
            'http://www.google.com/analytics/',
447
            'http://www.nazwa.pl/',
448
            'http://www.apache.org/',
449
            'http://www.nginx.com/',
450
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
451
            'http://www.dropbox.com/',
452
            'http://www.google.de/',
453
        ];
454
455
        $urlsSortedByURL = $urlsSortedById;
456
        sort($urlsSortedByURL);
457
458
        return [
459
            [new SortClause\Id(SortClause::SORT_ASC), $urlsSortedById],
460
            [new SortClause\Id(SortClause::SORT_DESC), array_reverse($urlsSortedById)],
461
            [new SortClause\URL(SortClause::SORT_ASC), $urlsSortedByURL],
462
            [new SortClause\URL(SortClause::SORT_DESC), array_reverse($urlsSortedByURL)],
463
        ];
464
    }
465
466
    /**
467
     * Test for URLService::updateUrl() method.
468
     *
469
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
470
     */
471
    public function testUpdateUrl()
472
    {
473
        $repository = $this->getRepository();
474
475
        $id = $this->generateId('url', 23);
476
477
        /* BEGIN: Use Case */
478
        $urlService = $repository->getURLService();
479
480
        $urlBeforeUpdate = $urlService->loadById($id);
481
        $updateStruct = $urlService->createUpdateStruct();
482
        $updateStruct->url = 'https://someurl.com/';
483
484
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
485
        /* END: Use Case */
486
487
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
488
        $this->assertPropertiesCorrect([
489
            'id' => 23,
490
            'url' => 'https://someurl.com/',
491
            // (!) URL status should be reset to valid nad never checked
492
            'isValid' => true,
493
            'lastChecked' => null,
494
            'created' => new DateTime('@1343140541'),
495
        ], $urlAfterUpdate);
496
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
0 ignored issues
show
Documentation introduced by
The property $modified is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
497
    }
498
499
    /**
500
     * Test for URLService::updateUrl() method.
501
     *
502
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
503
     */
504
    public function testUpdateUrlStatus()
505
    {
506
        $repository = $this->getRepository();
507
508
        $id = $this->generateId('url', 23);
509
        $checked = new DateTime('@' . time());
510
511
        /* BEGIN: Use Case */
512
        $urlService = $repository->getURLService();
513
514
        $urlBeforeUpdate = $urlService->loadById($id);
515
        $updateStruct = $urlService->createUpdateStruct();
516
        $updateStruct->isValid = false;
517
        $updateStruct->lastChecked = $checked;
518
519
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
520
        /* END: Use Case */
521
522
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
523
        $this->assertPropertiesCorrect([
524
            'id' => $id,
525
            'url' => '/content/view/sitemap/2',
526
            // (!) URL status should be reset to valid nad never checked
527
            'isValid' => false,
528
            'lastChecked' => $checked,
529
            'created' => new DateTime('@1343140541'),
530
        ], $urlAfterUpdate);
531
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
0 ignored issues
show
Documentation introduced by
The property $modified is declared protected in eZ\Publish\API\Repository\Values\URL\URL. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
532
    }
533
534
    /**
535
     * Test for URLService::updateUrl() method.
536
     *
537
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
538
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
539
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testUpdateUrl
540
     */
541 View Code Duplication
    public function testUpdateUrlWithNonUniqueUrl()
542
    {
543
        $repository = $this->getRepository();
544
545
        $id = $this->generateId('url', 23);
546
547
        /* BEGIN: Use Case */
548
        $urlService = $repository->getURLService();
549
550
        $urlBeforeUpdate = $urlService->loadById($id);
551
        $updateStruct = $urlService->createUpdateStruct();
552
        $updateStruct->url = 'http://www.youtube.com/';
553
554
        // This call will fail with a InvalidArgumentException
555
        $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
556
        /* END: Use Case */
557
    }
558
559
    /**
560
     * Test for URLService::loadById() method.
561
     *
562
     * @see \eZ\Publish\Core\Repository\URLService::loadById
563
     */
564 View Code Duplication
    public function testLoadById()
565
    {
566
        $repository = $this->getRepository();
567
568
        $id = $this->generateId('url', 23);
569
570
        /* BEGIN: Use Case */
571
        $urlService = $repository->getURLService();
572
573
        $url = $urlService->loadById($id);
574
        /* END: Use Case */
575
576
        $this->assertInstanceOf(URL::class, $url);
577
        $this->assertPropertiesCorrect([
578
            'id' => 23,
579
            'url' => '/content/view/sitemap/2',
580
            'isValid' => true,
581
            'lastChecked' => null,
582
            'created' => new DateTime('@1343140541'),
583
            'modified' => new DateTime('@1343140541'),
584
        ], $url);
585
    }
586
587
    /**
588
     * Test for URLService::loadById() method.
589
     *
590
     * @see \eZ\Publish\Core\Repository\URLService::loadById
591
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
592
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
593
     */
594
    public function testLoadByIdThrowsNotFoundException()
595
    {
596
        $repository = $this->getRepository();
597
598
        $nonExistentUrlId = $this->generateId('url', self::DB_INT_MAX);
599
        /* BEGIN: Use Case */
600
        $urlService = $repository->getURLService();
601
602
        // This call will fail with a NotFoundException
603
        $url = $urlService->loadById($nonExistentUrlId);
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
604
        /* END: Use Case */
605
    }
606
607
    /**
608
     * Test for URLService::loadByUrl() method.
609
     *
610
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
611
     */
612 View Code Duplication
    public function testLoadByUrl()
613
    {
614
        $repository = $this->getRepository();
615
616
        $urlAddr = '/content/view/sitemap/2';
617
        /* BEGIN: Use Case */
618
        $urlService = $repository->getURLService();
619
620
        $url = $urlService->loadByUrl($urlAddr);
621
622
        /* END: Use Case */
623
624
        $this->assertInstanceOf(URL::class, $url);
625
        $this->assertPropertiesCorrect([
626
            'id' => 23,
627
            'url' => '/content/view/sitemap/2',
628
            'isValid' => true,
629
            'lastChecked' => null,
630
            'created' => new DateTime('@1343140541'),
631
            'modified' => new DateTime('@1343140541'),
632
        ], $url);
633
    }
634
635
    /**
636
     * Test for URLService::loadByUrl() method.
637
     *
638
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
639
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
640
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadByUrl
641
     */
642
    public function testLoadByUrlThrowsNotFoundException()
643
    {
644
        $repository = $this->getRepository();
645
646
        $nonExistentUrl = 'https://laravel.com/';
647
        /* BEGIN: Use Case */
648
        $urlService = $repository->getURLService();
649
650
        // This call will fail with a NotFoundException
651
        $url = $urlService->loadByUrl($nonExistentUrl);
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
652
        /* END: Use Case */
653
    }
654
655
    /**
656
     * Test for URLService::createUpdateStruct() method.
657
     *
658
     * @see \eZ\Publish\API\Repository\URLService::createUpdateStruct
659
     *
660
     * @return \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct
661
     */
662
    public function testCreateUpdateStruct()
663
    {
664
        $repository = $this->getRepository();
665
666
        /* BEGIN: Use Case */
667
        $urlService = $repository->getURLService();
668
        $updateStruct = $urlService->createUpdateStruct();
669
        /* END: Use Case */
670
671
        $this->assertInstanceOf(URLUpdateStruct::class, $updateStruct);
672
673
        return $updateStruct;
674
    }
675
676
    /**
677
     * Test for URLService::createUpdateStruct() method.
678
     *
679
     * @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $updateStruct
680
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testCreateUpdateStruct
681
     */
682
    public function testCreateUpdateStructValues(URLUpdateStruct $updateStruct)
683
    {
684
        $this->assertPropertiesCorrect([
685
            'url' => null,
686
            'isValid' => null,
687
            'lastChecked' => null,
688
        ], $updateStruct);
689
    }
690
691
    /**
692
     * Test for URLService::testFindUsages() method.
693
     *
694
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
695
     * @dataProvider dataProviderForFindUsages
696
     */
697
    public function testFindUsages($urlId, $offset, $limit, array $expectedContentInfos, $expectedTotalCount = null)
698
    {
699
        $repository = $this->getRepository();
700
701
        $id = $this->generateId('url', $urlId);
702
        /* BEGIN: Use Case */
703
        $urlService = $repository->getURLService();
704
705
        $loadedUrl = $urlService->loadById($id);
706
707
        $usagesSearchResults = $urlService->findUsages($loadedUrl, $offset, $limit);
708
        /* END: Use Case */
709
710
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
711
        $this->assertEquals($expectedTotalCount, $usagesSearchResults->totalCount);
712
        $this->assertUsagesSearchResultItems($usagesSearchResults, $expectedContentInfos);
713
    }
714
715 View Code Duplication
    public function dataProviderForFindUsages()
716
    {
717
        return [
718
            // findUsages($url, 0, -1)
719
            [23, 0, -1, [54], 1],
720
            // findUsages($url, 0, $limit)
721
            [23, 0, 1, [54], 1],
722
        ];
723
    }
724
725
    /**
726
     * Test for URLService::testFindUsages() method.
727
     *
728
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUsages
729
     */
730
    public function testFindUsagesReturnsEmptySearchResults()
731
    {
732
        $repository = $this->getRepository();
733
734
        /* BEGIN: Use Case */
735
        $urlService = $repository->getURLService();
736
737
        $loadedUrl = $urlService->loadByUrl('http://www.dropbox.com/');
738
739
        $usagesSearchResults = $urlService->findUsages($loadedUrl);
740
        /* END: Use Case */
741
742
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
743
        $this->assertPropertiesCorrect([
744
            'totalCount' => 0,
745
            'items' => [],
746
        ], $usagesSearchResults);
747
    }
748
}
749