Completed
Push — ezp-30616-follow-up ( 2e0607...b74676 )
by
unknown
35:08 queued 20:12
created

URLWildcardServiceTest::testCreateEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 29
loc 29
rs 9.456
c 0
b 0
f 0
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\Event\Tests;
8
9
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeCreateEvent as BeforeCreateEventInterface;
10
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeRemoveEvent as BeforeRemoveEventInterface;
11
use eZ\Publish\API\Repository\Events\URLWildcard\BeforeTranslateEvent as BeforeTranslateEventInterface;
12
use eZ\Publish\API\Repository\Events\URLWildcard\CreateEvent as CreateEventInterface;
13
use eZ\Publish\API\Repository\Events\URLWildcard\RemoveEvent as RemoveEventInterface;
14
use eZ\Publish\API\Repository\Events\URLWildcard\TranslateEvent as TranslateEventInterface;
15
use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
16
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
17
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
18
use eZ\Publish\Core\Event\URLWildcardService;
19
20
class URLWildcardServiceTest extends AbstractServiceTest
21
{
22
    public function testRemoveEvents()
23
    {
24
        $traceableEventDispatcher = $this->getEventDispatcher(
25
            BeforeRemoveEventInterface::class,
26
            RemoveEventInterface::class
27
        );
28
29
        $parameters = [
30
            $this->createMock(URLWildcard::class),
31
        ];
32
33
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
34
35
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
36
        $service->remove(...$parameters);
37
38
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
39
40
        $this->assertSame($calledListeners, [
41
            [BeforeRemoveEventInterface::class, 0],
42
            [RemoveEventInterface::class, 0],
43
        ]);
44
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
45
    }
46
47
    public function testRemoveStopPropagationInBeforeEvents()
48
    {
49
        $traceableEventDispatcher = $this->getEventDispatcher(
50
            BeforeRemoveEventInterface::class,
51
            RemoveEventInterface::class
52
        );
53
54
        $parameters = [
55
            $this->createMock(URLWildcard::class),
56
        ];
57
58
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
59
60
        $traceableEventDispatcher->addListener(BeforeRemoveEventInterface::class, function (BeforeRemoveEventInterface $event) {
61
            $event->stopPropagation();
62
        }, 10);
63
64
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
65
        $service->remove(...$parameters);
66
67
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
68
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
69
70
        $this->assertSame($calledListeners, [
71
            [BeforeRemoveEventInterface::class, 10],
72
        ]);
73
        $this->assertSame($notCalledListeners, [
74
            [BeforeRemoveEventInterface::class, 0],
75
            [RemoveEventInterface::class, 0],
76
        ]);
77
    }
78
79 View Code Duplication
    public function testCreateEvents()
80
    {
81
        $traceableEventDispatcher = $this->getEventDispatcher(
82
            BeforeCreateEventInterface::class,
83
            CreateEventInterface::class
84
        );
85
86
        $parameters = [
87
            'random_value_5cff79c316c1f5.58580131',
88
            'random_value_5cff79c316c223.93334332',
89
            'random_value_5cff79c316c237.08397355',
90
        ];
91
92
        $urlWildcard = $this->createMock(URLWildcard::class);
93
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
94
        $innerServiceMock->method('create')->willReturn($urlWildcard);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
96
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
97
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
98
99
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
100
101
        $this->assertSame($urlWildcard, $result);
102
        $this->assertSame($calledListeners, [
103
            [BeforeCreateEventInterface::class, 0],
104
            [CreateEventInterface::class, 0],
105
        ]);
106
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
107
    }
108
109 View Code Duplication
    public function testReturnCreateResultInBeforeEvents()
110
    {
111
        $traceableEventDispatcher = $this->getEventDispatcher(
112
            BeforeCreateEventInterface::class,
113
            CreateEventInterface::class
114
        );
115
116
        $parameters = [
117
            'random_value_5cff79c316c2d5.26653678',
118
            'random_value_5cff79c316c2e7.55400833',
119
            'random_value_5cff79c316c2f8.59874187',
120
        ];
121
122
        $urlWildcard = $this->createMock(URLWildcard::class);
123
        $eventUrlWildcard = $this->createMock(URLWildcard::class);
124
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
125
        $innerServiceMock->method('create')->willReturn($urlWildcard);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
        $traceableEventDispatcher->addListener(BeforeCreateEventInterface::class, function (BeforeCreateEventInterface $event) use ($eventUrlWildcard) {
128
            $event->setUrlWildcard($eventUrlWildcard);
129
        }, 10);
130
131
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
132
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
133
134
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
135
136
        $this->assertSame($eventUrlWildcard, $result);
137
        $this->assertSame($calledListeners, [
138
            [BeforeCreateEventInterface::class, 10],
139
            [BeforeCreateEventInterface::class, 0],
140
            [CreateEventInterface::class, 0],
141
        ]);
142
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
143
    }
144
145 View Code Duplication
    public function testCreateStopPropagationInBeforeEvents()
146
    {
147
        $traceableEventDispatcher = $this->getEventDispatcher(
148
            BeforeCreateEventInterface::class,
149
            CreateEventInterface::class
150
        );
151
152
        $parameters = [
153
            'random_value_5cff79c316c359.46056769',
154
            'random_value_5cff79c316c361.53134429',
155
            'random_value_5cff79c316c374.82657815',
156
        ];
157
158
        $urlWildcard = $this->createMock(URLWildcard::class);
159
        $eventUrlWildcard = $this->createMock(URLWildcard::class);
160
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
161
        $innerServiceMock->method('create')->willReturn($urlWildcard);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
163
        $traceableEventDispatcher->addListener(BeforeCreateEventInterface::class, function (BeforeCreateEventInterface $event) use ($eventUrlWildcard) {
164
            $event->setUrlWildcard($eventUrlWildcard);
165
            $event->stopPropagation();
166
        }, 10);
167
168
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
169
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
170
171
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
172
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
173
174
        $this->assertSame($eventUrlWildcard, $result);
175
        $this->assertSame($calledListeners, [
176
            [BeforeCreateEventInterface::class, 10],
177
        ]);
178
        $this->assertSame($notCalledListeners, [
179
            [BeforeCreateEventInterface::class, 0],
180
            [CreateEventInterface::class, 0],
181
        ]);
182
    }
183
184 View Code Duplication
    public function testTranslateEvents()
185
    {
186
        $traceableEventDispatcher = $this->getEventDispatcher(
187
            BeforeTranslateEventInterface::class,
188
            TranslateEventInterface::class
189
        );
190
191
        $parameters = [
192
            'random_value_5cff79c316cfa7.72466150',
193
        ];
194
195
        $result = $this->createMock(URLWildcardTranslationResult::class);
196
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
197
        $innerServiceMock->method('translate')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
199
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
200
        $result = $service->translate(...$parameters);
201
202
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
203
204
        $this->assertSame($result, $result);
205
        $this->assertSame($calledListeners, [
206
            [BeforeTranslateEventInterface::class, 0],
207
            [TranslateEventInterface::class, 0],
208
        ]);
209
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
210
    }
211
212 View Code Duplication
    public function testReturnTranslateResultInBeforeEvents()
213
    {
214
        $traceableEventDispatcher = $this->getEventDispatcher(
215
            BeforeTranslateEventInterface::class,
216
            TranslateEventInterface::class
217
        );
218
219
        $parameters = [
220
            'random_value_5cff79c316d370.25863709',
221
        ];
222
223
        $result = $this->createMock(URLWildcardTranslationResult::class);
224
        $eventResult = $this->createMock(URLWildcardTranslationResult::class);
225
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
226
        $innerServiceMock->method('translate')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
227
228
        $traceableEventDispatcher->addListener(BeforeTranslateEventInterface::class, function (BeforeTranslateEventInterface $event) use ($eventResult) {
229
            $event->setResult($eventResult);
230
        }, 10);
231
232
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
233
        $result = $service->translate(...$parameters);
234
235
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
236
237
        $this->assertSame($eventResult, $result);
238
        $this->assertSame($calledListeners, [
239
            [BeforeTranslateEventInterface::class, 10],
240
            [BeforeTranslateEventInterface::class, 0],
241
            [TranslateEventInterface::class, 0],
242
        ]);
243
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
244
    }
245
246 View Code Duplication
    public function testTranslateStopPropagationInBeforeEvents()
247
    {
248
        $traceableEventDispatcher = $this->getEventDispatcher(
249
            BeforeTranslateEventInterface::class,
250
            TranslateEventInterface::class
251
        );
252
253
        $parameters = [
254
            'random_value_5cff79c316d3f9.73226122',
255
        ];
256
257
        $result = $this->createMock(URLWildcardTranslationResult::class);
258
        $eventResult = $this->createMock(URLWildcardTranslationResult::class);
259
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
260
        $innerServiceMock->method('translate')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
261
262
        $traceableEventDispatcher->addListener(BeforeTranslateEventInterface::class, function (BeforeTranslateEventInterface $event) use ($eventResult) {
263
            $event->setResult($eventResult);
264
            $event->stopPropagation();
265
        }, 10);
266
267
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
268
        $result = $service->translate(...$parameters);
269
270
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
271
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
272
273
        $this->assertSame($eventResult, $result);
274
        $this->assertSame($calledListeners, [
275
            [BeforeTranslateEventInterface::class, 10],
276
        ]);
277
        $this->assertSame($notCalledListeners, [
278
            [BeforeTranslateEventInterface::class, 0],
279
            [TranslateEventInterface::class, 0],
280
        ]);
281
    }
282
}
283