Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

Base::getPersistenceMock()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 58
rs 8.9163
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
 * File contains: Abstract Base service test class for Mock testing.
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\Mock;
10
11
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait;
12
use eZ\Publish\Core\Repository\Helper\RelationProcessor;
13
use eZ\Publish\Core\Search\Common\BackgroundIndexer\NullIndexer;
14
use PHPUnit\Framework\TestCase;
15
use eZ\Publish\Core\Repository\Repository;
16
use eZ\Publish\Core\Repository\Values\Content\Content;
17
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
18
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
19
use eZ\Publish\Core\Repository\Values\User\User;
20
21
/**
22
 * Base test case for tests on services using Mock testing.
23
 */
24
abstract class Base extends TestCase
25
{
26
    use PHPUnit5CompatTrait;
27
28
    /**
29
     * @var \eZ\Publish\API\Repository\Repository
30
     */
31
    private $repository;
32
33
    /**
34
     * @var \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    private $repositoryMock;
37
38
    /**
39
     * @var \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject
40
     */
41
    private $persistenceMock;
42
43
    /**
44
     * The Content / Location / Search ... handlers for the persistence / Search / .. handler mocks.
45
     *
46
     * @var \PHPUnit_Framework_MockObject_MockObject[] Key is relative to "\eZ\Publish\SPI\"
47
     *
48
     * @see getPersistenceMockHandler()
49
     */
50
    private $spiMockHandlers = array();
51
52
    /**
53
     * Get Real repository with mocked dependencies.
54
     *
55
     * @param array $serviceSettings If set then non shared instance of Repository is returned
56
     *
57
     * @return \eZ\Publish\API\Repository\Repository
58
     */
59
    protected function getRepository(array $serviceSettings = array())
60
    {
61
        if ($this->repository === null || !empty($serviceSettings)) {
62
            $repository = new Repository(
63
                $this->getPersistenceMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPersistenceMock() targeting eZ\Publish\Core\Reposito...e::getPersistenceMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...pository::__construct() does only seem to accept object<eZ\Publish\SPI\Persistence\Handler>, 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...
64
                $this->getSPIMockHandler('Search\\Handler'),
65
                new NullIndexer(),
66
                $this->getRelationProcessorMock(),
67
                $serviceSettings,
68
                $this->getStubbedUser(14)
69
            );
70
71
            if (!empty($serviceSettings)) {
72
                return $repository;
73
            }
74
75
            $this->repository = $repository;
76
        }
77
78
        return $this->repository;
79
    }
80
81
    protected $fieldTypeServiceMock;
82
83
    /**
84
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\FieldTypeService
85
     */
86
    protected function getFieldTypeServiceMock()
87
    {
88
        if (!isset($this->fieldTypeServiceMock)) {
89
            $this->fieldTypeServiceMock = $this
90
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\FieldTypeService')
91
                ->disableOriginalConstructor()
92
                ->getMock();
93
        }
94
95
        return $this->fieldTypeServiceMock;
96
    }
97
98
    protected $fieldTypeRegistryMock;
99
100
    /**
101
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\FieldTypeRegistry
102
     */
103
    protected function getFieldTypeRegistryMock()
104
    {
105
        if (!isset($this->fieldTypeRegistryMock)) {
106
            $this->fieldTypeRegistryMock = $this
107
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\FieldTypeRegistry')
108
                ->disableOriginalConstructor()
109
                ->getMock();
110
        }
111
112
        return $this->fieldTypeRegistryMock;
113
    }
114
115
    protected $nameableFieldTypeRegistryMock;
116
117
    /**
118
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\NameableFieldTypeRegistry
119
     */
120
    protected function getNameableFieldTypeRegistryMock()
121
    {
122
        if (!isset($this->nameableFieldTypeRegistryMock)) {
123
            $this->nameableFieldTypeRegistryMock = $this
124
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\NameableFieldTypeRegistry')
125
                ->disableOriginalConstructor()
126
                ->getMock();
127
        }
128
129
        return $this->nameableFieldTypeRegistryMock;
130
    }
131
132
    /**
133
     * @return \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
134
     */
135
    protected function getRepositoryMock()
136
    {
137
        if (!isset($this->repositoryMock)) {
138
            $this->repositoryMock = self::getMock('eZ\\Publish\\API\\Repository\\Repository');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

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...
139
        }
140
141
        return $this->repositoryMock;
142
    }
143
144
    /**
145
     * Returns a persistence Handler mock.
146
     *
147
     * @return \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject
148
     */
149
    protected function getPersistenceMock()
150
    {
151
        if (!isset($this->persistenceMock)) {
152
            $this->persistenceMock = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

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...
153
                'eZ\\Publish\\SPI\\Persistence\\Handler',
154
                array(),
155
                array(),
156
                '',
157
                false
158
            );
159
160
            $this->persistenceMock->expects($this->any())
161
                ->method('contentHandler')
162
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Handler')));
163
164
            $this->persistenceMock->expects($this->any())
165
                ->method('searchHandler')
166
                ->will($this->returnValue($this->getSPIMockHandler('Search\\Handler')));
167
168
            $this->persistenceMock->expects($this->any())
169
                ->method('contentTypeHandler')
170
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Type\\Handler')));
171
172
            $this->persistenceMock->expects($this->any())
173
                ->method('contentLanguageHandler')
174
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Language\\Handler')));
175
176
            $this->persistenceMock->expects($this->any())
177
                ->method('locationHandler')
178
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Location\\Handler')));
179
180
            $this->persistenceMock->expects($this->any())
181
                ->method('objectStateHandler')
182
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\ObjectState\\Handler')));
183
184
            $this->persistenceMock->expects($this->any())
185
                ->method('trashHandler')
186
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Location\\Trash\\Handler')));
187
188
            $this->persistenceMock->expects($this->any())
189
                ->method('userHandler')
190
                ->will($this->returnValue($this->getPersistenceMockHandler('User\\Handler')));
191
192
            $this->persistenceMock->expects($this->any())
193
                ->method('sectionHandler')
194
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\Section\\Handler')));
195
196
            $this->persistenceMock->expects($this->any())
197
                ->method('urlAliasHandler')
198
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\UrlAlias\\Handler')));
199
200
            $this->persistenceMock->expects($this->any())
201
                ->method('urlWildcardHandler')
202
                ->will($this->returnValue($this->getPersistenceMockHandler('Content\\UrlWildcard\\Handler')));
203
        }
204
205
        return $this->persistenceMock;
206
    }
207
208
    protected function getRelationProcessorMock()
209
    {
210
        return $this->getMock(RelationProcessor::class,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

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...
211
            array(),
212
            array(),
213
            '',
214
            false
215
        );
216
    }
217
218
    /**
219
     * Returns a SPI Handler mock.
220
     *
221
     * @param string $handler For instance "Content\\Type\\Handler" or "Search\\Handler", must be relative to "eZ\Publish\SPI"
222
     *
223
     * @return \PHPUnit_Framework_MockObject_MockObject
224
     */
225
    protected function getSPIMockHandler($handler)
226
    {
227
        if (!isset($this->spiMockHandlers[$handler])) {
228
            $this->spiMockHandlers[$handler] = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

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...
229
                "eZ\\Publish\\SPI\\{$handler}",
230
                array(),
231
                array(),
232
                '',
233
                false
234
            );
235
        }
236
237
        return $this->spiMockHandlers[$handler];
238
    }
239
240
    /**
241
     * Returns a persistence Handler mock.
242
     *
243
     * @param string $handler For instance "Content\\Type\\Handler", must be relative to "eZ\Publish\SPI\Persistence"
244
     *
245
     * @return \PHPUnit_Framework_MockObject_MockObject
246
     */
247
    protected function getPersistenceMockHandler($handler)
248
    {
249
        return $this->getSPIMockHandler("Persistence\\{$handler}");
250
    }
251
252
    /**
253
     * Returns User stub with $id as User/Content id.
254
     *
255
     * @param int $id
256
     *
257
     * @return \eZ\Publish\API\Repository\Values\User\User
258
     */
259 View Code Duplication
    protected function getStubbedUser($id)
260
    {
261
        return new User(
262
            array(
263
                'content' => new Content(
264
                    array(
265
                        'versionInfo' => new VersionInfo(
266
                            array(
267
                                'contentInfo' => new ContentInfo(array('id' => $id)),
268
                            )
269
                        ),
270
                        'internalFields' => array(),
271
                    )
272
                ),
273
            )
274
        );
275
    }
276
}
277