From 9ae880664a0767886fec0bf6847cc8a3821f19d0 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 23 Nov 2023 01:19:33 +0000 Subject: [PATCH] test: add violation message assertions --- CHANGELOG.md | 6 ++ .../Validator/ConfigurationValidatorTest.php | 58 ++++++++++++++----- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0791789..a9e7580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2023-11-23 + +### Added + +* Test assertions for the expected violation messages. + ## 2023-11-22 ### Added diff --git a/tests/Kernel/Validator/ConfigurationValidatorTest.php b/tests/Kernel/Validator/ConfigurationValidatorTest.php index c4be1be..dd55c2a 100644 --- a/tests/Kernel/Validator/ConfigurationValidatorTest.php +++ b/tests/Kernel/Validator/ConfigurationValidatorTest.php @@ -66,6 +66,7 @@ class ConfigurationValidatorTest extends KernelTestCase public function testTheProjectNameShouldBeAString( mixed $projectName, int $expectedViolationCount, + ?string $expectedMessage, ): void { if ($projectName === null) { self::expectException(NotNormalizableValueException::class); @@ -80,6 +81,13 @@ class ConfigurationValidatorTest extends KernelTestCase expectedCount: $expectedViolationCount, haystack: $violations, ); + + if ($violations->count() > 0) { + self::assertSame( + actual: $expectedMessage, + expected: $violations[0]->getMessage(), + ); + } } /** @@ -88,6 +96,7 @@ class ConfigurationValidatorTest extends KernelTestCase public function testTheProjectLanguageShouldBeASupportedLanguage( string $language, int $expectedViolationCount, + ?string $expectedMessage, ): void { $configurationDataDTO = self::createConfigurationDTO(); $configurationDataDTO->language = $language; @@ -104,6 +113,11 @@ class ConfigurationValidatorTest extends KernelTestCase actual: $language, expected: $violations[0]->getInvalidValue(), ); + + self::assertSame( + actual: $expectedMessage, + expected: $violations[0]->getMessage(), + ); } } @@ -113,6 +127,7 @@ class ConfigurationValidatorTest extends KernelTestCase public function testTheProjectTypeShouldBeASupportedType( string $projectType, int $expectedViolationCount, + ?string $expectedMessage, ): void { $configurationDataDTO = self::createConfigurationDTO(); $configurationDataDTO->type = $projectType; @@ -129,6 +144,11 @@ class ConfigurationValidatorTest extends KernelTestCase actual: $projectType, expected: $violations[0]->getInvalidValue(), ); + + self::assertSame( + actual: $expectedMessage, + expected: $violations[0]->getMessage(), + ); } } @@ -138,6 +158,7 @@ class ConfigurationValidatorTest extends KernelTestCase public function testTheWebServerTypeIsValid( string $webServer, int $expectedViolationCount, + ?string $expectedMessage, ): void { $configurationDataDTO = self::createConfigurationDTO(); $configurationDataDTO->web['type'] = $webServer; @@ -154,6 +175,11 @@ class ConfigurationValidatorTest extends KernelTestCase actual: $webServer, expected: $violations[0]->getInvalidValue(), ); + + self::assertSame( + actual: $expectedMessage, + expected: $violations[0]->getMessage(), + ); } } @@ -170,40 +196,40 @@ class ConfigurationValidatorTest extends KernelTestCase public function projectLanguageProvider(): \Generator { return [ - yield 'Supported language string' => ['php', 0], - yield 'Non-supported language string' => ['not-supported', 1], - yield 'Empty string' => ['', 1], + 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.'], ]; } public function projectNameProvider(): \Generator { return [ - yield 'Non-empty string' => ['test', 0], - yield 'Empty string' => ['', 1], + yield 'Non-empty string' => ['test', 0, null], + yield 'Empty string' => ['', 1, 'This value should not be blank.'], ]; } 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], + 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], ]; } 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], + 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], ]; }