Preserve the scalar type of an extra property default value in the CQRS layer by ousamabenyounes · Pull Request #42343 · PrestaShop/PrestaShop · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AddExtraPropertyDefinitionCommand
* @param bool $required Whether the field is marked required in the BO form and in the Admin API (OpenAPI) schema
* @param bool $nullable Whether the storage column allows NULL
* @param int|null $size Varchar size for string type (null → 255)
* @param string|null $defaultValue SQL DEFAULT clause value
* @param scalar|null $defaultValue SQL DEFAULT clause value (kept as its scalar type to match ExtraPropertyDefinition)
* @param list<string>|null $enumValues Allowed values for CHOICE type
* @param string|null $labelWording i18n wording for the BO label (required when associated_forms or associated_grids)
* @param string|null $labelDomain Translation domain for the label
Expand All @@ -56,7 +56,7 @@ public function __construct(
protected readonly bool $required = false,
protected readonly bool $nullable = true,
protected readonly ?int $size = null,
protected readonly ?string $defaultValue = null,
protected readonly int|float|string|bool|null $defaultValue = null,
protected readonly ?array $enumValues = null,
protected readonly ?string $labelWording = null,
protected readonly ?string $labelDomain = null,
Expand Down Expand Up @@ -116,7 +116,7 @@ public function getSize(): ?int
return $this->size;
}

public function getDefaultValue(): ?string
public function getDefaultValue(): int|float|string|bool|null
{
return $this->defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EditableExtraPropertyDefinition
* @param ExtraPropertySqlIndex $sqlIndex
* @param bool $nullable
* @param int|null $size Varchar size for string fields
* @param string|null $defaultValue
* @param scalar|null $defaultValue Kept as its scalar type to match ExtraPropertyDefinition
* @param list<string>|null $enumValues Allowed values for CHOICE type
* @param bool $displayFront
* @param bool $required
Expand All @@ -59,7 +59,7 @@ public function __construct(
protected readonly ExtraPropertySqlIndex $sqlIndex,
protected readonly bool $nullable,
protected readonly ?int $size,
protected readonly ?string $defaultValue,
protected readonly int|float|string|bool|null $defaultValue,
protected readonly ?array $enumValues,
protected readonly bool $displayFront,
protected readonly bool $required,
Expand Down Expand Up @@ -124,7 +124,7 @@ public function getSize(): ?int
return $this->size;
}

public function getDefaultValue(): ?string
public function getDefaultValue(): int|float|string|bool|null
{
return $this->defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getData($id): array
'sql_index' => $definition->getSqlIndex()->value,
'nullable' => $definition->isNullable(),
'size' => $definition->getSize(),
'default_value' => $definition->getDefaultValue(),
'default_value' => null !== $definition->getDefaultValue() ? (string) $definition->getDefaultValue() : null,
'enum_values' => null !== $definition->getEnumValues() ? implode("\n", $definition->getEnumValues()) : null,
],
'visibility' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests\Unit\Core\Domain\ExtraProperty\Command;

use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Core\Domain\ExtraProperty\Command\AddExtraPropertyDefinitionCommand;

/**
* The command carries the DEFAULT clause value down to ExtraPropertyDefinition, whose
* $defaultValue is typed int|float|string|bool|null. The command must accept and preserve
* the same scalar types instead of coercing everything to string, so a non-string default
* (e.g. an int coming from the Admin API JSON payload) keeps its type through the CQRS layer.
*/
class AddExtraPropertyDefinitionCommandTest extends TestCase
{
/**
* @dataProvider defaultValueProvider
*/
public function testItPreservesTheScalarTypeOfTheDefaultValue(int|float|string|bool|null $defaultValue): void
{
$command = new AddExtraPropertyDefinitionCommand(
entityName: 'product',
propertyName: 'internal_code',
defaultValue: $defaultValue,
);

$this->assertSame($defaultValue, $command->getDefaultValue());
}

/**
* @return iterable<string, array{int|float|string|bool|null}>
*/
public static function defaultValueProvider(): iterable
{
yield 'int' => [42];
yield 'float' => [3.5];
yield 'bool true' => [true];
yield 'bool false' => [false];
yield 'string' => ['some-default'];
yield 'null' => [null];
}
}