Skip to content

Commit 353e20e

Browse files
committed
test: error resolution controller
1 parent 3e7e28c commit 353e20e

File tree

3 files changed

+334
-1
lines changed

3 files changed

+334
-1
lines changed

src/Migration/ErrorResolution/MigrationFieldExampleGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
2828
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
2929
use Shopware\Core\Framework\DataAbstractionLayer\Field\TaxFreeConfigField;
30+
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
3031
use Shopware\Core\Framework\DataAbstractionLayer\Field\VariantListingConfigField;
3132
use Shopware\Core\Framework\Log\Package;
3233

@@ -117,7 +118,7 @@ private static function getScalarDefault(Field $field): mixed
117118
$field instanceof IntField => 0,
118119
$field instanceof FloatField => 0.1,
119120
$field instanceof BoolField => false,
120-
$field instanceof StringField => '[string]',
121+
$field instanceof StringField, $field instanceof TranslatedField => '[string]',
121122
$field instanceof IdField, $field instanceof FkField => '[uuid]',
122123
$field instanceof DateField => \sprintf('[date (%s)]', Defaults::STORAGE_DATE_FORMAT),
123124
$field instanceof DateTimeField => \sprintf('[datetime (%s)]', Defaults::STORAGE_DATE_TIME_FORMAT),
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <info@shopware.com>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace integration\Migration\Controller;
9+
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\DataProvider;
12+
use PHPUnit\Framework\TestCase;
13+
use Shopware\Core\Defaults;
14+
use Shopware\Core\Framework\Context;
15+
use Shopware\Core\Framework\Log\Package;
16+
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
17+
use SwagMigrationAssistant\Controller\ErrorResolutionController;
18+
use SwagMigrationAssistant\Exception\MigrationException;
19+
use Symfony\Component\HttpFoundation\JsonResponse;
20+
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\HttpFoundation\Response;
22+
23+
/**
24+
* @internal
25+
*/
26+
#[Package('fundamentals@after-sales')]
27+
#[CoversClass(ErrorResolutionController::class)]
28+
class ErrorResolutionControllerTest extends TestCase
29+
{
30+
use IntegrationTestBehaviour;
31+
32+
private ErrorResolutionController $errorResolutionController;
33+
34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
$this->errorResolutionController = static::getContainer()->get(ErrorResolutionController::class);
39+
}
40+
41+
public function testGetFieldStructureUnsetEntityName(): void
42+
{
43+
static::expectExceptionObject(MigrationException::missingRequestParameter('entityName'));
44+
45+
$request = new Request([], [
46+
'fieldName' => 'name',
47+
]);
48+
49+
$this->errorResolutionController->getExampleFieldStructure($request);
50+
}
51+
52+
public function testGetFieldStructureUnsetFieldName(): void
53+
{
54+
static::expectExceptionObject(MigrationException::missingRequestParameter('fieldName'));
55+
56+
$request = new Request([], [
57+
'entityName' => 'product',
58+
]);
59+
60+
$this->errorResolutionController->getExampleFieldStructure($request);
61+
}
62+
63+
public function testGetFieldStructureUnknownField(): void
64+
{
65+
static::expectExceptionObject(MigrationException::entityFieldNotFound('product', 'unknownField'));
66+
67+
$request = new Request([], [
68+
'entityName' => 'product',
69+
'fieldName' => 'unknownField',
70+
]);
71+
72+
$this->errorResolutionController->getExampleFieldStructure($request);
73+
}
74+
75+
/**
76+
* @param array<string, string> $expected
77+
*/
78+
#[DataProvider('fieldStructureProvider')]
79+
public function testGetFieldStructureProduct(string $entityName, string $fieldName, array $expected): void
80+
{
81+
$request = new Request([], [
82+
'entityName' => $entityName,
83+
'fieldName' => $fieldName,
84+
]);
85+
86+
$response = $this->errorResolutionController->getExampleFieldStructure($request);
87+
$responseData = $this->jsonResponseToArray($response);
88+
89+
static::assertArrayHasKey('fieldType', $responseData);
90+
static::assertArrayHasKey('example', $responseData);
91+
92+
static::assertSame($expected['fieldType'], $responseData['fieldType']);
93+
static::assertSame($expected['example'], $responseData['example']);
94+
}
95+
96+
public static function fieldStructureProvider(): \Generator
97+
{
98+
yield 'product name field' => [
99+
'entityName' => 'product',
100+
'fieldName' => 'name',
101+
'expected' => [
102+
'fieldType' => 'TranslatedField',
103+
'example' => '"[string]"',
104+
],
105+
];
106+
107+
yield 'product availableStock field' => [
108+
'entityName' => 'product',
109+
'fieldName' => 'availableStock',
110+
'expected' => [
111+
'fieldType' => 'IntField',
112+
'example' => '0',
113+
],
114+
];
115+
116+
yield 'product price field' => [
117+
'entityName' => 'product',
118+
'fieldName' => 'price',
119+
'expected' => [
120+
'fieldType' => 'PriceField',
121+
'example' => \json_encode([
122+
[
123+
'currencyId' => '[uuid]',
124+
'gross' => 0.1,
125+
'net' => 0.1,
126+
'linked' => false,
127+
],
128+
], \JSON_PRETTY_PRINT),
129+
],
130+
];
131+
132+
yield 'product variant listing config' => [
133+
'entityName' => 'product',
134+
'fieldName' => 'variantListingConfig',
135+
'expected' => [
136+
'fieldType' => 'VariantListingConfigField',
137+
'example' => \json_encode([
138+
'displayParent' => false,
139+
'mainVariantId' => '[uuid]',
140+
'configuratorGroupConfig' => [],
141+
], \JSON_PRETTY_PRINT),
142+
],
143+
];
144+
}
145+
146+
public function testValidateResolutionUnsetEntityName(): void
147+
{
148+
static::expectExceptionObject(MigrationException::missingRequestParameter('entityName'));
149+
150+
$request = new Request([], [
151+
'fieldName' => 'name',
152+
]);
153+
154+
$this->errorResolutionController->validateResolution(
155+
$request,
156+
Context::createDefaultContext()
157+
);
158+
}
159+
160+
public function testValidateResolutionUnsetFieldName(): void
161+
{
162+
static::expectExceptionObject(MigrationException::missingRequestParameter('fieldName'));
163+
164+
$request = new Request([], [
165+
'entityName' => 'product',
166+
]);
167+
168+
$this->errorResolutionController->validateResolution(
169+
$request,
170+
Context::createDefaultContext()
171+
);
172+
}
173+
174+
/**
175+
* @param string|list<string>|null $fieldValue
176+
*/
177+
#[DataProvider('invalidResolutionProvider')]
178+
public function testValidateResolutionInvalidRequest(string|array|null $fieldValue): void
179+
{
180+
static::expectExceptionObject(MigrationException::missingRequestParameter('fieldValue'));
181+
182+
$request = new Request([], [
183+
'entityName' => 'product',
184+
'fieldName' => 'name',
185+
'fieldValue' => $fieldValue,
186+
]);
187+
188+
$this->errorResolutionController->validateResolution(
189+
$request,
190+
Context::createDefaultContext()
191+
);
192+
}
193+
194+
public static function invalidResolutionProvider(): \Generator
195+
{
196+
yield 'null' => ['fieldValue' => null];
197+
yield 'empty string' => ['fieldValue' => ''];
198+
yield 'empty array' => ['fieldValue' => []];
199+
}
200+
201+
/**
202+
* @param array<string, mixed> $expected
203+
*/
204+
#[DataProvider('validateResolutionProvider')]
205+
public function testValidateResolution(string $entityName, string $fieldName, mixed $fieldValue, array $expected): void
206+
{
207+
$request = new Request([], [
208+
'entityName' => $entityName,
209+
'fieldName' => $fieldName,
210+
'fieldValue' => $fieldValue,
211+
]);
212+
213+
$response = $this->errorResolutionController->validateResolution(
214+
$request,
215+
Context::createDefaultContext()
216+
);
217+
$data = $this->jsonResponseToArray($response);
218+
219+
static::assertArrayHasKey('valid', $data);
220+
static::assertArrayHasKey('violations', $data);
221+
222+
$violationMessages = array_map(static fn (array $violation) => $violation['message'], $data['violations']);
223+
224+
static::assertSame($expected['valid'], $data['valid']);
225+
static::assertSame($expected['violations'], $violationMessages);
226+
}
227+
228+
public static function validateResolutionProvider(): \Generator
229+
{
230+
yield 'valid product name' => [
231+
'entityName' => 'product',
232+
'fieldName' => 'name',
233+
'fieldValue' => 'Valid Product Name',
234+
'expected' => [
235+
'valid' => true,
236+
'violations' => [],
237+
],
238+
];
239+
240+
yield 'invalid product stock' => [
241+
'entityName' => 'product',
242+
'fieldName' => 'stock',
243+
'fieldValue' => 'jhdwhawbdh',
244+
'expected' => [
245+
'valid' => false,
246+
'violations' => [
247+
'This value should be of type int.',
248+
],
249+
],
250+
];
251+
252+
yield 'valid product active' => [
253+
'entityName' => 'product',
254+
'fieldName' => 'active',
255+
'fieldValue' => true,
256+
'expected' => [
257+
'valid' => true,
258+
'violations' => [],
259+
],
260+
];
261+
262+
yield 'invalid product taxId' => [
263+
'entityName' => 'product',
264+
'fieldName' => 'taxId',
265+
'fieldValue' => 'invalid-uuid',
266+
'expected' => [
267+
'valid' => false,
268+
'violations' => [
269+
'The string "invalid-uuid" is not a valid uuid.',
270+
],
271+
],
272+
];
273+
274+
yield 'invalid product variant config' => [
275+
'entityName' => 'product',
276+
'fieldName' => 'variantListingConfig',
277+
'fieldValue' => [
278+
'displayParent' => 'not-a-boolean',
279+
'mainVariantId' => 'also-not-a-uuid',
280+
'configuratorGroupConfig' => [],
281+
],
282+
'expected' => [
283+
'valid' => false,
284+
'violations' => [
285+
'This value should be of type boolean.',
286+
'The string "also-not-a-uuid" is not a valid uuid.',
287+
],
288+
],
289+
];
290+
291+
yield 'valid product price' => [
292+
'entityName' => 'product',
293+
'fieldName' => 'price',
294+
'fieldValue' => [
295+
[
296+
'currencyId' => Defaults::CURRENCY,
297+
'gross' => 19.99,
298+
'net' => 16.81,
299+
'linked' => false,
300+
],
301+
],
302+
'expected' => [
303+
'valid' => true,
304+
'violations' => [],
305+
],
306+
];
307+
}
308+
309+
/**
310+
* @return array<string, mixed>|list<array<string, mixed>>
311+
*/
312+
private function jsonResponseToArray(?Response $response): array
313+
{
314+
static::assertNotNull($response);
315+
static::assertInstanceOf(JsonResponse::class, $response);
316+
317+
$content = $response->getContent();
318+
static::assertIsNotBool($content);
319+
static::assertJson($content);
320+
321+
$array = \json_decode($content, true);
322+
static::assertIsArray($array);
323+
324+
return $array;
325+
}
326+
}

tests/unit/Migration/ErrorResolution/MigrationFieldExampleGeneratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
2929
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
3030
use Shopware\Core\Framework\DataAbstractionLayer\Field\TaxFreeConfigField;
31+
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
3132
use Shopware\Core\Framework\DataAbstractionLayer\Field\VariantListingConfigField;
3233
use Shopware\Core\Framework\Log\Package;
3334
use SwagMigrationAssistant\Migration\ErrorResolution\MigrationFieldExampleGenerator;
@@ -69,6 +70,11 @@ public static function exampleFieldProvider(): \Generator
6970
'expected' => '"[string]"',
7071
];
7172

73+
yield 'TranslatedField' => [
74+
'field' => new TranslatedField('test'),
75+
'expected' => '"[string]"',
76+
];
77+
7278
yield 'IdField' => [
7379
'field' => new IdField('test', 'test'),
7480
'expected' => '"[uuid]"',

0 commit comments

Comments
 (0)