Skip to content

Commit

Permalink
Merge branch 'next-37411/exception-currencyId' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-37411 - Exception for null-currencyId

See merge request shopware/6/product/platform!14547
  • Loading branch information
shyim committed Aug 23, 2024
2 parents 9757788 + d379ce5 commit 3dd816b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
4 changes: 0 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3835,10 +3835,6 @@ parameters:
count: 8
path: src/Core/Framework/DataAbstractionLayer/FieldSerializer/PriceDefinitionFieldSerializer.php

-
message: "#^Throwing new exceptions within classes are not allowed\\. Please use domain exception pattern\\. See https\\://github\\.com/shopware/platform/blob/v6\\.4\\.20\\.0/adr/2022\\-02\\-24\\-domain\\-exceptions\\.md$#"
count: 1
path: src/Core/Framework/DataAbstractionLayer/FieldSerializer/PriceFieldSerializer.php

-
message: "#^Throwing new exceptions within classes are not allowed\\. Please use domain exception pattern\\. See https\\://github\\.com/shopware/platform/blob/v6\\.4\\.20\\.0/adr/2022\\-02\\-24\\-domain\\-exceptions\\.md$#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\HttpException;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
use Symfony\Component\HttpFoundation\Response;

#[Package('core')]
Expand Down Expand Up @@ -57,6 +58,7 @@ class DataAbstractionLayerException extends HttpException
public const ATTRIBUTE_NOT_FOUND = 'FRAMEWORK__ATTRIBUTE_NOT_FOUND';
public const EXPECTED_ARRAY_WITH_TYPE = 'FRAMEWORK__EXPECTED_ARRAY_WITH_TYPE';
public const INVALID_AGGREGATION_NAME = 'FRAMEWORK__INVALID_AGGREGATION_NAME';
public const MISSING_FIELD_VALUE = 'FRAMEWORK__MISSING_FIELD_VALUE';
public const NOT_CUSTOM_FIELDS_SUPPORT = 'FRAMEWORK__NOT_CUSTOM_FIELDS_SUPPORT';
public const INTERNAL_FIELD_ACCESS_NOT_ALLOWED = 'FRAMEWORK__INTERNAL_FIELD_ACCESS_NOT_ALLOWED';
public const PROPERTY_NOT_FOUND = 'FRAMEWORK__PROPERTY_NOT_FOUND';
Expand Down Expand Up @@ -497,6 +499,16 @@ public static function invalidArraySerialization(Field $field, mixed $value): se
);
}

public static function missingFieldValue(Field $field): self
{
return new self(
Response::HTTP_BAD_REQUEST,
self::MISSING_FIELD_VALUE,
'A value for the field "{{ field }}" is required, but it is missing or `null`.',
['field' => $field->getPropertyName()]
);
}

public static function notCustomFieldsSupport(string $methodName): self
{
return new self(
Expand Down Expand Up @@ -701,4 +713,9 @@ public static function invalidTimeZone(string $timeZone): self
['timeZone' => $timeZone]
);
}

public static function invalidWriteConstraintViolation(\Symfony\Component\Validator\ConstraintViolationList $violationList, string $getPath): WriteConstraintViolationException
{
return new WriteConstraintViolationException($violationList, $getPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Util\Json;
use Shopware\Core\Framework\Validation\Constraint\Uuid;
use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Optional;
Expand Down Expand Up @@ -117,6 +116,10 @@ public function decode(Field $field, mixed $value): ?PriceCollection
$collection = new PriceCollection();

foreach ($value as $row) {
if (empty($row['currencyId'])) {
throw DataAbstractionLayerException::missingFieldValue($field);
}

if ((!isset($row['listPrice']) || !isset($row['listPrice']['gross'])) && (!isset($row['regulationPrice']) || !isset($row['regulationPrice']['gross']))) {
$collection->add(
new Price($row['currencyId'], (float) $row['net'], (float) $row['gross'], (bool) $row['linked'])
Expand Down Expand Up @@ -228,7 +231,6 @@ private function ensureDefaultPrice(WriteParameterBag $parameters, array $prices
$prices
)
);

throw new WriteConstraintViolationException($violationList, $parameters->getPath());
throw DataAbstractionLayerException::invalidWriteConstraintViolation($violationList, $parameters->getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,17 @@ public function testExpectedArrayWithType(): void
static::assertSame($path, $exception->getParameters()['path']);
static::assertSame($type, $exception->getParameters()['type']);
}

public function testMissingFieldValue(): void
{
$field = new IdField('test_field', 'test_field');
$exception = DataAbstractionLayerException::missingFieldValue($field);

static::assertSame(Response::HTTP_BAD_REQUEST, $exception->getStatusCode());
static::assertSame(DataAbstractionLayerException::MISSING_FIELD_VALUE, $exception->getErrorCode());
static::assertSame(
'A value for the field "test_field" is required, but it is missing or `null`.',
$exception->getMessage()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Integration\Core\Framework\DataAbstractionLayer\FieldSerializer;
namespace Shopware\Tests\Unit\Core\Framework\DataAbstractionLayer\FieldSerializer;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityWriteGateway;
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\PriceFieldSerializer;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
Expand All @@ -15,22 +17,43 @@
use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityExistence;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteContext;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteParameterBag;
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
use Shopware\Core\Test\Stub\DataAbstractionLayer\StaticDefinitionInstanceRegistry;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\Factory\BlackHoleMetadataFactory;
use Symfony\Component\Validator\Validator\RecursiveValidator;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @internal
*/
#[CoversClass(PriceFieldSerializer::class)]
class PriceFieldSerializerTest extends TestCase
{
use KernelTestBehaviour;

protected PriceFieldSerializer $serializer;

protected function setUp(): void
{
$this->serializer = $this->getContainer()->get(PriceFieldSerializer::class);
$validator = new RecursiveValidator(
new ExecutionContextFactory(
$this->createMock(TranslatorInterface::class)
),
new BlackHoleMetadataFactory(),
new ConstraintValidatorFactory()
);

$this->serializer = new PriceFieldSerializer(
$validator,
new StaticDefinitionInstanceRegistry(
[
new ProductDefinition(),
],
$validator,
$this->createMock(EntityWriteGateway::class)
)
);
}

public function testSerializeStrings(): void
Expand Down Expand Up @@ -339,7 +362,7 @@ private function encode(array $data): string
$existence = new EntityExistence('test', ['someId' => true], true, false, false, []);
$keyPair = new KeyValuePair('someId', $data, false);
$bag = new WriteParameterBag(
$this->getContainer()->get(ProductDefinition::class),
new ProductDefinition(),
WriteContext::createFromContext(Context::createDefaultContext()),
'',
new WriteCommandQueue()
Expand Down

0 comments on commit 3dd816b

Please sign in to comment.