Completed
Push — ezp-30616 ( 9239a0...7bf8e8 )
by
unknown
57:53 queued 37:56
created

ServiceTest::testService()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 2
nop 6
dl 0
loc 54
rs 8.0703
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 containing the ServiceTest 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\SignalSlot\Tests;
10
11
use eZ\Publish\Core\Repository\Values\User\User;
12
use eZ\Publish\Core\Repository\Values\User\UserGroup;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
15
use eZ\Publish\Core\Repository\Values\Content\Content;
16
use eZ\Publish\Core\SignalSlot\SignalDispatcher;
17
use PHPUnit\Framework\TestCase;
18
19
abstract class ServiceTest extends TestCase
20
{
21
    /**
22
     * Returns a mock of the aggregated service.
23
     */
24
    abstract protected function getServiceMock();
25
26
    /**
27
     * Returns an instance of the SignalSlot service to test.
28
     *
29
     * @param mixed $innerService mock of the inner service used by the signal
30
     * slot one used to test whether the original method is called is correctly
31
     * called.
32
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $dispatcher mock of
33
     * the dispatcher used to test whether the emit method is correctly called
34
     *
35
     * @return object An instance of the SignalSlot service
36
     */
37
    abstract protected function getSignalSlotService($innerService, SignalDispatcher $dispatcher);
38
39
    /**
40
     * @dataProvider serviceProvider
41
     *
42
     * Tests that:
43
     * - the original service method is called with the exact same arguments
44
     * - the signal is emitted with the correct signal object containing the
45
     *   expected attributes/values
46
     * - the returned value from the original service method is returned
47
     *   by the method from the signal slot service
48
     */
49
    public function testService(
50
        $method,
51
        $parameters,
52
        $return,
53
        $emitNr,
54
        $signalClass = '',
55
        array $signalAttr = null
56
    ) {
57
        $innerService = $this->getServiceMock();
58
        $innerService->expects($this->once())
59
                     ->method($method)
60
                     ->will(
61
                         $this->returnValueMap(
62
                             array(
63
                                 array_merge($parameters, array($return)),
64
                             )
65
                         )
66
                     );
67
68
        $dispatcher = $this->createMock(SignalDispatcher::class);
69
        $that = $this;
70
        $d = $dispatcher->expects($this->exactly($emitNr))
71
                        ->method('emit');
72
        if ($emitNr && $signalClass && $signalAttr) {
73
            $d->with(
74
                $this->callback(
75
                    function ($signal) use ($that, $signalClass, $signalAttr) {
76
                        if (!$signal instanceof $signalClass) {
77
                            $that->fail(
78
                                "The signal is not an instance of $signalClass"
79
                            );
80
81
                            return false;
82
                        }
83
                        foreach ($signalAttr as $attr => $val) {
84
                            if ($signal->{$attr} !== $val) {
85
                                $that->fail(
86
                                    "The attribute '{$attr}' of the signal does not have the correct value '{$val}'"
87
                                );
88
89
                                return false;
90
                            }
91
                        }
92
93
                        return true;
94
                    }
95
                )
96
            );
97
        }
98
        $service = $this->getSignalSlotService($innerService, $dispatcher);
99
        $result = call_user_func_array(array($service, $method), $parameters);
100
101
        $this->assertTrue($result === $return);
102
    }
103
104
    /**
105
     * Creates a content info from $contentId and $remoteId.
106
     *
107
     * @param mixed $contentId
108
     * @param mixed $remoteId
109
     *
110
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
111
     */
112
    protected function getContentInfo($contentId, $remoteId)
113
    {
114
        return new ContentInfo(
115
            array('id' => $contentId, 'remoteId' => $remoteId)
116
        );
117
    }
118
119
    /**
120
     * Creates a version info object from $contentInfo and $versionNo.
121
     *
122
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
123
     * @param int $versionNo
124
     *
125
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
126
     */
127
    protected function getVersionInfo(ContentInfo $contentInfo, $versionNo)
128
    {
129
        return new VersionInfo(
130
            array(
131
                'contentInfo' => $contentInfo,
132
                'versionNo' => $versionNo,
133
            )
134
        );
135
    }
136
137
    /**
138
     * Creates a content object from $versionInfo.
139
     *
140
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
141
     *
142
     * @return \eZ\Publish\API\Repository\Values\Content\Content
143
     */
144
    protected function getContent(VersionInfo $versionInfo)
145
    {
146
        return new Content(
147
            array(
148
                'versionInfo' => $versionInfo,
149
                'internalFields' => array(),
150
            )
151
        );
152
    }
153
154
    /**
155
     * Creates a User object from $userId, $userRemoteId and $userVersionNo.
156
     *
157
     * @param mixed $userId
158
     * @param mixed $userRemoteId
159
     * @param int $userVersionNo
160
     *
161
     * @return \eZ\Publish\Core\Repository\Values\User\User
162
     */
163
    protected function getUser($userId, $userRemoteId, $userVersionNo)
164
    {
165
        return new User(
166
            array(
167
                'content' => $this->getContent(
168
                    $this->getVersionInfo(
169
                        $this->getContentInfo($userId, $userRemoteId),
170
                        $userVersionNo
171
                    )
172
                ),
173
            )
174
        );
175
    }
176
177
    /**
178
     * Returns a new UserGroup.
179
     *
180
     * @param mixed $groupId
181
     * @param mixed $groupRemoteId
182
     * @param int $groupVersioNo
183
     *
184
     * @return \eZ\Publish\Core\Repository\Values\User\UserGroup
185
     */
186
    protected function getUserGroup($groupId, $groupRemoteId, $groupVersioNo)
187
    {
188
        return new UserGroup(
189
            array(
190
                'content' => $this->getContent(
191
                    $this->getVersionInfo(
192
                        $this->getContentInfo($groupId, $groupRemoteId),
193
                        $groupVersioNo
194
                    )
195
                ),
196
            )
197
        );
198
    }
199
}
200