Completed
Push — EZP-31776 ( 579c0c )
by
unknown
18:37
created

RestExecutedViewTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 148
Duplicated Lines 11.49 %

Coupling/Cohesion

Components 2
Dependencies 12

Importance

Changes 0
Metric Value
dl 17
loc 148
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildLocationSearchHit() 0 8 1
A testVisit() 0 45 1
A provideXpathAssertions() 17 17 1
A testGeneratedXml() 0 4 1
A internalGetVisitor() 0 8 1
A getLocationServiceMock() 0 4 1
A getContentServiceMock() 0 4 1
A getContentTypeServiceMock() 0 4 1
A buildContentSearchHit() 0 11 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\Core\REST\Server\Tests\Output\ValueObjectVisitor;
8
9
use eZ\Publish\API\Repository\ContentService;
10
use eZ\Publish\API\Repository\ContentTypeService;
11
use eZ\Publish\API\Repository\LocationService;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
14
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
15
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
16
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
17
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
18
use eZ\Publish\Core\Repository\Values\Content;
19
use eZ\Publish\Core\REST\Server\Values\RestExecutedView;
20
use eZ\Publish\Core\Repository\Values\Content as ApiValues;
21
22
class RestExecutedViewTest extends ValueObjectVisitorBaseTest
23
{
24
    /**
25
     * Test the RestExecutedView visitor.
26
     *
27
     * @return \DOMDocument
28
     */
29
    public function testVisit()
30
    {
31
        $visitor = $this->getVisitor();
32
        $generator = $this->getGenerator();
33
34
        $generator->startDocument(null);
35
36
        $view = new RestExecutedView(
37
            [
38
                'identifier' => 'test_view',
39
                'searchResults' => new SearchResult([
40
                    'searchHits' => [
41
                        $this->buildContentSearchHit(),
42
                        $this->buildLocationSearchHit(),
43
                    ],
44
                ]),
45
            ]
46
        );
47
48
        $this->addRouteExpectation(
49
            'ezpublish_rest_views_load',
50
            ['viewId' => $view->identifier],
51
            "/content/views/{$view->identifier}"
52
        );
53
        $this->addRouteExpectation(
54
            'ezpublish_rest_views_load_results',
55
            ['viewId' => $view->identifier],
56
            "/content/views/{$view->identifier}/results"
57
        );
58
59
        $visitor->visit(
60
            $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...
61
            $generator,
62
            $view
63
        );
64
65
        $result = $generator->endDocument(null);
66
67
        $this->assertNotNull($result);
68
69
        $dom = new \DOMDocument();
70
        $dom->loadXml($result);
71
72
        return $dom;
73
    }
74
75 View Code Duplication
    public function provideXpathAssertions()
76
    {
77
        return [
78
            ['/View'],
79
            ['/View[@media-type="application/vnd.ez.api.View+xml"]'],
80
            ['/View[@href="/content/views/test_view"]'],
81
            ['/View/identifier'],
82
            ['/View/identifier[text()="test_view"]'],
83
            ['/View/Query'],
84
            ['/View/Query[@media-type="application/vnd.ez.api.Query+xml"]'],
85
            ['/View/Result'],
86
            ['/View/Result[@media-type="application/vnd.ez.api.ViewResult+xml"]'],
87
            ['/View/Result[@href="/content/views/test_view/results"]'],
88
            ['/View/Result/searchHits/searchHit[@score="0.123" and @index="alexandria"]'],
89
            ['/View/Result/searchHits/searchHit[@score="0.234" and @index="waze"]'],
90
        ];
91
    }
92
93
    /**
94
     * @param string $xpath
95
     * @param \DOMDocument $dom
96
     *
97
     * @depends testVisit
98
     * @dataProvider provideXpathAssertions
99
     */
100
    public function testGeneratedXml($xpath, \DOMDocument $dom)
101
    {
102
        $this->assertXPath($dom, $xpath);
103
    }
104
105
    /**
106
     * Get the Relation visitor.
107
     *
108
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\RestExecutedView
109
     */
110
    protected function internalGetVisitor()
111
    {
112
        return new ValueObjectVisitor\RestExecutedView(
113
            $this->getLocationServiceMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getLocationServiceMock() targeting eZ\Publish\Core\REST\Ser...etLocationServiceMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Ser...utedView::__construct() does only seem to accept object<eZ\Publish\API\Repository\LocationService>, 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...
114
            $this->getContentServiceMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getContentServiceMock() targeting eZ\Publish\Core\REST\Ser...getContentServiceMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Ser...utedView::__construct() does only seem to accept object<eZ\Publish\API\Repository\ContentService>, 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...
115
            $this->getContentTypeServiceMock()
116
        );
117
    }
118
119
    /**
120
     * @return \eZ\Publish\API\Repository\LocationService|\PHPUnit\Framework\MockObject\MockObject
121
     */
122
    public function getLocationServiceMock()
123
    {
124
        return $this->createMock(LocationService::class);
125
    }
126
127
    /**
128
     * @return \eZ\Publish\API\Repository\ContentService|\PHPUnit\Framework\MockObject\MockObject
129
     */
130
    public function getContentServiceMock()
131
    {
132
        return $this->createMock(ContentService::class);
133
    }
134
135
    /**
136
     * @return \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject
137
     */
138
    public function getContentTypeServiceMock()
139
    {
140
        return $this->createMock(ContentTypeService::class);
141
    }
142
143
    /**
144
     * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit
145
     */
146
    protected function buildContentSearchHit()
147
    {
148
        return new SearchHit([
149
            'score' => 0.123,
150
            'index' => 'alexandria',
151
            'valueObject' => new ApiValues\Content([
152
                'versionInfo' => new Content\VersionInfo(['contentInfo' => new ContentInfo()]),
153
                'contentType' => new ContentType(),
154
            ]),
155
        ]);
156
    }
157
158
    /**
159
     * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchHit
160
     */
161
    protected function buildLocationSearchHit()
162
    {
163
        return new SearchHit([
164
            'score' => 0.234,
165
            'index' => 'waze',
166
            'valueObject' => new ApiValues\Location(),
167
        ]);
168
    }
169
}
170