Passed
Push — master ( 60a4ae...29fcc8 )
by
unknown
46:42 queued 14:26
created

testSendCmsPagesToMessageBrokerWithDifferentPageStatus()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 31
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 51
rs 8.4906

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
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace SprykerTest\Zed\Cms\Business\MessageBroker;
11
12
use ArrayObject;
13
use Codeception\Test\Unit;
14
use Generated\Shared\DataBuilder\CmsPageBuilder;
15
use Generated\Shared\DataBuilder\CmsVersionBuilder;
16
use Generated\Shared\Transfer\CmsPageMessageBrokerRequestTransfer;
17
use Generated\Shared\Transfer\CmsPagePublishedTransfer;
18
use Generated\Shared\Transfer\CmsPageTransfer;
19
use Generated\Shared\Transfer\CmsPageUnpublishedTransfer;
20
use Generated\Shared\Transfer\CmsVersionTransfer;
21
use Spryker\Zed\Cms\Business\Extractor\DataExtractorInterface;
22
use Spryker\Zed\Cms\Business\MessageBroker\CmsPageMessageBrokerPublisher;
23
use Spryker\Zed\Cms\Business\Page\CmsPageReaderInterface;
24
use Spryker\Zed\Cms\Business\Page\LocaleCmsPageDataExpanderInterface;
25
use Spryker\Zed\Cms\Business\Version\VersionFinderInterface;
26
use Spryker\Zed\Cms\CmsConfig;
27
use Spryker\Zed\Cms\Dependency\Facade\CmsToMessageBrokerFacadeInterface;
28
use Spryker\Zed\Cms\Persistence\CmsRepositoryInterface;
29
30
/**
31
 * Auto-generated group annotations
32
 *
33
 * @group SprykerTest
34
 * @group Zed
35
 * @group Cms
36
 * @group Business
37
 * @group MessageBroker
38
 * @group CmsPageMessageBrokerPublisherTest
39
 * Add your own group annotations below this line
40
 */
41
class CmsPageMessageBrokerPublisherTest extends Unit
42
{
43
    /**
44
     * @var \SprykerTest\Zed\Cms\CmsBusinessTester
45
     */
46
    protected $tester;
47
48
    /**
49
     * @var string
50
     */
51
    protected const TEST_TENANT_IDENTIFIER = 'test-tenant';
52
53
    /**
54
     * @var int
55
     */
56
    protected const TEST_CMS_PAGE_ID = 123;
57
58
    /**
59
     * @var int
60
     */
61
    protected const TEST_CMS_VERSION_ID = 456;
62
63
    /**
64
     * @var int
65
     */
66
    protected const TEST_FK_PAGE = 789;
67
68
    /**
69
     * @return void
70
     */
71
    public function testSendCmsPagesToMessageBrokerWithVersionIds(): void
72
    {
73
        // Arrange
74
        $cmsVersionIds = [static::TEST_CMS_VERSION_ID];
75
        $cmsPageMessageBrokerRequestTransfer = (new CmsPageMessageBrokerRequestTransfer())
76
            ->setCmsVersionIds($cmsVersionIds);
77
78
        $cmsVersionTransfer = $this->createCmsVersionTransfer();
79
        $cmsVersionTransfers = new ArrayObject([$cmsVersionTransfer]);
80
        $cmsPageTransfer = $this->createCmsPageTransfer();
81
82
        $mocks = $this->createPublisherDependenciesMock($cmsPageTransfer);
83
        $cmsConfig = $this->createConfigWithTenantIdentifier();
84
85
        $mocks['versionFinder']
86
            ->expects($this->once())
87
            ->method('findCmsVersionsByIds')
88
            ->with($cmsVersionIds)
89
            ->willReturn($cmsVersionTransfers);
90
91
        $mocks['messageBrokerFacade']
92
            ->expects($this->once())
93
            ->method('sendMessage')
94
            ->with($this->callback(function ($transfer) {
95
                return $transfer instanceof CmsPagePublishedTransfer
96
                    && $transfer->getId() === static::TEST_FK_PAGE
97
                    && $transfer->getCmsPage() !== null
98
                    && $transfer->getMessageAttributes()->getTenantIdentifier() === static::TEST_TENANT_IDENTIFIER;
99
            }));
100
101
        $publisher = $this->createPublisher($mocks, $cmsConfig);
102
103
        // Act
104
        $publisher->sendCmsPagesToMessageBroker($cmsPageMessageBrokerRequestTransfer);
105
    }
106
107
    /**
108
     * @dataProvider pageStatusDataProvider
109
     *
110
     * @param bool $isActive
111
     * @param bool $isSearchable
112
     * @param string $expectedMessageType
113
     * @param bool $shouldCallVersionFinder
114
     *
115
     * @return void
116
     */
117
    public function testSendCmsPagesToMessageBrokerWithDifferentPageStatus(
118
        bool $isActive,
119
        bool $isSearchable,
120
        string $expectedMessageType,
121
        bool $shouldCallVersionFinder
122
    ): void {
123
        // Arrange
124
        $cmsPageIds = [static::TEST_CMS_PAGE_ID];
125
        $cmsPageMessageBrokerRequestTransfer = (new CmsPageMessageBrokerRequestTransfer())
126
            ->setCmsPageIds($cmsPageIds);
127
128
        $cmsPageTransfer = $this->createCmsPageTransfer($isActive, $isSearchable);
129
        $cmsVersionTransfer = $this->createCmsVersionTransfer();
130
131
        $mocks = $this->createPublisherDependenciesMock($cmsPageTransfer);
132
        $cmsConfig = $this->createConfigWithTenantIdentifier();
133
134
        if ($shouldCallVersionFinder) {
135
            $mocks['versionFinder']
136
                ->expects($this->once())
137
                ->method('findLatestCmsVersionByIdCmsPage')
138
                ->with(static::TEST_CMS_PAGE_ID)
139
                ->willReturn($cmsVersionTransfer);
140
        } else {
141
            $mocks['versionFinder']
142
                ->expects($this->never())
143
                ->method('findLatestCmsVersionByIdCmsPage');
144
        }
145
146
        $mocks['messageBrokerFacade']
147
            ->expects($this->once())
148
            ->method('sendMessage')
149
            ->with($this->callback(function ($transfer) use ($expectedMessageType) {
0 ignored issues
show
Unused Code introduced by
The import $expectedMessageType is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
150
                if ($transfer instanceof CmsPagePublishedTransfer) {
151
                    return $transfer->getId() === static::TEST_FK_PAGE
152
                        && $transfer->getCmsPage() !== null
153
                        && $transfer->getMessageAttributes()->getTenantIdentifier() === static::TEST_TENANT_IDENTIFIER;
154
                }
155
156
                if ($transfer instanceof CmsPageUnpublishedTransfer) {
157
                    return $transfer->getId() === static::TEST_CMS_PAGE_ID
158
                        && $transfer->getMessageAttributes()->getTenantIdentifier() === static::TEST_TENANT_IDENTIFIER;
159
                }
160
161
                return false;
162
            }));
163
164
        $publisher = $this->createPublisher($mocks, $cmsConfig);
165
166
        // Act
167
        $publisher->sendCmsPagesToMessageBroker($cmsPageMessageBrokerRequestTransfer);
168
    }
169
170
    /**
171
     * @return void
172
     */
173
    public function testSendCmsPagesToMessageBrokerHandlesEmptyVersions(): void
174
    {
175
        // Arrange
176
        $cmsVersionIds = [static::TEST_CMS_VERSION_ID];
177
        $cmsPageMessageBrokerRequestTransfer = (new CmsPageMessageBrokerRequestTransfer())
178
            ->setCmsVersionIds($cmsVersionIds);
179
180
        $emptyVersionTransfers = new ArrayObject();
181
182
        $mocks = $this->createPublisherDependenciesMock();
183
        $cmsConfig = $this->createConfigWithTenantIdentifier();
184
185
        $mocks['versionFinder']
186
            ->expects($this->once())
187
            ->method('findCmsVersionsByIds')
188
            ->with($cmsVersionIds)
189
            ->willReturn($emptyVersionTransfers);
190
191
        $mocks['messageBrokerFacade']
192
            ->expects($this->never())
193
            ->method('sendMessage');
194
195
        $publisher = $this->createPublisher($mocks, $cmsConfig);
196
197
        // Act
198
        $publisher->sendCmsPagesToMessageBroker($cmsPageMessageBrokerRequestTransfer);
199
    }
200
201
    /**
202
     * @return void
203
     */
204
    public function testSendCmsPagesToMessageBrokerHandlesNonExistentPage(): void
205
    {
206
        // Arrange
207
        $cmsPageIds = [static::TEST_CMS_PAGE_ID];
208
        $cmsPageMessageBrokerRequestTransfer = (new CmsPageMessageBrokerRequestTransfer())
209
            ->setCmsPageIds($cmsPageIds);
210
211
        $mocks = $this->createPublisherDependenciesMock(null);
212
        $cmsConfig = $this->createConfigWithTenantIdentifier();
213
214
        $mocks['messageBrokerFacade']
215
            ->expects($this->never())
216
            ->method('sendMessage');
217
218
        $publisher = $this->createPublisher($mocks, $cmsConfig);
219
220
        // Act
221
        $publisher->sendCmsPagesToMessageBroker($cmsPageMessageBrokerRequestTransfer);
222
    }
223
224
    /**
225
     * @return array<string, array<mixed>>
226
     */
227
    public function pageStatusDataProvider(): array
228
    {
229
        return [
230
            'active and searchable page sends published message' => [
231
                true, // isActive
232
                true, // isSearchable
233
                CmsPagePublishedTransfer::class, // expectedMessageType
234
                true, // shouldCallVersionFinder
235
            ],
236
            'inactive page sends unpublished message' => [
237
                false, // isActive
238
                false, // isSearchable
239
                CmsPageUnpublishedTransfer::class, // expectedMessageType
240
                false, // shouldCallVersionFinder
241
            ],
242
            'active but not searchable page sends unpublished message' => [
243
                true, // isActive
244
                false, // isSearchable
245
                CmsPageUnpublishedTransfer::class, // expectedMessageType
246
                false, // shouldCallVersionFinder
247
            ],
248
        ];
249
    }
250
251
    /**
252
     * @param bool $isActive
253
     * @param bool $isSearchable
254
     *
255
     * @return \Generated\Shared\Transfer\CmsPageTransfer
256
     */
257
    protected function createCmsPageTransfer(bool $isActive = true, bool $isSearchable = true): CmsPageTransfer
258
    {
259
        return (new CmsPageBuilder([
260
            'idCmsPage' => static::TEST_CMS_PAGE_ID,
261
            'fkPage' => static::TEST_FK_PAGE,
262
            'isActive' => $isActive,
263
            'isSearchable' => $isSearchable,
264
        ]))->build();
265
    }
266
267
    /**
268
     * @return \Generated\Shared\Transfer\CmsVersionTransfer
269
     */
270
    protected function createCmsVersionTransfer(): CmsVersionTransfer
271
    {
272
        return (new CmsVersionBuilder([
273
            'idCmsVersion' => static::TEST_CMS_VERSION_ID,
274
            'fkCmsPage' => static::TEST_CMS_PAGE_ID,
275
            'createdAt' => '2023-01-01 12:00:00',
276
        ]))->build();
277
    }
278
279
    /**
280
     * @param \Generated\Shared\Transfer\CmsPageTransfer|null $cmsPageTransfer
281
     *
282
     * @return array<string, mixed>
283
     */
284
    protected function createPublisherDependenciesMock(?CmsPageTransfer $cmsPageTransfer = null): array
285
    {
286
        $cmsPageReaderMock = $this->getMockBuilder(CmsPageReaderInterface::class)->getMock();
287
288
        if ($cmsPageTransfer !== null) {
289
            $cmsPageReaderMock
290
                ->expects($this->once())
291
                ->method('findCmsPageById')
292
                ->with(static::TEST_CMS_PAGE_ID)
293
                ->willReturn($cmsPageTransfer);
294
        }
295
296
        return [
297
            'messageBrokerFacade' => $this->getMockBuilder(CmsToMessageBrokerFacadeInterface::class)->getMock(),
298
            'cmsPageReader' => $cmsPageReaderMock,
299
            'cmsRepository' => $this->getMockBuilder(CmsRepositoryInterface::class)->getMock(),
300
            'versionFinder' => $this->getMockBuilder(VersionFinderInterface::class)->getMock(),
301
            'dataExtractor' => $this->getMockBuilder(DataExtractorInterface::class)->getMock(),
302
            'localeCmsPageDataExpander' => $this->getMockBuilder(LocaleCmsPageDataExpanderInterface::class)->getMock(),
303
        ];
304
    }
305
306
    /**
307
     * @return \Spryker\Zed\Cms\CmsConfig
308
     */
309
    protected function createConfigWithTenantIdentifier(): CmsConfig
310
    {
311
        $this->tester->mockConfigMethod('getTenantIdentifier', static::TEST_TENANT_IDENTIFIER);
312
313
        return $this->tester->getModuleConfig();
314
    }
315
316
    /**
317
     * @param array<string, mixed> $mocks
318
     * @param \Spryker\Zed\Cms\CmsConfig $cmsConfig
319
     *
320
     * @return \Spryker\Zed\Cms\Business\MessageBroker\CmsPageMessageBrokerPublisher
321
     */
322
    protected function createPublisher(array $mocks, CmsConfig $cmsConfig): CmsPageMessageBrokerPublisher
323
    {
324
        return new CmsPageMessageBrokerPublisher(
325
            $mocks['messageBrokerFacade'],
326
            $mocks['cmsPageReader'],
327
            $mocks['cmsRepository'],
328
            $mocks['versionFinder'],
329
            $mocks['dataExtractor'],
330
            $mocks['localeCmsPageDataExpander'],
331
            $cmsConfig,
332
        );
333
    }
334
}
335