Completed
Push — EZP-31681 ( 54659b...e049ce )
by
unknown
18:38 queued 10s
created

MatcherSerializationTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 161
Duplicated Lines 18.63 %

Coupling/Cohesion

Components 2
Dependencies 14

Importance

Changes 0
Metric Value
dl 30
loc 161
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeMatcher() 0 7 1
A deserializeMatcher() 0 8 1
A testDeserialize() 0 8 1
B matcherProvider() 0 83 1
A getMapPortMatcherTestCase() 10 10 1
A getMapHostMatcherTestCase() 10 10 1
A getMapURIMatcherTestCase() 10 10 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\MVC\Symfony\SiteAccess\Tests;
8
9
use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
10
use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher;
11
use PHPUnit\Framework\TestCase;
12
13
class MatcherSerializationTest extends TestCase
14
{
15
    use SerializerTrait;
16
17
    /**
18
     * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher|null $expected
19
     *
20
     * @dataProvider matcherProvider
21
     */
22
    public function testDeserialize(Matcher $matcher, $expected = null)
23
    {
24
        $serializedMatcher = $this->serializeMatcher($matcher);
25
        $unserializedMatcher = $this->deserializeMatcher($serializedMatcher, get_class($matcher));
26
        $expected = $expected ?? $matcher;
27
28
        $this->assertEquals($expected, $unserializedMatcher);
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    private function serializeMatcher(Matcher $matcher)
35
    {
36
        return $this->getSerializer()->serialize(
37
            $matcher,
38
            'json'
39
        );
40
    }
41
42
    /**
43
     * @param string $serializedMatcher
44
     * @param string $matcherFQCN
45
     *
46
     * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher|object
47
     */
48
    private function deserializeMatcher($serializedMatcher, $matcherFQCN)
49
    {
50
        return $this->getSerializer()->deserialize(
51
            $serializedMatcher,
52
            $matcherFQCN,
53
            'json'
54
        );
55
    }
56
57
    public function matcherProvider()
58
    {
59
        $subMatchers = [
60
            'Map\URI' => [
61
                'map' => ['key' => 'value'],
62
            ],
63
            'Map\Host' => [
64
                'map' => ['key' => 'value'],
65
            ],
66
        ];
67
        $logicalAnd = new Matcher\Compound\LogicalAnd(
68
            [
69
                [
70
                    'match' => 'site_access_name',
71
                ],
72
            ]
73
        );
74
        $logicalAnd->setSubMatchers($subMatchers);
75
        $expectedLogicalAnd = new Matcher\Compound\LogicalAnd([]);
76
        $expectedLogicalAnd->setSubMatchers($subMatchers);
77
78
        $logicalOr = new Matcher\Compound\LogicalOr(
79
            [
80
                [
81
                    'match' => 'site_access_name',
82
                ],
83
            ]
84
        );
85
        $logicalOr->setSubMatchers($subMatchers);
86
        $expectedLogicalOr = new Matcher\Compound\LogicalOr([]);
87
        $expectedLogicalOr->setSubMatchers($subMatchers);
88
89
        $expectedMapURI = new Matcher\Map\URI([]);
90
        $expectedMapURI->setMapKey('site');
91
92
        return [
93
            'URIText' => [
94
                new Matcher\URIText([
95
                    'prefix' => 'foo',
96
                    'suffix' => 'bar',
97
                ]),
98
            ],
99
            'HostText' => [
100
                new Matcher\HostText([
101
                    'prefix' => 'foo',
102
                    'suffix' => 'bar',
103
                ]),
104
            ],
105
            'RegexHost' => [
106
                new Matcher\Regex\Host([
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...cess\Matcher\Regex\Host has been deprecated with message: since 5.3 as it cannot be reverted.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
107
                    'regex' => 'foo',
108
                    'itemNumber' => 2,
109
                ]),
110
            ],
111
            'RegexURI' => [
112
                new Matcher\Regex\URI([
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...ccess\Matcher\Regex\URI has been deprecated with message: since 5.3 as it cannot be reverted.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
113
                    'regex' => 'foo',
114
                    'itemNumber' => 2,
115
                ]),
116
            ],
117
            'URIElement' => [
118
                new Matcher\URIElement([
119
                    'elementNumber' => 2,
120
                ]),
121
            ],
122
            'HostElement' => [
123
                new Matcher\HostElement([
124
                    'elementNumber' => 2,
125
                ]),
126
            ],
127
            'MapURI' => $this->getMapURIMatcherTestCase(),
128
            'MapPort' => $this->getMapPortMatcherTestCase(),
129
            'MapHost' => $this->getMapHostMatcherTestCase(),
130
            'CompoundAnd' => [
131
                $logicalAnd,
132
                $expectedLogicalAnd,
133
            ],
134
            'CompoundOr' => [
135
                $logicalOr,
136
                $expectedLogicalOr,
137
            ],
138
        ];
139
    }
140
141 View Code Duplication
    private function getMapPortMatcherTestCase()
142
    {
143
        $matcherBeforeSerialization = new Matcher\Map\Port(['map' => ['key' => 'value']]);
144
        $matcherBeforeSerialization->setMapKey('map');
145
146
        $matcherAfterDeserialization = new Matcher\Map\Port([]);
147
        $matcherAfterDeserialization->setMapKey('map');
148
149
        return [$matcherBeforeSerialization, $matcherAfterDeserialization];
150
    }
151
152 View Code Duplication
    private function getMapHostMatcherTestCase()
153
    {
154
        $matcherBeforeSerialization = new Matcher\Map\Host(['map' => ['key' => 'value']]);
155
        $matcherBeforeSerialization->setMapKey('map');
156
157
        $matcherAfterDeserialization = new Matcher\Map\Host([]);
158
        $matcherAfterDeserialization->setMapKey('map');
159
160
        return [$matcherBeforeSerialization, $matcherAfterDeserialization];
161
    }
162
163 View Code Duplication
    private function getMapURIMatcherTestCase()
164
    {
165
        $matcherBeforeSerialization = new Matcher\Map\URI(['map' => ['key' => 'value']]);
166
        $matcherBeforeSerialization->setMapKey('map');
167
168
        $matcherAfterDeserialization = new Matcher\Map\URI([]);
169
        $matcherAfterDeserialization->setMapKey('map');
170
171
        return [$matcherBeforeSerialization, $matcherAfterDeserialization];
172
    }
173
}
174