Skip to content

Commit

Permalink
Merge pull request #8 from netlogix/bugfix/fix-tests
Browse files Browse the repository at this point in the history
BUGFIX: Fix unit tests
  • Loading branch information
paxuclus authored Nov 11, 2020
2 parents 3d3ca79 + f0ea98e commit 95a40cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
10 changes: 9 additions & 1 deletion Classes/Domain/Service/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Netlogix\Migrations\Domain\Service;

use Netlogix\Migrations\Error\InvalidClassName;

class VersionResolver
{
public function extractVersion(string $migrationClassName): string
Expand All @@ -17,7 +19,13 @@ public function extractVersion(string $migrationClassName): string
* + 2 digits second
* = 14 digits
*/
preg_match('#\\\\Version(?<dateFormatVersionNumber>\\d{14})(\\\\|$)#', $migrationClassName, $matches);
preg_match('#(\\\\|^)Version(?<dateFormatVersionNumber>\\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'];
}
}
8 changes: 8 additions & 0 deletions Classes/Error/InvalidClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Netlogix\Migrations\Error;

class InvalidClassName extends \InvalidArgumentException
{
}
18 changes: 9 additions & 9 deletions Tests/Unit/Domain/Service/MigrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ protected function setUp()

$this->fileSystemMigrationsResolver->method('findMigrationFiles')
->willReturn([
Version1312192::class,
Version1312193::class,
Version20191001142901::class,
Version20191001142902::class,
]);

$this->migrationService = new MigrationService(
Expand Down Expand Up @@ -107,15 +107,15 @@ public function Can_Filter_Executed_Migrations_In_Get_UnexecutedMigrations()
->getMock();

$migrationStatusMock->method('getVersion')
->willReturn('1312192');
->willReturn('20191001142901');

$this->migrationStatusRepository->method('findAll')
->willReturn([$migrationStatusMock]);

$migrations = $this->migrationService->findUnexecutedMigrations();

$this->assertCount(1, $migrations);
$this->assertArrayHasKey('1312193', $migrations);
$this->assertArrayHasKey('20191001142902', $migrations);
}

/**
Expand All @@ -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);
}
Expand All @@ -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]);
Expand All @@ -171,10 +171,10 @@ public function Will_Throw_Unknown_Migration_If_Migration_Not_Found()
}
}

class Version1312192 {
class Version20191001142901 {

}

class Version1312193 {
class Version20191001142902 {

}
35 changes: 26 additions & 9 deletions Tests/Unit/Domain/Service/VersionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace Netlogix\Migrations\Tests\Unit\Domain\Service;

use Doctrine\DBAL\Migrations\Finder\GlobFinder;
use Neos\Flow\Package;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Tests\UnitTestCase;
use Netlogix\Migrations\Domain\Model\Migration;
use Netlogix\Migrations\Domain\Service\VersionResolver;
use Netlogix\Migrations\Error\InvalidClassName;

class VersionResolverTest extends UnitTestCase
{
Expand All @@ -20,24 +17,44 @@ class VersionResolverTest extends UnitTestCase
protected function setUp()
{
parent::setUp();

$this->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'],
'class name with namespace' => ['Migrations\\Version20201111143501', '20201111143501'],
'version number in namespace' => ['Migrations\\Version20201111143502\\MyClass', '20201111143502'],
];
}
}

0 comments on commit 95a40cc

Please sign in to comment.