From 95012241e1749cc2a48671602a97ded3e8929c13 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 22 Nov 2023 20:07:11 +0000 Subject: [PATCH] chore: add missing values to the Config DTO Add missing properties and constraints to the `Config` data transfer object. --- src/DataTransferObject/Config.php | 224 +++++++++++++++--- .../Validator/ConfigurationValidatorTest.php | 2 - 2 files changed, 193 insertions(+), 33 deletions(-) diff --git a/src/DataTransferObject/Config.php b/src/DataTransferObject/Config.php index 9643b80..8f07715 100644 --- a/src/DataTransferObject/Config.php +++ b/src/DataTransferObject/Config.php @@ -8,67 +8,229 @@ use Symfony\Component\Validator\Constraints as Assert; final class Config { - /** - * @var array> - */ + #[Assert\Type('array')] + #[Assert\Valid] #[Assert\Collection( allowExtraFields: false, fields: [ - 'extra_databases' => new Assert\Optional( - new Assert\All(new Assert\Type('string')) - ), - 'type' => new Assert\Choice(['mariadb', 'mysql']), - 'version' => new Assert\Type('integer'), + 'type' => new Assert\Required([ + new Assert\Choice(choices: ['mariadb', 'mysql']), + new Assert\Type('string'), + ]), + 'version' => new Assert\Required([ + new Assert\Type('int'), + ]), ], )] public array $database; - /** - * @var array - */ + #[Assert\Type('array')] + #[Assert\Valid] + #[Assert\Collection( + allowExtraFields: false, + fields: [ + 'services' => new Assert\Required([ + new Assert\Type('array'), + new Assert\Count(['min' => 1]), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ], + )] + public array $dockerCompose; + + #[Assert\Type('array')] + #[Assert\Valid] + #[Assert\Collection( + allowExtraFields: false, + fields: [ + 'stages' => new Assert\Optional([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Collection([ + 'commands' => new Assert\Optional([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + 'extends' => new Assert\Optional([ + new Assert\Type('string'), + ]), + 'extensions' => new Assert\Optional([ + new Assert\Collection( + allowExtraFields: false, + fields: [ + 'install' => new Assert\Required([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ], + ), + ]), + 'packages' => new Assert\Optional([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + 'root_commands' => new Assert\Optional([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ]), + ]), + ]), + ], + )] + public array $dockerfile; + + #[Assert\Type('array')] + #[Assert\Valid] #[Assert\Collection( allowExtraFields: false, fields: ['docroot' => new Assert\Choice([null, 'web', 'docroot'])], )] public array $drupal; - /** - * @var array - */ + #[Assert\Type('array')] + #[Assert\Valid] #[Assert\Collection( allowExtraFields: false, - allowMissingFields: true, fields: [ - 'createGitHubActionsConfiguration' => new Assert\Type('boolean'), - 'runGitHooksBeforePush' => new Assert\Type('boolean'), - 'useNewDatabaseCredentials' => new Assert\Type('boolean'), - ] + 'createGitHubActionsConfiguration' => new Assert\Optional([ + new Assert\Type('bool'), + ]), + 'runGitHooksBeforePush' => new Assert\Optional([ + new Assert\Type('bool'), + ]), + 'useNewDatabaseCredentials' => new Assert\Optional([ + new Assert\Type('bool'), + ]), + ], )] public array $experimental; + #[Assert\Type('array')] + #[Assert\Valid()] + #[Assert\Collection([ + 'ignore' => new Assert\Optional([ + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ])] + public array $git; + + #[Assert\Type('array')] + #[Assert\Valid] + #[Assert\Collection( + allowExtraFields: false, + fields: [ + 'devshell' => new Assert\Required([ + new Assert\Type('array'), + new Assert\Collection([ + 'packages' => new Assert\Required([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ]), + ]), + ], + )] + public array $flake; + + #[Assert\Type('string')] #[Assert\Choice(choices: ['javascript', 'php', 'typescript'])] public string $language; #[Assert\NotBlank] + #[Assert\Type('string')] public string $name; - #[Assert\Choice(choices: ['astro', 'drupal', 'fractal', 'laravel', 'php-library', 'terraform', 'vuejs'])] - #[Assert\NotBlank] - public string $type; - + #[Assert\Type('array')] + #[Assert\Valid] #[Assert\Collection( allowExtraFields: false, fields: [ - 'ignore' => new Assert\Optional([ + 'type' => new Assert\Required([ new Assert\Type('array'), - new Assert\Count(['min' => 1]), - new Assert\All([ - new Assert\Type('string'), - ]) - ]) - ] + new Assert\Collection([ + 'type' => new Assert\Required([ + new Assert\Type('string'), + ]), + ]), + ]), + ], )] - public array $git; + public array $node; + #[Assert\Type('array')] + #[Assert\Valid] + #[Assert\Collection( + allowExtraFields: false, + fields: [ + 'version' => new Assert\Required([ + new Assert\Type('string'), + ]), + 'phpcs' => new Assert\Optional([ + new Assert\Collection([ + 'paths' => new Assert\Required([ + new Assert\Type('array'), + new Assert\Count(['min' => 1]), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + 'standards' => new Assert\Required([ + new Assert\Type('array'), + new Assert\Count(['min' => 1]), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ]), + ]), + 'phpstan' => new Assert\Optional([ + new Assert\Collection([ + 'level' => new Assert\Required([ + new Assert\Type(['string', 'integer']), + ]), + 'paths' => new Assert\Required([ + new Assert\Type('array'), + new Assert\Count(['min' => 1]), + new Assert\All([ + new Assert\Type('string'), + ]), + ]), + ]), + ]), + ], + )] public array $php; + + #[Assert\Type('string')] + public string $projectRoot; + + #[Assert\NotBlank] + #[Assert\Type('string')] + #[Assert\Choice(choices: ['drupal', 'fractal', 'laravel', 'php-library', 'symfony'])] + public string $type; + + #[Assert\Type('array')] + #[Assert\Valid()] + #[Assert\Collection([ + 'type' => new Assert\Required([ + new Assert\Type('string'), + new Assert\Choice(choices: ['nginx', 'caddy']), + ]), + ])] + public array $web; } diff --git a/tests/Kernel/Validator/ConfigurationValidatorTest.php b/tests/Kernel/Validator/ConfigurationValidatorTest.php index 1ae285e..4f511e6 100644 --- a/tests/Kernel/Validator/ConfigurationValidatorTest.php +++ b/tests/Kernel/Validator/ConfigurationValidatorTest.php @@ -96,8 +96,6 @@ class ConfigurationValidatorTest extends KernelTestCase return [ yield 'Non-empty string' => ['test', 0], yield 'Empty string' => ['', 1], - yield 'False' => [false, 1], - yield 'Null' => [null, 1], ]; }