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

BaseURLServiceTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A doTestFindUrls() 0 16 2
A assertSearchResultItems() 0 12 3
A assertUsagesSearchResultItems() 0 7 2
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\SearchResult;
10
use eZ\Publish\API\Repository\Values\URL\URLQuery;
11
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
12
13
/**
14
 * Base class for URLService tests.
15
 */
16
abstract class BaseURLServiceTest extends BaseTest
17
{
18
    protected function doTestFindUrls(URLQuery $query, array $expectedUrls, $exectedTotalCount = null, $ignoreOrder = true)
19
    {
20
        $repository = $this->getRepository();
21
22
        /* BEGIN: Use Case */
23
        $searchResult = $repository->getURLService()->findUrls($query);
24
        /* END: Use Case */
25
26
        if ($exectedTotalCount === null) {
27
            $exectedTotalCount = count($expectedUrls);
28
        }
29
30
        $this->assertInstanceOf(SearchResult::class, $searchResult);
31
        $this->assertEquals($exectedTotalCount, $searchResult->totalCount);
32
        $this->assertSearchResultItems($searchResult, $expectedUrls, $ignoreOrder);
33
    }
34
35
    protected function assertSearchResultItems(SearchResult $searchResult, array $expectedUrls, $ignoreOrder)
36
    {
37
        $this->assertCount(count($expectedUrls), $searchResult->items);
38
39
        foreach ($searchResult->items as $i => $item) {
40
            if ($ignoreOrder) {
41
                $this->assertContains($item->url, $expectedUrls);
0 ignored issues
show
Documentation introduced by
The property $url 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...
42
            } else {
43
                $this->assertEquals($expectedUrls[$i], $item->url);
0 ignored issues
show
Documentation introduced by
The property $url 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...
44
            }
45
        }
46
    }
47
48
    protected function assertUsagesSearchResultItems(UsageSearchResult $searchResult, array $expectedContentInfoIds)
49
    {
50
        $this->assertCount(count($expectedContentInfoIds), $searchResult->items);
51
        foreach ($searchResult->items as $contentInfo) {
52
            $this->assertContains($contentInfo->id, $expectedContentInfoIds);
53
        }
54
    }
55
}
56