Completed
Push — ezp26914-imagine_resolve_excep... ( c5a137...48c7cc )
by
unknown
42:21 queued 27:20
created

Base   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 190
Duplicated Lines 44.21 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 84
loc 190
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 9

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A getStubbedUser() 17 17 1
B createUserVersion1() 32 32 1
A tearDown() 0 5 1
getRepository() 0 1 ?
A assertPropertiesCorrect() 10 10 3
A assertSameClassPropertiesCorrect() 15 15 3
A assertStructPropertiesCorrect() 10 10 3
B assertProperty() 0 31 6
A getDateTime() 0 7 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
 * File contains: Abstract Base service test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Integration;
10
11
use PHPUnit_Framework_TestCase;
12
use eZ\Publish\API\Repository\Values\ValueObject;
13
use eZ\Publish\Core\Repository\Values\User\User;
14
use eZ\Publish\Core\Repository\Values\Content\Content;
15
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
16
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
17
18
/**
19
 * Base test case for tests on services
20
 * Initializes repository.
21
 */
22
abstract class Base extends PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var \eZ\Publish\Core\Repository\Repository
26
     */
27
    protected $repository;
28
29
    /**
30
     * Setup test.
31
     */
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
        $this->repository = static::getRepository();
36
        $this->repository->setCurrentUser($this->getStubbedUser(14));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
37
    }
38
39
    /**
40
     * Returns User stub with $id as User/Content id.
41
     *
42
     * @param int $id
43
     *
44
     * @return \eZ\Publish\API\Repository\Values\User\User
45
     */
46 View Code Duplication
    protected function getStubbedUser($id)
47
    {
48
        return new User(
49
            array(
50
                'content' => new Content(
51
                    array(
52
                        'versionInfo' => new VersionInfo(
53
                            array(
54
                                'contentInfo' => new ContentInfo(array('id' => $id)),
55
                            )
56
                        ),
57
                        'internalFields' => array(),
58
                    )
59
                ),
60
            )
61
        );
62
    }
63
64
    /**
65
     * @return \eZ\Publish\Core\Repository\Values\User\User
66
     */
67 View Code Duplication
    protected function createUserVersion1()
68
    {
69
        $repository = $this->repository;
70
71
        /* BEGIN: Inline */
72
        // ID of the "Editors" user group in an eZ Publish demo installation
73
        $editorsGroupId = 13;
74
75
        $userService = $repository->getUserService();
76
77
        // Instantiate a create struct with mandatory properties
78
        $userCreate = $userService->newUserCreateStruct(
79
            'user',
80
            '[email protected]',
81
            'secret',
82
            'eng-US'
83
        );
84
        $userCreate->enabled = true;
85
86
        // Set some fields required by the user ContentType
87
        $userCreate->setField('first_name', 'Example');
88
        $userCreate->setField('last_name', 'User');
89
90
        // Load parent group for the user
91
        $group = $userService->loadUserGroup($editorsGroupId);
92
93
        // Create a new user instance.
94
        $user = $userService->createUser($userCreate, array($group));
95
        /* END: Inline */
96
97
        return $user;
98
    }
99
100
    /**
101
     * Tear down test (properties).
102
     */
103
    protected function tearDown()
104
    {
105
        unset($this->repository);
106
        parent::tearDown();
107
    }
108
109
    /**
110
     * Generate \eZ\Publish\API\Repository\Repository.
111
     *
112
     * Makes it possible to inject different Io / Persistence handlers
113
     *
114
     * @return \eZ\Publish\API\Repository\Repository
115
     */
116
    abstract protected function getRepository();
117
118
    /**
119
     * Asserts that properties given in $expectedValues are correctly set in
120
     * $actualObject.
121
     *
122
     * @param mixed[] $expectedValues
123
     * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject
124
     * @param array $skipProperties
125
     */
126 View Code Duplication
    protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject, array $skipProperties = array())
127
    {
128
        foreach ($expectedValues as $propertyName => $propertyValue) {
129
            if (in_array($propertyName, $skipProperties)) {
130
                continue;
131
            }
132
133
            $this->assertProperty($propertyName, $propertyValue, $actualObject->$propertyName);
134
        }
135
    }
136
137 View Code Duplication
    protected function assertSameClassPropertiesCorrect(
138
        array $propertiesNames,
139
        ValueObject $expectedValues,
140
        ValueObject $actualObject,
141
        array $skipProperties = array(),
142
        $equal = true
143
    ) {
144
        foreach ($propertiesNames as $propertyName) {
145
            if (in_array($propertyName, $skipProperties)) {
146
                continue;
147
            }
148
149
            $this->assertProperty($propertyName, $expectedValues->$propertyName, $actualObject->$propertyName, $equal);
150
        }
151
    }
152
153
    /**
154
     * Asserts all properties from $expectedValues are correctly set in
155
     * $actualObject.
156
     *
157
     * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues
158
     * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject
159
     * @param array $skipProperties
160
     */
161 View Code Duplication
    protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $skipProperties = array())
162
    {
163
        foreach ($expectedValues as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $expectedValues of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
164
            if (in_array($propertyName, $skipProperties)) {
165
                continue;
166
            }
167
168
            $this->assertProperty($propertyName, $propertyValue, $actualObject->$propertyName);
169
        }
170
    }
171
172
    private function assertProperty($propertyName, $expectedValue, $actualValue, $equal = true)
173
    {
174
        if ($expectedValue instanceof \ArrayObject) {
175
            $expectedValue = $expectedValue->getArrayCopy();
176
        }
177
        if ($actualValue instanceof \ArrayObject) {
178
            $actualValue = $actualValue->getArrayCopy();
179
        }
180
181
        // For PHP 7.1 make sure we just compare the timestamp and not the offset value
182
        if ($expectedValue instanceof \DateTimeInterface) {
183
            $expectedValue = $expectedValue->getTimestamp();
184
        }
185
        if ($actualValue instanceof \DateTimeInterface) {
186
            $actualValue = $actualValue->getTimestamp();
187
        }
188
189
        if ($equal) {
190
            $this->assertEquals(
191
                $expectedValue,
192
                $actualValue,
193
                sprintf('Object property "%s" incorrect.', $propertyName)
194
            );
195
        } else {
196
            $this->assertNotEquals(
197
                $expectedValue,
198
                $actualValue,
199
                sprintf('Object property "%s" incorrect.', $propertyName)
200
            );
201
        }
202
    }
203
204
    protected function getDateTime($timestamp)
205
    {
206
        $dateTime = new \DateTime();
207
        $dateTime->setTimestamp($timestamp);
208
209
        return $dateTime;
210
    }
211
}
212