Completed
Push — 7.5 ( ffc5d4...888411 )
by Łukasz
23:14
created

VisibleOnlyTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 29.46 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 38
loc 129
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testAccept() 0 7 1
A testHandle() 0 46 1
B getMockExpression() 38 59 4

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\Persistence\Legacy\Tests\URL\Query\CriterionHandler;
8
9
use eZ\Publish\API\Repository\Values\URL\Query\Criterion\VisibleOnly;
10
use eZ\Publish\API\Repository\Values\URL\Query\Criterion;
11
use eZ\Publish\Core\Persistence\Database\Expression;
12
use eZ\Publish\Core\Persistence\Database\SelectQuery;
13
use eZ\Publish\Core\Persistence\Legacy\URL\Query\CriteriaConverter;
14
use eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler\VisibleOnly as VisibleOnlyHandler;
15
16
class VisibleOnlyTest extends CriterionHandlerTest
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function testAccept()
22
    {
23
        $handler = new VisibleOnlyHandler();
24
25
        $this->assertHandlerAcceptsCriterion($handler, VisibleOnly::class);
26
        $this->assertHandlerRejectsCriterion($handler, Criterion::class);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function testHandle()
33
    {
34
        $expected = 'ezcontentobject_tree.is_invisible = 0';
35
36
        $basicQuery = $this->createMock(SelectQuery::class);
37
        $basicQuery->method('bindValue')->with(0, null, \PDO::PARAM_INT)->willReturn('0');
38
39
        $converter = $this->createMock(CriteriaConverter::class);
40
        $handler = new VisibleOnlyHandler();
41
        $criterion = new VisibleOnly();
42
43
        // tables not joined yet - handle should join them
44
        $query = clone $basicQuery;
45
        $query->expr = $this->getMockExpression(true, true, true);
46
        $query->method('getQuery')->willReturn('SELECT');
47
        $actual = $handler->handle($converter, $query, $criterion);
48
        $this->assertEquals($expected, $actual);
49
50
        // table link already joined, should not join again
51
        $query = clone $basicQuery;
52
        $query->expr = $this->getMockExpression(false, true, true);
53
        $query->method('getQuery')->willReturn('(SELECT) INNER JOIN ezurl_object_link (ON)');
54
        $actual = $handler->handle($converter, $query, $criterion);
55
        $this->assertEquals($expected, $actual);
56
57
        // table link and attribute already joined, should not join again
58
        $query = clone $basicQuery;
59
        $query->expr = $this->getMockExpression(false, false, true);
60
        $query->method('getQuery')->willReturn(
61
            '(SELECT) INNER JOIN ezurl_object_link (ON) '
62
            . 'INNER JOIN ezcontentobject_attribute (ON)'
63
        );
64
        $actual = $handler->handle($converter, $query, $criterion);
65
        $this->assertEquals($expected, $actual);
66
67
        // table link, attribute, and tree already joined, should not join again
68
        $query = clone $basicQuery;
69
        $query->expr = $this->getMockExpression(false, false, false);
70
        $query->method('getQuery')->willReturn(
71
            '(SELECT) INNER JOIN ezurl_object_link (ON) ' .
72
            'INNER JOIN ezcontentobject_attribute (ON) ' .
73
            'INNER JOIN ezcontentobject_tree (ON)'
74
        );
75
        $actual = $handler->handle($converter, $query, $criterion);
76
        $this->assertEquals($expected, $actual);
77
    }
78
79
    /**
80
     * @param bool $includeLinkJoin
81
     * @param bool $includeAttributeJoin
82
     * @param bool $includeTreeJoin
83
     * @return \PHPUnit\Framework\MockObject\MockObject
84
     */
85
    private function getMockExpression(bool $includeLinkJoin, bool $includeAttributeJoin, bool $includeTreeJoin)
86
    {
87
        $execAt = 0;
88
        $expr = $this->createMock(Expression::class);
89
        if ($includeLinkJoin) {
90
            $expr
91
                ->expects($this->at($execAt++))
92
                ->method('eq')
93
                ->with('ezurl.id', 'ezurl_object_link.url_id')
94
                ->willReturn('ezurl.id=ezurl_object_link.url_id');
95
        }
96 View Code Duplication
        if ($includeAttributeJoin) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $expr
98
                ->expects($this->at($execAt++))
99
                ->method('eq')
100
                ->with('ezurl_object_link.contentobject_attribute_id', 'ezcontentobject_attribute.id')
101
                ->willReturn('ezurl_object_link.contentobject_attribute_id = ezcontentobject_attribute.id');
102
            $expr
103
                ->expects($this->at($execAt++))
104
                ->method('eq')
105
                ->with('ezurl_object_link.contentobject_attribute_version', 'ezcontentobject_attribute.version')
106
                ->willReturn('ezurl_object_link.contentobject_attribute_version = ezcontentobject_attribute.version');
107
            $expr
108
                ->expects($this->at($execAt++))
109
                ->method('lAnd')
110
                ->with(
111
                    'ezurl_object_link.contentobject_attribute_id = ezcontentobject_attribute.id',
112
                    'ezurl_object_link.contentobject_attribute_version = ezcontentobject_attribute.version'
113
                )->willReturn('ezurl_object_link.contentobject_attribute_id = ezcontentobject_attribute.id AND ezurl_object_link.contentobject_attribute_version = ezcontentobject_attribute.version');
114
        }
115
116 View Code Duplication
        if ($includeTreeJoin) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
            $expr
118
                ->expects($this->at($execAt++))
119
                ->method('eq')
120
                ->with('ezcontentobject_tree.contentobject_id', 'ezcontentobject_attribute.contentobject_id')
121
                ->willReturn('ezcontentobject_tree.contentobject_id = ezcontentobject_attribute.contentobject_id');
122
            $expr
123
                ->expects($this->at($execAt++))
124
                ->method('eq')
125
                ->with('ezcontentobject_tree.contentobject_version', 'ezcontentobject_attribute.version')
126
                ->willReturn('ezcontentobject_tree.contentobject_version = ezcontentobject_attribute.version');
127
            $expr
128
                ->expects($this->at($execAt++))
129
                ->method('lAnd')
130
                ->with(
131
                    'ezcontentobject_tree.contentobject_id = ezcontentobject_attribute.contentobject_id',
132
                    'ezcontentobject_tree.contentobject_version = ezcontentobject_attribute.version'
133
                )->willReturn('ezcontentobject_tree.contentobject_id = ezcontentobject_attribute.contentobject_id AND ezcontentobject_tree.contentobject_version = ezcontentobject_attribute.version');
134
        }
135
136
        $expr
137
            ->expects($this->at($execAt))
138
            ->method('eq')
139
            ->with('ezcontentobject_tree.is_invisible', '0')
140
            ->willReturn('ezcontentobject_tree.is_invisible = 0');
141
142
        return $expr;
143
    }
144
}
145