From e9d5c4ea6fe93ec1f9fb452b24961abfd2837df0 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 22 Nov 2023 21:43:07 +0000 Subject: [PATCH] test: ensure the project type is valid --- CHANGELOG.md | 2 + run | 8 +--- src/DataTransferObject/Config.php | 2 +- src/Enum/ProjectType.php | 13 +++--- .../Validator/ConfigurationValidatorTest.php | 40 +++++++++++++++++++ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de4fbf5..e5153a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,11 @@ * Add CHANGELOG.md. * Add missing validation rules to the `Configuration` DTO object. +* Add a test to ensure the project type is a valid type. * Add a test to ensure the web server is a valid type. ### Changed +* `App\Enum\ProjectType` now returns a string. * `App\Enum\Webserver` now returns a string. * `ConfigurationValidatorTest` no longer performs serialisation. diff --git a/run b/run index 6e37c92..6140f81 100755 --- a/run +++ b/run @@ -1,11 +1,7 @@ #!/usr/bin/env bash -set -eu - -TTY="${TTY:-}" -if [[ ! -t 1 ]]; then - TTY="-T" -fi +set -o errexit +set -o pipefail export PATH=$PATH:./bin:./vendor/bin:./vendor-bin/box/vendor/bin diff --git a/src/DataTransferObject/Config.php b/src/DataTransferObject/Config.php index 8f07715..2d2d498 100644 --- a/src/DataTransferObject/Config.php +++ b/src/DataTransferObject/Config.php @@ -221,7 +221,7 @@ final class Config #[Assert\NotBlank] #[Assert\Type('string')] - #[Assert\Choice(choices: ['drupal', 'fractal', 'laravel', 'php-library', 'symfony'])] + #[Assert\Choice(choices: ['astro', 'drupal', 'fractal', 'laravel', 'php-library', 'symfony', 'terraform'])] public string $type; #[Assert\Type('array')] diff --git a/src/Enum/ProjectType.php b/src/Enum/ProjectType.php index 9754a34..12f0868 100644 --- a/src/Enum/ProjectType.php +++ b/src/Enum/ProjectType.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace App\Enum; -enum ProjectType +enum ProjectType: string { - case Astro; - case Drupal; - case Fractal; - case Terraform; + case Astro = 'astro'; + case Drupal = 'drupal'; + case Fractal = 'fractal'; + case Laravel = 'laravel'; + case PHPLibrary = 'php-library'; + case Symfony = 'symfony'; + case Terraform = 'terraform'; } diff --git a/tests/Kernel/Validator/ConfigurationValidatorTest.php b/tests/Kernel/Validator/ConfigurationValidatorTest.php index 189e633..8c8ab01 100644 --- a/tests/Kernel/Validator/ConfigurationValidatorTest.php +++ b/tests/Kernel/Validator/ConfigurationValidatorTest.php @@ -3,6 +3,7 @@ namespace App\Tests; use App\DataTransferObject\Config; +use App\Enum\ProjectType; use App\Enum\WebServer; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; @@ -67,6 +68,31 @@ class ConfigurationValidatorTest extends KernelTestCase } } + /** + * @dataProvider projectTypeProvider + */ + public function testTheProjectTypeShouldBeASupportedType( + string $projectType, + int $expectedViolationCount, + ): void { + $configurationDataDTO = self::createConfigurationDTO(); + $configurationDataDTO->type = $projectType; + + $violations = $this->validator->validate($configurationDataDTO); + + self::assertCount( + expectedCount: $expectedViolationCount, + haystack: $violations, + ); + + if ($expectedViolationCount > 0) { + self::assertSame( + actual: $projectType, + expected: $violations[0]->getInvalidValue(), + ); + } + } + /** * @dataProvider validWebServerTypesProvider */ @@ -109,6 +135,20 @@ class ConfigurationValidatorTest extends KernelTestCase ]; } + public function projectTypeProvider(): \Generator + { + return [ + yield 'astro' => [ProjectType::Astro->value, 0], + yield 'drupal' => [ProjectType::Drupal->value, 0], + yield 'fractal' => [ProjectType::Fractal->value, 0], + yield 'invalid' => ['not-a-project-type', 1], + yield 'laravel' => [ProjectType::Laravel->value, 0], + yield 'php-library' => [ProjectType::PHPLibrary->value, 0], + yield 'symfony' => [ProjectType::Symfony->value, 0], + yield 'terraform' => [ProjectType::Terraform->value, 0], + ]; + } + public function validWebServerTypesProvider(): \Generator { return [