From c57fcea34b308ae690bcd0c8ef20dd8de4a99da1 Mon Sep 17 00:00:00 2001 From: Stephan Schuler Date: Wed, 11 Nov 2020 14:37:36 +0100 Subject: [PATCH 1/2] BUGFIX: Fix unit tests Version numbered migration class names are required to contain a 14 digits version number. Test cases came with ony 6 digits. --- Classes/Domain/Service/VersionResolver.php | 2 +- .../Domain/Service/MigrationServiceTest.php | 18 +++++++++--------- .../Domain/Service/VersionResolverTest.php | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Classes/Domain/Service/VersionResolver.php b/Classes/Domain/Service/VersionResolver.php index 74c2939..13923bd 100644 --- a/Classes/Domain/Service/VersionResolver.php +++ b/Classes/Domain/Service/VersionResolver.php @@ -17,7 +17,7 @@ public function extractVersion(string $migrationClassName): string * + 2 digits second * = 14 digits */ - preg_match('#\\\\Version(?\\d{14})(\\\\|$)#', $migrationClassName, $matches); + preg_match('#(\\\\|^)Version(?\\d{14})(\\\\|$)#', $migrationClassName, $matches); return $matches['dateFormatVersionNumber']; } } diff --git a/Tests/Unit/Domain/Service/MigrationServiceTest.php b/Tests/Unit/Domain/Service/MigrationServiceTest.php index 9a76296..d8df602 100644 --- a/Tests/Unit/Domain/Service/MigrationServiceTest.php +++ b/Tests/Unit/Domain/Service/MigrationServiceTest.php @@ -62,8 +62,8 @@ protected function setUp() $this->fileSystemMigrationsResolver->method('findMigrationFiles') ->willReturn([ - Version1312192::class, - Version1312193::class, + Version20191001142901::class, + Version20191001142902::class, ]); $this->migrationService = new MigrationService( @@ -107,7 +107,7 @@ public function Can_Filter_Executed_Migrations_In_Get_UnexecutedMigrations() ->getMock(); $migrationStatusMock->method('getVersion') - ->willReturn('1312192'); + ->willReturn('20191001142901'); $this->migrationStatusRepository->method('findAll') ->willReturn([$migrationStatusMock]); @@ -115,7 +115,7 @@ public function Can_Filter_Executed_Migrations_In_Get_UnexecutedMigrations() $migrations = $this->migrationService->findUnexecutedMigrations(); $this->assertCount(1, $migrations); - $this->assertArrayHasKey('1312193', $migrations); + $this->assertArrayHasKey('20191001142902', $migrations); } /** @@ -134,12 +134,12 @@ public function Can_Return_A_Single_Migration() ->getMock(); $migrationStatusMock->method('getVersion') - ->willReturn('1312192'); + ->willReturn('20191001142901'); $this->migrationStatusRepository->method('findAll') ->willReturn([$migrationStatusMock]); - $migration = $this->migrationService->getMigrationByVersion('1312192'); + $migration = $this->migrationService->getMigrationByVersion('20191001142901'); $this->assertInstanceOf(Migration::class, $migration); } @@ -162,7 +162,7 @@ public function Will_Throw_Unknown_Migration_If_Migration_Not_Found() ->getMock(); $migrationStatusMock->method('getVersion') - ->willReturn('1312192'); + ->willReturn('20191001142901'); $this->migrationStatusRepository->method('findAll') ->willReturn([$migrationStatusMock]); @@ -171,10 +171,10 @@ public function Will_Throw_Unknown_Migration_If_Migration_Not_Found() } } -class Version1312192 { +class Version20191001142901 { } -class Version1312193 { +class Version20191001142902 { } diff --git a/Tests/Unit/Domain/Service/VersionResolverTest.php b/Tests/Unit/Domain/Service/VersionResolverTest.php index 6b9fca2..746f215 100644 --- a/Tests/Unit/Domain/Service/VersionResolverTest.php +++ b/Tests/Unit/Domain/Service/VersionResolverTest.php @@ -38,6 +38,8 @@ public function getProvidedData() return [ ['Version20190930132259', '20190930132259'], ['Version20190930132253', '20190930132253'], + ['Migrations\\Version20201111143501', '20201111143501'], + ['Migrations\\Version20201111143502\\MyClass', '20201111143502'], ]; } } From f0ea98ecbc720fa7e61032ffa99a859d996eaebe Mon Sep 17 00:00:00 2001 From: Stephan Schuler Date: Wed, 11 Nov 2020 14:53:13 +0100 Subject: [PATCH 2/2] FEATURE: Throw exceptions when version numberd class name is invalid --- Classes/Domain/Service/VersionResolver.php | 8 ++++ Classes/Error/InvalidClassName.php | 8 ++++ .../Domain/Service/VersionResolverTest.php | 37 +++++++++++++------ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 Classes/Error/InvalidClassName.php diff --git a/Classes/Domain/Service/VersionResolver.php b/Classes/Domain/Service/VersionResolver.php index 13923bd..86b12ca 100644 --- a/Classes/Domain/Service/VersionResolver.php +++ b/Classes/Domain/Service/VersionResolver.php @@ -3,6 +3,8 @@ namespace Netlogix\Migrations\Domain\Service; +use Netlogix\Migrations\Error\InvalidClassName; + class VersionResolver { public function extractVersion(string $migrationClassName): string @@ -18,6 +20,12 @@ public function extractVersion(string $migrationClassName): string * = 14 digits */ preg_match('#(\\\\|^)Version(?\\d{14})(\\\\|$)#', $migrationClassName, $matches); + if (!isset($matches['dateFormatVersionNumber'])) { + throw new InvalidClassName( + 'Version numbered class names are required to match "Version" followed by exactly 14 digits.', + 1605102278 + ); + } return $matches['dateFormatVersionNumber']; } } diff --git a/Classes/Error/InvalidClassName.php b/Classes/Error/InvalidClassName.php new file mode 100644 index 0000000..710384d --- /dev/null +++ b/Classes/Error/InvalidClassName.php @@ -0,0 +1,8 @@ +versionResolver = new VersionResolver(); + $this->versionResolver = new VersionResolver(); } /** * @test - * @dataProvider getProvidedData + * @dataProvider validClassNames */ public function Can_extract_version_string(string $migrationClassName, string $expectedVersionString) { $this->assertEquals($expectedVersionString, $this->versionResolver->extractVersion($migrationClassName)); } - public function getProvidedData() + /** + * @test + * @dataProvider invalidClassNames + */ + public function Can_not_extract_version_string(string $migrationClassName) + { + $this->expectException(InvalidClassName::class); + $this->versionResolver->extractVersion($migrationClassName); + } + + public function invalidClassNames() + { + return [ + 'lower case class name' => ['version20201111145100'], + 'to many digits' => ['Version202011111451001'], + 'to little digits' => ['version2020111114510'], + ]; + } + + public function validClassNames() { return [ - ['Version20190930132259', '20190930132259'], + 'class name without namespace' => ['Version20190930132259', '20190930132259'], ['Version20190930132253', '20190930132253'], - ['Migrations\\Version20201111143501', '20201111143501'], - ['Migrations\\Version20201111143502\\MyClass', '20201111143502'], + 'class name with namespace' => ['Migrations\\Version20201111143501', '20201111143501'], + 'version number in namespace' => ['Migrations\\Version20201111143502\\MyClass', '20201111143502'], ]; } }