Passed
Branch master (bbfb46)
by Jan
22:41 queued 14:44
created

RequestTransformerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 152
dl 0
loc 265
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Test\Framework\Routing;
4
5
use Doctrine\DBAL\Connection;
6
use PHPUnit\Framework\TestCase;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
9
use Shopware\Core\Framework\Context;
10
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
11
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
12
use Shopware\Core\Framework\Uuid\Uuid;
13
use Shopware\Core\PlatformRequest;
14
use Shopware\Core\SalesChannelRequest;
15
use Shopware\Storefront\Framework\Routing\RequestTransformer;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class RequestTransformerTest extends TestCase
19
{
20
    use IntegrationTestBehaviour;
21
    public const LOCALE_DE_DE_ISO = 'de-DE';
22
    public const LOCALE_EN_GB_ISO = 'en-GB';
23
24
    /**
25
     * @var RequestTransformer
26
     */
27
    private $requestBuilder;
28
29
    protected function setUp(): void
30
    {
31
        $this->requestBuilder = new RequestTransformer($this->getContainer()->get(Connection::class));
32
    }
33
34
    /**
35
     * @dataProvider domainProvider
36
     *
37
     * @param array[]           $salesChannels
38
     * @param ExpectedRequest[] $requests
39
     */
40
    public function testDomainResolving(array $salesChannels, array $requests): void
41
    {
42
        $this->createSalesChannels($salesChannels);
43
44
        /** @var ExpectedRequest $expectedRequest */
45
        foreach ($requests as $expectedRequest) {
46
            $request = Request::create($expectedRequest->url);
47
48
            $resolved = $this->requestBuilder->transform($request);
49
50
            static::assertSame($expectedRequest->salesChannelId, $resolved->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID));
51
52
            static::assertSame($expectedRequest->domainId, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_ID));
53
            static::assertSame($expectedRequest->isStorefrontRequest, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST));
54
            static::assertSame($expectedRequest->locale, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_LOCALE));
55
            static::assertSame($expectedRequest->currency, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_CURRENCY_ID));
56
            static::assertSame($expectedRequest->snippetSetId, $resolved->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_SNIPPET_SET_ID));
57
            static::assertSame($expectedRequest->baseUrl, $resolved->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL), $expectedRequest->url);
58
            static::assertSame($expectedRequest->language, $resolved->headers->get(PlatformRequest::HEADER_LANGUAGE_ID));
59
        }
60
    }
61
62
    public function domainProvider(): array
63
    {
64
        $germanId = Uuid::randomHex();
65
        $englishId = Uuid::randomHex();
66
        $gerUkId = Uuid::randomHex();
67
68
        $gerDomainId = Uuid::randomHex();
69
        $ukDomainId = Uuid::randomHex();
70
71
        $snippetSetDE = $this->getSnippetSetIdForLocale(self::LOCALE_DE_DE_ISO);
72
        $snippetSetEN = $this->getSnippetSetIdForLocale(self::LOCALE_EN_GB_ISO);
73
74
        return [
75
            'single' => [
76
                [$this->getGermanSalesChannel($germanId, $gerDomainId, 'http://german.test')],
77
                [
78
                    new ExpectedRequest('http://german.test', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
79
                    new ExpectedRequest('http://german.test/', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
80
                    new ExpectedRequest('http://german.test/foobar', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
81
                ],
82
            ],
83
            'two' => [
84
                [
85
                    $this->getGermanSalesChannel($germanId, $gerDomainId, 'http://german.test'),
86
                    $this->getEnglishSalesChannel($englishId, $ukDomainId, 'http://english.test'),
87
                ],
88
                [
89
                    new ExpectedRequest('http://german.test', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
90
                    new ExpectedRequest('http://german.test/', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
91
                    new ExpectedRequest('http://german.test/foobar', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
92
93
                    new ExpectedRequest('http://english.test', '', $ukDomainId, $englishId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
94
                    new ExpectedRequest('http://english.test/', '', $ukDomainId, $englishId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
95
                    new ExpectedRequest('http://english.test/foobar', '', $ukDomainId, $englishId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
96
97
                    new ExpectedRequest('http://english.test/navigation/1', '', $ukDomainId, $englishId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
98
                    new ExpectedRequest('http://german.test/navigation/1', '', $gerDomainId, $germanId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
99
                ],
100
            ],
101
            'single-with-ger-and-uk-domain' => [
102
                [
103
                    $this->getSalesChannelWithGerAndUkDomain($gerUkId, $gerDomainId, 'http://german.test', $ukDomainId, 'http://english.test'),
104
                ],
105
                [
106
                    new ExpectedRequest('http://german.test', '', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
107
                    new ExpectedRequest('http://german.test/', '', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
108
                    new ExpectedRequest('http://german.test/foobar', '', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
109
110
                    new ExpectedRequest('http://english.test', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
111
                    new ExpectedRequest('http://english.test/', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
112
                    new ExpectedRequest('http://english.test/foobar', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
113
                ],
114
            ],
115
            'two-domains-same-host-different-path' => [
116
                [
117
                    $this->getSalesChannelWithGerAndUkDomain($gerUkId, $gerDomainId, 'http://saleschannel.test/de', $ukDomainId, 'http://saleschannel.test/en'),
118
                ],
119
                [
120
                    new ExpectedRequest('http://saleschannel.test/de', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
121
                    new ExpectedRequest('http://saleschannel.test/de/', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
122
                    new ExpectedRequest('http://saleschannel.test/de/foobar', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
123
124
                    new ExpectedRequest('http://saleschannel.test/en', '/en', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
125
                    new ExpectedRequest('http://saleschannel.test/en/', '/en', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
126
                    new ExpectedRequest('http://saleschannel.test/en/foobar', '/en', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
127
128
                    new ExpectedRequest('http://saleschannel.test/de/navigation/1', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
129
                    new ExpectedRequest('http://saleschannel.test/en/navigation/1', '/en', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
130
131
                    new ExpectedRequest('http://saleschannel.test/de/de/navigation/1', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
132
                    new ExpectedRequest('http://saleschannel.test/en/en/navigation/1', '/en', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
133
                ],
134
            ],
135
            'two-domains-same-host-extended-path' => [
136
                [
137
                    $this->getSalesChannelWithGerAndUkDomain($gerUkId, $gerDomainId, 'http://saleschannel.test/de', $ukDomainId, 'http://saleschannel.test'),
138
                ],
139
                [
140
                    new ExpectedRequest('http://saleschannel.test/de', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
141
                    new ExpectedRequest('http://saleschannel.test/de/', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
142
                    new ExpectedRequest('http://saleschannel.test/de/foobar', '/de', $gerDomainId, $gerUkId, true, self::LOCALE_DE_DE_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM_DE, $snippetSetDE),
143
144
                    new ExpectedRequest('http://saleschannel.test', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
145
                    new ExpectedRequest('http://saleschannel.test/', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
146
                    new ExpectedRequest('http://saleschannel.test/foobar', '', $ukDomainId, $gerUkId, true, self::LOCALE_EN_GB_ISO, Defaults::CURRENCY, Defaults::LANGUAGE_SYSTEM, $snippetSetEN),
147
                ],
148
            ],
149
            'inactive' => [
150
                [
151
                    $this->getInactiveSalesChannel($germanId, $gerDomainId, 'http://inactive.test'),
152
                ],
153
                [
154
                    new ExpectedRequest('http://inactive.test', null, null, null, null, null, null, null, null),
155
                    new ExpectedRequest('http://inactive.test/', null, null, null, null, null, null, null, null),
156
                    new ExpectedRequest('http://inactive.test/foobar', null, null, null, null, null, null, null, null),
157
                ],
158
            ],
159
        ];
160
    }
161
162
    private function getEnglishSalesChannel(string $salesChannelId, string $domainId, string $url): array
163
    {
164
        return [
165
            'id' => $salesChannelId,
166
            'name' => 'english',
167
            'languages' => [
168
                ['id' => Defaults::LANGUAGE_SYSTEM],
169
            ],
170
            'domains' => [
171
                [
172
                    'id' => $domainId,
173
                    'url' => $url,
174
                    'languageId' => Defaults::LANGUAGE_SYSTEM,
175
                    'currencyId' => Defaults::CURRENCY,
176
                    'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_EN_GB_ISO),
177
                ],
178
            ],
179
        ];
180
    }
181
182
    private function getGermanSalesChannel(string $salesChannelId, string $domainId, string $url): array
183
    {
184
        return [
185
            'id' => $salesChannelId,
186
            'name' => 'german',
187
            'languages' => [
188
                ['id' => Defaults::LANGUAGE_SYSTEM_DE],
189
            ],
190
            'domains' => [
191
                [
192
                    'id' => $domainId,
193
                    'url' => $url,
194
                    'languageId' => Defaults::LANGUAGE_SYSTEM_DE,
195
                    'currencyId' => Defaults::CURRENCY,
196
                    'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_DE_DE_ISO),
197
                ],
198
            ],
199
        ];
200
    }
201
202
    private function getSalesChannelWithGerAndUkDomain(string $salesChannelId, string $gerDomainId, string $gerUrl, string $ukDomainId, string $ukUrl): array
203
    {
204
        return [
205
            'id' => $salesChannelId,
206
            'name' => 'english',
207
            'languages' => [
208
                ['id' => Defaults::LANGUAGE_SYSTEM],
209
                ['id' => Defaults::LANGUAGE_SYSTEM_DE],
210
            ],
211
            'domains' => [
212
                [
213
                    'id' => $gerDomainId,
214
                    'url' => $gerUrl,
215
                    'languageId' => Defaults::LANGUAGE_SYSTEM_DE,
216
                    'currencyId' => Defaults::CURRENCY,
217
                    'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_DE_DE_ISO),
218
                ],
219
                [
220
                    'id' => $ukDomainId,
221
                    'url' => $ukUrl,
222
                    'languageId' => Defaults::LANGUAGE_SYSTEM,
223
                    'currencyId' => Defaults::CURRENCY,
224
                    'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_EN_GB_ISO),
225
                ],
226
            ],
227
        ];
228
    }
229
230
    private function getInactiveSalesChannel(string $salesChannelId, string $domainId, string $url): array
231
    {
232
        return [
233
            'id' => $salesChannelId,
234
            'name' => 'inactive sales channel',
235
            'active' => false,
236
            'languages' => [
237
                ['id' => Defaults::LANGUAGE_SYSTEM_DE],
238
            ],
239
            'domains' => [
240
                [
241
                    'id' => $domainId,
242
                    'url' => $url,
243
                    'languageId' => Defaults::LANGUAGE_SYSTEM_DE,
244
                    'currencyId' => Defaults::CURRENCY,
245
                    'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_DE_DE_ISO),
246
                ],
247
            ],
248
        ];
249
    }
250
251
    private function createSalesChannels($salesChannels): EntityWrittenContainerEvent
252
    {
253
        $salesChannels = array_map(function ($salesChannelData) {
254
            $defaults = [
255
                'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
256
                'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'),
257
                'languageId' => Defaults::LANGUAGE_SYSTEM,
258
                'snippetSetId' => $this->getSnippetSetIdForLocale(self::LOCALE_EN_GB_ISO),
259
                'currencyId' => Defaults::CURRENCY,
260
                'currencyVersionId' => Defaults::LIVE_VERSION,
261
                'paymentMethodId' => $this->getValidPaymentMethodId(),
262
                'paymentMethodVersionId' => Defaults::LIVE_VERSION,
263
                'shippingMethodId' => $this->getValidShippingMethodId(),
264
                'shippingMethodVersionId' => Defaults::LIVE_VERSION,
265
                'navigationCategoryId' => $this->getValidCategoryId(),
266
                'navigationCategoryVersionId' => Defaults::LIVE_VERSION,
267
                'countryId' => $this->getValidCountryId(),
268
                'countryVersionId' => Defaults::LIVE_VERSION,
269
                'currencies' => [['id' => Defaults::CURRENCY]],
270
                'languages' => [['id' => Defaults::LANGUAGE_SYSTEM]],
271
                'paymentMethods' => [['id' => $this->getValidPaymentMethodId()]],
272
                'shippingMethods' => [['id' => $this->getValidShippingMethodId()]],
273
                'countries' => [['id' => $this->getValidCountryId()]],
274
                'customerGroupId' => Defaults::FALLBACK_CUSTOMER_GROUP,
275
            ];
276
277
            return array_merge_recursive($defaults, $salesChannelData);
278
        }, $salesChannels);
279
280
        $salesChannelRepository = $this->getContainer()->get('sales_channel.repository');
281
282
        return $salesChannelRepository->create($salesChannels, Context::createDefaultContext());
283
    }
284
}
285
286
class ExpectedRequest
287
{
288
    /** @var string */
289
    public $url;
290
291
    /** @var string|null */
292
    public $baseUrl;
293
294
    /** @var string */
295
    public $domainId;
296
297
    /** @var string */
298
    public $salesChannelId;
299
300
    /** @var bool */
301
    public $isStorefrontRequest;
302
303
    /** @var string */
304
    public $locale;
305
306
    /** @var string */
307
    public $currency;
308
309
    /** @var string */
310
    public $language;
311
312
    /** @var string */
313
    public $snippetSetId;
314
315
    public function __construct(
316
        string $url,
317
        ?string $baseUrl,
318
        ?string $domainId,
319
        ?string $salesChannelId,
320
        ?bool $isStorefrontRequest,
321
        ?string $locale,
322
        ?string $currency,
323
        ?string $language,
324
        ?string $snippetSetId
325
    ) {
326
        $this->url = $url;
327
        $this->domainId = $domainId;
328
        $this->salesChannelId = $salesChannelId;
329
        $this->isStorefrontRequest = $isStorefrontRequest;
330
        $this->locale = $locale;
331
        $this->currency = $currency;
332
        $this->language = $language;
333
        $this->snippetSetId = $snippetSetId;
334
        $this->baseUrl = $baseUrl;
335
    }
336
}
337