Skip to content

Commit

Permalink
better entity multiple transformer validation (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
troytft committed Apr 8, 2022
1 parent df732f6 commit 8024b5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": ">=8.0",
Expand Down
13 changes: 9 additions & 4 deletions src/Services/Mapper/Transformer/DoctrineEntityTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ private function transformSingleItem(string $class, string $fieldName, mixed $va

private function transformMultipleItems(string $class, string $fieldName, mixed $value): array
{
$firstCollectionItem = $value[0] ?? null;
if ($firstCollectionItem === null) {
$columnType = RestApiBundle\Helper\DoctrineHelper::extractColumnType($class, $fieldName);
if ($columnType === PropertyInfo\Type::BUILTIN_TYPE_INT && !is_array($value)) {
throw new RestApiBundle\Exception\Mapper\Transformer\CollectionOfIntegersRequiredException();
} elseif ($columnType === PropertyInfo\Type::BUILTIN_TYPE_STRING && !is_array($value)) {
throw new RestApiBundle\Exception\Mapper\Transformer\CollectionOfStringsRequiredException();
}

if (!count($value)) {
return [];
}

$columnType = RestApiBundle\Helper\DoctrineHelper::extractColumnType($class, $fieldName);

$firstCollectionItem = $value[0] ?? null;
if ($columnType === PropertyInfo\Type::BUILTIN_TYPE_INT && !is_numeric($firstCollectionItem)) {
throw new RestApiBundle\Exception\Mapper\Transformer\CollectionOfIntegersRequiredException();
} elseif ($columnType === PropertyInfo\Type::BUILTIN_TYPE_STRING && (!is_string($firstCollectionItem) && !is_numeric($firstCollectionItem))) {
Expand Down
31 changes: 29 additions & 2 deletions tests/cases/Mapper/EntityTransformerMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,45 @@ public function testSuccessFetch()
$this->assertSame(2, $model->bySlug[1]->getId());
}

public function testInvalid()
public function testInvalidCollection()
{
$model = new Tests\Fixture\Mapper\EntityTransformerMultipleTest\Model();

try {
$this->getMapper()->map($model, [
'byId' => 'undefined'
'byId' => 13,
]);
$this->fail();
} catch (RestApiBundle\Exception\Mapper\MappingException $exception) {
$this->assertSame(['byId' => ['This value should be a collection of integers.']], $exception->getProperties());
}

try {
$this->getMapper()->map($model, [
'byId' => [null,],
]);
$this->fail();
} catch (RestApiBundle\Exception\Mapper\MappingException $exception) {
$this->assertSame(['byId' => ['This value should be a collection of integers.']], $exception->getProperties());
}

try {
$this->getMapper()->map($model, [
'bySlug' => 'keto-cookbook-beginners-low-carb-homemade',
]);
$this->fail();
} catch (RestApiBundle\Exception\Mapper\MappingException $exception) {
$this->assertSame(['bySlug' => ['This value should be a collection of strings.']], $exception->getProperties());
}

try {
$this->getMapper()->map($model, [
'bySlug' => [null,],
]);
$this->fail();
} catch (RestApiBundle\Exception\Mapper\MappingException $exception) {
$this->assertSame(['bySlug' => ['This value should be a collection of strings.']], $exception->getProperties());
}
}

public function testOrder()
Expand Down

0 comments on commit 8024b5f

Please sign in to comment.