Issues (3877)

CreatePushNotificationProviderCollectionTest.php (1 issue)

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
namespace SprykerTest\Zed\PushNotification\Business\Facade;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\DataBuilder\PushNotificationProviderBuilder;
12
use Generated\Shared\Transfer\PushNotificationProviderCollectionRequestTransfer;
13
use Generated\Shared\Transfer\PushNotificationProviderTransfer;
14
use Spryker\Shared\Kernel\Transfer\Exception\RequiredTransferPropertyException;
15
use SprykerTest\Zed\PushNotification\PushNotificationBusinessTester;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group SprykerTest
21
 * @group Zed
22
 * @group PushNotification
23
 * @group Business
24
 * @group Facade
25
 * @group CreatePushNotificationProviderCollectionTest
26
 * Add your own group annotations below this line
27
 */
28
class CreatePushNotificationProviderCollectionTest extends Unit
29
{
30
    /**
31
     * @var string
32
     */
33
    protected const PUSH_NOTIFICATION_PROVIDER_NAME = 'Push Notification Provider Name';
34
35
    /**
36
     * @uses \Spryker\Zed\PushNotification\Business\Validator\Rules\PushNotificationProvider\NameUniquenessPushNotificationProviderValidatorRule::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_IS_NOT_UNIQUE
37
     *
38
     * @var string
39
     */
40
    protected const GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_IS_NOT_UNIQUE = 'push_notification.validation.push_notification_provider_name_is_not_unique';
41
42
    /**
43
     * @uses \Spryker\Zed\PushNotification\Business\Validator\Rules\PushNotificationProvider\NameExistencePushNotificationProviderValidatorRule::GLOSSARY_KEY_VALIDATION_SERVICE_TYPE_NAME_EXISTS
44
     *
45
     * @var string
46
     */
47
    protected const GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_EXISTS = 'push_notification.validation.push_notification_provider_name_exists';
48
49
    /**
50
     * @uses \Spryker\Zed\PushNotification\Business\Validator\Rules\PushNotificationProvider\NameLengthPushNotificationProviderValidatorRule::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_WRONG_LENGTH
51
     *
52
     * @var string
53
     */
54
    protected const GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_WRONG_LENGTH = 'push_notification.validation.push_notification_provider_name_wrong_length';
55
56
    /**
57
     * @var \SprykerTest\Zed\PushNotification\PushNotificationBusinessTester
58
     */
59
    protected PushNotificationBusinessTester $tester;
60
61
    /**
62
     * @return void
63
     */
64
    protected function setUp(): void
65
    {
66
        parent::setUp();
67
68
        $this->tester->ensurePushNotificationTablesAreEmpty();
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    public function testShouldCreatePushNotificationProvider(): void
75
    {
76
        // Arrange
77
        $pushNotificationProviderTransfer = (new PushNotificationProviderBuilder())->build();
78
79
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
80
            ->addPushNotificationProvider($pushNotificationProviderTransfer)
81
            ->setIsTransactional(true);
82
83
        // Act
84
        $pushNotificationProviderCollectionResponseTransfer = $this->tester->getFacade()
85
            ->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
86
87
        // Assert
88
        $this->assertCount(0, $pushNotificationProviderCollectionResponseTransfer->getErrors());
89
        $this->assertSame(1, $this->tester->getPushNotificationProviderQuery()->count());
90
91
        /** @var \Generated\Shared\Transfer\PushNotificationProviderTransfer $persistedPushNotificationProviderTransfer */
92
        $persistedPushNotificationProviderTransfer = $pushNotificationProviderCollectionResponseTransfer->getPushNotificationProviders()->getIterator()->current();
93
94
        $this->assertEquals($pushNotificationProviderTransfer, $persistedPushNotificationProviderTransfer);
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    public function testShouldValidateNameExistence(): void
101
    {
102
        // Arrange
103
        $existingPushNotificationProviderTransfer = $this->tester->havePushNotificationProvider();
104
105
        $pushNotificationProviderTransfer = (new PushNotificationProviderBuilder([
106
            PushNotificationProviderTransfer::NAME => $existingPushNotificationProviderTransfer->getNameOrFail(),
107
        ]))->build();
108
109
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
110
            ->addPushNotificationProvider($pushNotificationProviderTransfer)
111
            ->setIsTransactional(true);
112
113
        // Act
114
        $pushNotificationProviderCollectionResponseTransfer = $this->tester->getFacade()
115
            ->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
116
117
        // Assert
118
        $this->assertCount(1, $pushNotificationProviderCollectionResponseTransfer->getErrors());
119
120
        /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */
121
        $errorTransfer = $pushNotificationProviderCollectionResponseTransfer->getErrors()->getIterator()->current();
122
123
        $this->assertSame('0', $errorTransfer->getEntityIdentifierOrFail());
124
        $this->assertSame(static::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_EXISTS, $errorTransfer->getMessageOrFail());
125
        $this->assertSame(1, $this->tester->getPushNotificationProviderQuery()->count());
126
    }
127
128
    /**
129
     * @return void
130
     */
131
    public function testShouldValidateNameUniqueness(): void
132
    {
133
        // Arrange
134
        $firstPushNotificationProviderTransfer = (new PushNotificationProviderBuilder([
135
            PushNotificationProviderTransfer::NAME => static::PUSH_NOTIFICATION_PROVIDER_NAME,
136
        ]))->build();
137
138
        $secondPushNotificationProviderTransfer = (new PushNotificationProviderBuilder([
139
            PushNotificationProviderTransfer::NAME => static::PUSH_NOTIFICATION_PROVIDER_NAME,
140
        ]))->build();
141
142
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
143
            ->addPushNotificationProvider($firstPushNotificationProviderTransfer)
144
            ->addPushNotificationProvider($secondPushNotificationProviderTransfer)
145
            ->setIsTransactional(true);
146
147
        // Act
148
        $pushNotificationProviderCollectionResponseTransfer = $this->tester->getFacade()
149
            ->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
150
151
        // Assert
152
        $this->assertCount(1, $pushNotificationProviderCollectionResponseTransfer->getErrors());
153
154
        /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */
155
        $errorTransfer = $pushNotificationProviderCollectionResponseTransfer->getErrors()->getIterator()->current();
156
157
        $this->assertSame('1', $errorTransfer->getEntityIdentifierOrFail());
158
        $this->assertSame(static::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_IS_NOT_UNIQUE, $errorTransfer->getMessageOrFail());
159
        $this->assertSame(0, $this->tester->getPushNotificationProviderQuery()->count());
160
    }
161
162
    /**
163
     * @dataProvider outOfLengthStringDataProvider
164
     *
165
     * @param string $name
166
     *
167
     * @return void
168
     */
169
    public function testShouldValidateNameLength(string $name): void
170
    {
171
        // Arrange
172
        $pushNotificationProviderTransfer = (new PushNotificationProviderBuilder([
173
            PushNotificationProviderTransfer::NAME => $name,
174
        ]))->build();
175
176
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
177
            ->addPushNotificationProvider($pushNotificationProviderTransfer)
178
            ->setIsTransactional(true);
179
180
        // Act
181
        $pushNotificationProviderCollectionResponseTransfer = $this->tester->getFacade()
182
            ->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
183
184
        // Assert
185
        $this->assertCount(1, $pushNotificationProviderCollectionResponseTransfer->getErrors());
186
187
        /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */
188
        $errorTransfer = $pushNotificationProviderCollectionResponseTransfer->getErrors()->getIterator()->current();
189
190
        $this->assertSame('0', $errorTransfer->getEntityIdentifierOrFail());
191
        $this->assertSame(static::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_WRONG_LENGTH, $errorTransfer->getMessageOrFail());
192
        $this->assertSame(0, $this->tester->getPushNotificationProviderQuery()->count());
193
    }
194
195
    /**
196
     * @return void
197
     */
198
    public function testShouldCreatePushNotificationProvidersForNonTransactionalMode(): void
199
    {
200
        // Arrange
201
        $firstPushNotificationProviderTransfer = (new PushNotificationProviderBuilder())->build();
202
203
        $secondPushNotificationProviderTransfer = (new PushNotificationProviderBuilder([
204
            PushNotificationProviderTransfer::NAME => '',
205
        ]))->build();
206
207
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
208
            ->addPushNotificationProvider($firstPushNotificationProviderTransfer)
209
            ->addPushNotificationProvider($secondPushNotificationProviderTransfer)
210
            ->setIsTransactional(false);
211
212
        // Act
213
        $pushNotificationProviderCollectionResponseTransfer = $this->tester->getFacade()
214
            ->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
215
216
        // Assert
217
        $this->assertCount(1, $pushNotificationProviderCollectionResponseTransfer->getErrors());
218
219
        /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */
220
        $errorTransfer = $pushNotificationProviderCollectionResponseTransfer->getErrors()->getIterator()->current();
221
222
        $this->assertSame('1', $errorTransfer->getEntityIdentifierOrFail());
223
        $this->assertSame(static::GLOSSARY_KEY_VALIDATION_PUSH_NOTIFICATION_PROVIDER_NAME_WRONG_LENGTH, $errorTransfer->getMessageOrFail());
224
        $this->assertSame(1, $this->tester->getPushNotificationProviderQuery()->count());
225
    }
226
227
    /**
228
     * @return void
229
     */
230
    public function testShouldThrowExceptionWhenIsTransactionalIsNotSet(): void
231
    {
232
        // Arrange
233
        $pushNotificationProviderTransfer = (new PushNotificationProviderBuilder())->build();
234
235
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
236
            ->addPushNotificationProvider($pushNotificationProviderTransfer);
237
238
        // Assert
239
        $this->expectException(RequiredTransferPropertyException::class);
240
241
        // Act
242
        $this->tester->getFacade()->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
243
    }
244
245
    /**
246
     * @return void
247
     */
248
    public function testShouldThrowExceptionWhenPushNotificationProvidersAreNotSet(): void
249
    {
250
        // Arrange
251
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
252
            ->setIsTransactional(true);
253
254
        // Assert
255
        $this->expectException(RequiredTransferPropertyException::class);
256
257
        // Act
258
        $this->tester->getFacade()->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
259
    }
260
261
    /**
262
     * @return void
263
     */
264
    public function testShouldThrowExceptionWhenPushNotificationProviderNameIsNotSet(): void
265
    {
266
        // Arrange
267
        $pushNotificationProviderTransfer = (new PushNotificationProviderBuilder())
268
            ->build()
269
            ->setName(null);
270
271
        $pushNotificationProviderCollectionRequestTransfer = (new PushNotificationProviderCollectionRequestTransfer())
272
            ->addPushNotificationProvider($pushNotificationProviderTransfer)
273
            ->setIsTransactional(true);
274
275
        // Assert
276
        $this->expectException(RequiredTransferPropertyException::class);
277
278
        // Act
279
        $this->tester->getFacade()->createPushNotificationProviderCollection($pushNotificationProviderCollectionRequestTransfer);
280
    }
281
282
    /**
283
     * @return array<list<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<list<string>> at position 2 could not be parsed: Expected '>' at position 2, but found 'list'.
Loading history...
284
     */
285
    protected function outOfLengthStringDataProvider(): array
286
    {
287
        return [
288
            [''],
289
            [str_repeat('a', 256)],
290
        ];
291
    }
292
}
293