Completed
Push — master ( 4896ac...e378d5 )
by Łukasz
24:08
created

BookmarkListTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 21.35 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testVisit() 19 19 1
A testResultContainsBookmarkListElement() 0 13 1
A testResultContainsCountElement() 0 10 1
A testResultContainsBookmarkElement() 0 10 1
A internalGetVisitor() 0 4 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
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor;
10
11
use DOMDocument;
12
use DOMXPath;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
15
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
16
use eZ\Publish\Core\REST\Server\Values\BookmarkList;
17
use eZ\Publish\Core\REST\Server\Values\RestLocation;
18
19
class BookmarkListTest extends ValueObjectVisitorBaseTest
20
{
21
    /**
22
     * @var \eZ\Publish\Core\REST\Server\Values\BookmarkList
23
     */
24
    private $data;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
25
26
    protected function setUp()
27
    {
28
        $this->data = new BookmarkList(10, [
29
            new RestLocation($this->createMock(Location::class), 0),
30
            new RestLocation($this->createMock(Location::class), 0),
31
            new RestLocation($this->createMock(Location::class), 0),
32
        ]);
33
    }
34
35 View Code Duplication
    public function testVisit(): string
36
    {
37
        $visitor = $this->getVisitor();
38
        $generator = $this->getGenerator();
39
40
        $generator->startDocument(null);
41
42
        $visitor->visit(
43
            $this->getVisitorMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getVisitorMock() targeting eZ\Publish\Core\REST\Com...eTest::getVisitorMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Com...eObjectVisitor::visit() does only seem to accept object<eZ\Publish\Core\R...\Common\Output\Visitor>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
            $generator,
45
            $this->data
46
        );
47
48
        $result = $generator->endDocument(null);
49
50
        $this->assertNotNull($result);
51
52
        return $result;
53
    }
54
55
    /**
56
     * @depends testVisit
57
     */
58
    public function testResultContainsBookmarkListElement(string $result): void
59
    {
60
        $this->assertXMLTag(
61
            [
62
                'tag' => 'BookmarkList',
63
                'attributes' => [
64
                    'media-type' => 'application/vnd.ez.api.BookmarkList+xml',
65
                ],
66
            ],
67
            $result,
68
            'Invalid <BookmarkList> attributes.'
69
        );
70
    }
71
72
    /**
73
     * @depends testVisit
74
     */
75
    public function testResultContainsCountElement(string $result): void
76
    {
77
        $this->assertXMLTag(
78
            [
79
                'tag' => 'count',
80
                'content' => $this->data->totalCount,
81
            ],
82
            $result
83
        );
84
    }
85
86
    /**
87
     * @depends testVisit
88
     */
89
    public function testResultContainsBookmarkElement(string $result): void
90
    {
91
        $query = "//BookmarkList/Bookmark[@media-type='application/vnd.ez.api.Bookmark+xml']";
92
93
        $document = new DOMDocument();
94
        $document->loadXML($result);
95
        $xpath = new DOMXPath($document);
96
97
        $this->assertEquals(count($this->data->items), $xpath->query($query)->length);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function internalGetVisitor()
104
    {
105
        return new ValueObjectVisitor\BookmarkList();
106
    }
107
}
108