2023-11-06 01:04:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Tests;
|
|
|
|
|
|
|
|
use App\DataTransferObject\Config;
|
2023-11-22 20:11:16 +00:00
|
|
|
use App\Enum\WebServer;
|
2023-11-06 01:04:25 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
2023-11-18 09:07:47 +00:00
|
|
|
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
|
|
|
|
{
|
2023-11-22 21:30:14 +00:00
|
|
|
$this->validator = Validation::createValidatorBuilder()
|
|
|
|
->enableAnnotationMapping()
|
|
|
|
->getValidator();
|
2023-11-06 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider projectNameProvider
|
|
|
|
*/
|
2023-11-22 21:30:14 +00:00
|
|
|
public function testTheProjectNameShouldBeAString(
|
|
|
|
mixed $projectName,
|
|
|
|
int $expectedViolationCount,
|
|
|
|
): void {
|
2023-11-18 09:37:05 +00:00
|
|
|
if ($projectName === null) {
|
2023-11-18 09:07:47 +00:00
|
|
|
self::expectException(NotNormalizableValueException::class);
|
|
|
|
}
|
|
|
|
|
2023-11-22 21:30:14 +00:00
|
|
|
$configurationDataDTO = self::createConfigurationDTO();
|
|
|
|
$configurationDataDTO->name = $projectName;
|
2023-11-06 01:04:25 +00:00
|
|
|
|
2023-11-22 21:30:14 +00:00
|
|
|
$violations = $this->validator->validate($configurationDataDTO);
|
2023-11-06 01:04:25 +00:00
|
|
|
|
|
|
|
self::assertCount(
|
|
|
|
expectedCount: $expectedViolationCount,
|
|
|
|
haystack: $violations,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider projectLanguageProvider
|
|
|
|
*/
|
2023-11-22 21:30:14 +00:00
|
|
|
public function testTheProjectLanguageShouldBeASupportedLanguage(
|
|
|
|
string $language,
|
|
|
|
int $expectedViolationCount,
|
|
|
|
): void {
|
|
|
|
$configurationDataDTO = self::createConfigurationDTO();
|
|
|
|
$configurationDataDTO->language = $language;
|
2023-11-06 01:04:25 +00:00
|
|
|
|
2023-11-22 21:30:14 +00:00
|
|
|
$violations = $this->validator->validate($configurationDataDTO);
|
2023-11-06 01:04:25 +00:00
|
|
|
|
|
|
|
self::assertCount(
|
|
|
|
expectedCount: $expectedViolationCount,
|
|
|
|
haystack: $violations,
|
|
|
|
);
|
2023-11-22 21:30:14 +00:00
|
|
|
|
|
|
|
if ($expectedViolationCount > 0) {
|
|
|
|
self::assertSame(
|
|
|
|
actual: $language,
|
|
|
|
expected: $violations[0]->getInvalidValue(),
|
|
|
|
);
|
|
|
|
}
|
2023-11-06 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 20:11:16 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider validWebServerTypesProvider
|
|
|
|
*/
|
|
|
|
public function testTheWebServerTypeIsValid(
|
|
|
|
string $webServer,
|
|
|
|
int $expectedViolationCount,
|
2023-11-22 21:30:14 +00:00
|
|
|
): void {
|
|
|
|
$configurationDataDTO = self::createConfigurationDTO();
|
|
|
|
$configurationDataDTO->web['type'] = $webServer;
|
2023-11-22 20:11:16 +00:00
|
|
|
|
2023-11-22 21:30:14 +00:00
|
|
|
$violations = $this->validator->validate($configurationDataDTO);
|
2023-11-22 20:11:16 +00:00
|
|
|
|
|
|
|
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],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:11:16 +00:00
|
|
|
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],
|
|
|
|
];
|
|
|
|
}
|
2023-11-22 21:30:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|