Completed
Push — ezp-30806-tmp-7.5 ( 069bb0...315c25 )
by
unknown
36:09 queued 14:41
created

ServiceTest::getUserGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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