Completed
Push — EZP-31776 ( 0f7845...f8b5ee )
by
unknown
23:09 queued 46s
created

RestExecutedViewTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 156
Duplicated Lines 10.9 %

Coupling/Cohesion

Components 2
Dependencies 12

Importance

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

10 Methods

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