test: remove serialisation

Refactor the tests so they don't need to serialise data, making the
tests simpler and faster to run.
This commit is contained in:
Oliver Davies 2023-11-22 21:30:14 +00:00
parent b65e92b790
commit 0aaf2cc59e

View file

@ -5,48 +5,36 @@ namespace App\Tests;
use App\DataTransferObject\Config; use App\DataTransferObject\Config;
use App\Enum\WebServer; use App\Enum\WebServer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
class ConfigurationValidatorTest extends KernelTestCase class ConfigurationValidatorTest extends KernelTestCase
{ {
private SerializerInterface $serializer;
private ValidatorInterface $validator; private ValidatorInterface $validator;
public function setUp(): void public function setUp(): void
{ {
$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter()); $this->validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
$this->serializer = new Serializer([$normalizer], [new JsonEncoder()]); ->getValidator();
$this->validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
} }
/** /**
* @dataProvider projectNameProvider * @dataProvider projectNameProvider
*/ */
public function testTheProjectNameShouldBeAString(mixed $projectName, int $expectedViolationCount): void public function testTheProjectNameShouldBeAString(
{ mixed $projectName,
int $expectedViolationCount,
): void {
if ($projectName === null) { if ($projectName === null) {
self::expectException(NotNormalizableValueException::class); self::expectException(NotNormalizableValueException::class);
} }
$configurationData = [ $configurationDataDTO = self::createConfigurationDTO();
'language' => 'php', $configurationDataDTO->name = $projectName;
'name' => $projectName,
'type' => 'drupal',
];
$configurationDataDto = $this->createConfigurationDTO($configurationData); $violations = $this->validator->validate($configurationDataDTO);
$violations = $this->validator->validate($configurationDataDto);
self::assertCount( self::assertCount(
expectedCount: $expectedViolationCount, expectedCount: $expectedViolationCount,
@ -57,26 +45,26 @@ class ConfigurationValidatorTest extends KernelTestCase
/** /**
* @dataProvider projectLanguageProvider * @dataProvider projectLanguageProvider
*/ */
public function testTheProjectLanguageShouldBeASupportedLanguage(mixed $language, int $expectedViolationCount): void public function testTheProjectLanguageShouldBeASupportedLanguage(
{ string $language,
if ($language === null) { int $expectedViolationCount,
self::expectException(NotNormalizableValueException::class); ): void {
} $configurationDataDTO = self::createConfigurationDTO();
$configurationDataDTO->language = $language;
$configurationData = [ $violations = $this->validator->validate($configurationDataDTO);
'language' => $language,
'name' => 'test',
'type' => 'drupal',
];
$configurationDataDto = $this->createConfigurationDTO($configurationData);
$violations = $this->validator->validate($configurationDataDto);
self::assertCount( self::assertCount(
expectedCount: $expectedViolationCount, expectedCount: $expectedViolationCount,
haystack: $violations, haystack: $violations,
); );
if ($expectedViolationCount > 0) {
self::assertSame(
actual: $language,
expected: $violations[0]->getInvalidValue(),
);
}
} }
/** /**
@ -85,18 +73,11 @@ class ConfigurationValidatorTest extends KernelTestCase
public function testTheWebServerTypeIsValid( public function testTheWebServerTypeIsValid(
string $webServer, string $webServer,
int $expectedViolationCount, int $expectedViolationCount,
): void ): void {
{ $configurationDataDTO = self::createConfigurationDTO();
$configurationData = [ $configurationDataDTO->web['type'] = $webServer;
'language' => 'php',
'name' => 'test',
'type' => 'drupal',
'web' => ['type' => $webServer],
];
$configurationDataDto = $this->createConfigurationDTO($configurationData); $violations = $this->validator->validate($configurationDataDTO);
$violations = $this->validator->validate($configurationDataDto);
self::assertCount( self::assertCount(
expectedCount: $expectedViolationCount, expectedCount: $expectedViolationCount,
@ -117,10 +98,6 @@ class ConfigurationValidatorTest extends KernelTestCase
yield 'Supported language string' => ['php', 0], yield 'Supported language string' => ['php', 0],
yield 'Non-supported language string' => ['not-supported', 1], yield 'Non-supported language string' => ['not-supported', 1],
yield 'Empty string' => ['', 1], yield 'Empty string' => ['', 1],
yield 'True' => [true, 1],
yield 'False' => [false, 1],
yield 'Integer' => [1, 1],
yield 'Null' => [null, 1],
]; ];
} }
@ -132,11 +109,6 @@ class ConfigurationValidatorTest extends KernelTestCase
]; ];
} }
private function createConfigurationDTO(array $configurationData): Config
{
return $this->serializer->deserialize(json_encode($configurationData), Config::class, 'json');
}
public function validWebServerTypesProvider(): \Generator public function validWebServerTypesProvider(): \Generator
{ {
return [ return [
@ -145,4 +117,14 @@ class ConfigurationValidatorTest extends KernelTestCase
yield 'nginx' => [WebServer::Nginx->value, 0], 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;
}
} }