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

131 lines
3.7 KiB
PHP
Raw Normal View History

2023-11-06 01:04:25 +00:00
<?php
namespace App\Tests;
use App\DataTransferObject\Config;
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
{
private ValidatorInterface $validator;
public function setUp(): void
{
$this->validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
2023-11-06 01:04:25 +00:00
}
/**
* @dataProvider projectNameProvider
*/
public function testTheProjectNameShouldBeAString(
mixed $projectName,
int $expectedViolationCount,
): 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,
);
}
/**
* @dataProvider projectLanguageProvider
*/
public function testTheProjectLanguageShouldBeASupportedLanguage(
string $language,
int $expectedViolationCount,
): 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-06 01:04:25 +00:00
}
/**
* @dataProvider validWebServerTypesProvider
*/
public function testTheWebServerTypeIsValid(
string $webServer,
int $expectedViolationCount,
): 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-06 01:04:25 +00:00
public function projectLanguageProvider(): \Generator
{
return [
yield 'Supported language string' => ['php', 0],
yield 'Non-supported language string' => ['not-supported', 1],
yield 'Empty string' => ['', 1],
];
}
public function projectNameProvider(): \Generator
{
return [
yield 'Non-empty string' => ['test', 0],
yield 'Empty string' => ['', 1],
];
}
public function validWebServerTypesProvider(): \Generator
{
return [
yield 'caddy' => [WebServer::Caddy->value, 0],
yield 'invalid' => ['not-a-valid-web-server', 1],
yield 'nginx' => [WebServer::Nginx->value, 0],
];
}
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
}