test: ensure the project type is valid

This commit is contained in:
Oliver Davies 2023-11-22 21:43:07 +00:00
parent 08852c898a
commit e9d5c4ea6f
5 changed files with 53 additions and 12 deletions

View file

@ -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.

8
run
View file

@ -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

View file

@ -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')]

View file

@ -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';
}

View file

@ -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 [