Completed
Push — master ( e16616...2d6e36 )
by
unknown
19:26 queued 03:33
created

VisibleOnlyTest::getMockExpression()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 59

Duplication

Lines 38
Ratio 64.41 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 3
dl 38
loc 59
rs 8.8945
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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)');
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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