build-configs/tests/Kernel/Validator/ConfigurationValidatorTest.php

246 lines
7.4 KiB
PHP
Raw Normal View History

2023-11-06 01:04:25 +00:00
<?php
namespace App\Tests;
use App\DataTransferObject\Config;
2023-11-22 21:43:07 +00:00
use App\Enum\ProjectType;
use App\Enum\WebServer;
2023-11-06 01:04:25 +00:00
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2023-11-06 01:04:25 +00:00
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ConfigurationValidatorTest extends KernelTestCase
{
2023-11-23 00:59:27 +00:00
private Config $configurationDataDTO;
2023-11-06 01:04:25 +00:00
private ValidatorInterface $validator;
public function setUp(): void
{
2023-11-23 00:59:27 +00:00
$this->configurationDataDTO = self::createConfigurationDTO();
$this->validator = Validation::createValidatorBuilder()
->enableAttributeMapping()
->getValidator();
2023-11-06 01:04:25 +00:00
}
2023-11-23 00:59:27 +00:00
/**
* @dataProvider extraDatabaseProvider
*/
public function testThatExtraDatabasesCanBeSpecified(
?array $extraDatabases,
int $expectedViolationCount,
?string $expectedMessage,
): void
{
$this->configurationDataDTO->database = [
'extra_databases' => $extraDatabases,
'type' => 'mariadb',
'version' => 10,
];
$violations = $this->validator->validate($this->configurationDataDTO);
self::assertCount(
expectedCount: $expectedViolationCount,
haystack: $violations,
);
if ($expectedViolationCount > 0) {
self::assertSame(
actual: 'database[extra_databases][0]',
expected: $violations[0]->getPropertyPath(),
);
self::assertSame(
actual: $expectedMessage,
expected: $violations[0]->getMessage(),
);
}
}
2023-11-06 01:04:25 +00:00
/**
* @dataProvider projectNameProvider
*/
public function testTheProjectNameShouldBeAString(
mixed $projectName,
int $expectedViolationCount,
2023-11-23 01:19:33 +00:00
?string $expectedMessage,
): void {
2023-11-18 09:37:05 +00:00
if ($projectName === null) {
self::expectException(NotNormalizableValueException::class);
}
$configurationDataDTO = self::createConfigurationDTO();
$configurationDataDTO->name = $projectName;
2023-11-06 01:04:25 +00:00
$violations = $this->validator->validate($configurationDataDTO);
2023-11-06 01:04:25 +00:00
self::assertCount(
expectedCount: $expectedViolationCount,
haystack: $violations,
);
2023-11-23 01:19:33 +00:00
if ($violations->count() > 0) {
self::assertSame(
actual: $expectedMessage,
expected: $violations[0]->getMessage(),
);
}
2023-11-06 01:04:25 +00:00
}
/**
* @dataProvider projectLanguageProvider
*/
public function testTheProjectLanguageShouldBeASupportedLanguage(
string $language,
int $expectedViolationCount,
2023-11-23 01:19:33 +00:00
?string $expectedMessage,
): void {
$configurationDataDTO = self::createConfigurationDTO();
$configurationDataDTO->language = $language;
2023-11-06 01:04:25 +00:00
$violations = $this->validator->validate($configurationDataDTO);
2023-11-06 01:04:25 +00:00
self::assertCount(
expectedCount: $expectedViolationCount,
haystack: $violations,
);
if ($expectedViolationCount > 0) {
self::assertSame(
actual: $language,
expected: $violations[0]->getInvalidValue(),
);
2023-11-23 01:19:33 +00:00
self::assertSame(
actual: $expectedMessage,
expected: $violations[0]->getMessage(),
);
}
2023-11-06 01:04:25 +00:00
}
2023-11-22 21:43:07 +00:00
/**
* @dataProvider projectTypeProvider
*/
public function testTheProjectTypeShouldBeASupportedType(
string $projectType,
int $expectedViolationCount,
2023-11-23 01:19:33 +00:00
?string $expectedMessage,
2023-11-22 21:43:07 +00:00
): 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(),
);
2023-11-23 01:19:33 +00:00
self::assertSame(
actual: $expectedMessage,
expected: $violations[0]->getMessage(),
);
2023-11-22 21:43:07 +00:00
}
}
/**
2023-11-25 08:25:00 +00:00
* @dataProvider validWebServerTypes
*/
public function testTheWebServerTypeIsValid(
string $webServer,
int $expectedViolationCount,
2023-11-23 01:19:33 +00:00
?string $expectedMessage,
): void {
$configurationDataDTO = self::createConfigurationDTO();
$configurationDataDTO->web['type'] = $webServer;
$violations = $this->validator->validate($configurationDataDTO);
self::assertCount(
expectedCount: $expectedViolationCount,
haystack: $violations,
);
if ($expectedViolationCount > 0) {
self::assertSame(
actual: $webServer,
expected: $violations[0]->getInvalidValue(),
);
2023-11-23 01:19:33 +00:00
self::assertSame(
actual: $expectedMessage,
expected: $violations[0]->getMessage(),
);
}
}
2023-11-25 08:25:00 +00:00
public static function extraDatabaseProvider(): \Generator
2023-11-23 00:59:27 +00:00
{
return [
yield 'correct' => [['migrate'], 0, null],
yield 'empty string' => [[''], 1, 'This value should not be blank.'],
yield 'missing' => [null, 0, null],
yield 'no extra databases' => [[], 0, null],
];
}
2023-11-25 08:25:00 +00:00
public static function projectLanguageProvider(): \Generator
2023-11-06 01:04:25 +00:00
{
return [
2023-11-23 01:19:33 +00:00
yield 'Supported language string' => ['php', 0, null],
yield 'Non-supported language string' => ['not-supported', 1, 'The value you selected is not a valid choice.'],
yield 'Empty string' => ['', 1, 'The value you selected is not a valid choice.'],
2023-11-06 01:04:25 +00:00
];
}
2023-11-25 08:25:00 +00:00
public static function projectNameProvider(): \Generator
2023-11-06 01:04:25 +00:00
{
return [
2023-11-23 01:19:33 +00:00
yield 'Non-empty string' => ['test', 0, null],
yield 'Empty string' => ['', 1, 'This value should not be blank.'],
2023-11-06 01:04:25 +00:00
];
}
2023-11-25 08:25:00 +00:00
public static function projectTypeProvider(): \Generator
2023-11-22 21:43:07 +00:00
{
return [
2023-11-23 01:19:33 +00:00
yield 'astro' => [ProjectType::Astro->value, 0, null],
yield 'drupal' => [ProjectType::Drupal->value, 0, null],
yield 'fractal' => [ProjectType::Fractal->value, 0, null],
yield 'invalid' => ['not-a-project-type', 1, 'The value you selected is not a valid choice.'],
yield 'laravel' => [ProjectType::Laravel->value, 0, null],
yield 'php-library' => [ProjectType::PHPLibrary->value, 0, null],
yield 'symfony' => [ProjectType::Symfony->value, 0, null],
yield 'terraform' => [ProjectType::Terraform->value, 0, null],
2023-11-22 21:43:07 +00:00
];
}
2023-11-25 08:25:00 +00:00
public static function validWebServerTypes(): \Generator
{
return [
2023-11-23 01:19:33 +00:00
yield 'caddy' => [WebServer::Caddy->value, 0, null],
yield 'invalid' => ['not-a-valid-web-server', 1, 'The value you selected is not a valid choice.'],
yield 'nginx' => [WebServer::Nginx->value, 0, null],
];
}
private static function createConfigurationDTO(): Config
{
$configurationDataDTO = new Config();
$configurationDataDTO->language = 'php';
$configurationDataDTO->name = 'test';
$configurationDataDTO->type = 'drupal';
return $configurationDataDTO;
}
2023-11-06 01:04:25 +00:00
}