-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Test\Phinx\Util; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
use Migrations\Util\Util; | ||
use RuntimeException; | ||
use Cake\TestSuite\TestCase; | ||
|
||
class UtilTest extends TestCase | ||
{ | ||
private function getCorrectedPath($path) | ||
{ | ||
return str_replace('/', DIRECTORY_SEPARATOR, $path); | ||
} | ||
|
||
public function testGetExistingMigrationClassNames() | ||
{ | ||
$expectedResults = [ | ||
'TestMigration', | ||
'TestMigration2', | ||
]; | ||
|
||
$existingClassNames = Util::getExistingMigrationClassNames($this->getCorrectedPath(__DIR__ . '/_files/migrations')); | ||
$this->assertCount(count($expectedResults), $existingClassNames); | ||
foreach ($expectedResults as $expectedResult) { | ||
$this->assertContains($expectedResult, $existingClassNames); | ||
} | ||
} | ||
|
||
public function testGetExistingMigrationClassNamesWithFile() | ||
{ | ||
$file = $this->getCorrectedPath(__DIR__ . '/_files/migrations/20120111235330_test_migration.php'); | ||
$existingClassNames = Util::getExistingMigrationClassNames($file); | ||
$this->assertCount(0, $existingClassNames); | ||
} | ||
|
||
public function testGetCurrentTimestamp() | ||
{ | ||
$dt = new DateTime('now', new DateTimeZone('UTC')); | ||
$expected = $dt->format(Util::DATE_FORMAT); | ||
|
||
$current = Util::getCurrentTimestamp(); | ||
|
||
// Rather than using a strict equals, we use greater/lessthan checks to | ||
// prevent false positives when the test hits the edge of a second. | ||
$this->assertGreaterThanOrEqual($expected, $current); | ||
// We limit the assertion time to 2 seconds, which should never fail. | ||
$this->assertLessThanOrEqual($expected + 2, $current); | ||
} | ||
|
||
public function testGetVersionFromFileName(): void | ||
{ | ||
$this->assertSame(20221130101652, Util::getVersionFromFileName('20221130101652_test.php')); | ||
} | ||
|
||
public function testGetVersionFromFileNameErrorNoVersion(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
Util::getVersionFromFileName('foo.php'); | ||
} | ||
|
||
public function testGetVersionFromFileNameErrorZeroVersion(): VoidCommand | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
Util::getVersionFromFileName('0_foo.php'); | ||
} | ||
|
||
public function providerMapClassNameToFileName(): array | ||
{ | ||
return [ | ||
['CamelCase87afterSomeBooze', '/^\d{14}_camel_case_87after_some_booze\.php$/'], | ||
['CreateUserTable', '/^\d{14}_create_user_table\.php$/'], | ||
['LimitResourceNamesTo30Chars', '/^\d{14}_limit_resource_names_to_30_chars\.php$/'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerMapClassNameToFileName | ||
*/ | ||
public function testMapClassNameToFileName(string $name, string $pattern): void | ||
{ | ||
$this->assertMatchesRegularExpression($pattern, Util::mapClassNameToFileName($name)); | ||
} | ||
|
||
public function providerMapFileName(): array | ||
{ | ||
return [ | ||
['20150902094024_create_user_table.php', 'CreateUserTable'], | ||
['20150902102548_my_first_migration2.php', 'MyFirstMigration2'], | ||
['20200412012035_camel_case_87after_some_booze.php', 'CamelCase87afterSomeBooze'], | ||
['20200412012036_limit_resource_names_to_30_chars.php', 'LimitResourceNamesTo30Chars'], | ||
['20200412012037_back_compat_names_to30_chars.php', 'BackCompatNamesTo30Chars'], | ||
['20200412012037.php', 'V20200412012037'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerMapFileName | ||
*/ | ||
public function testMapFileNameToClassName(string $fileName, string $className) | ||
{ | ||
$this->assertEquals($className, Util::mapFileNameToClassName($fileName)); | ||
} | ||
|
||
public function providerValidClassName(): array | ||
{ | ||
return [ | ||
['camelCase', false], | ||
['CreateUserTable', true], | ||
['UserSeeder', true], | ||
['Test', true], | ||
['test', false], | ||
['Q', true], | ||
['XMLTriggers', true], | ||
['Form_Cards', false], | ||
['snake_high_scores', false], | ||
['Code2319Incidents', true], | ||
['V20200509232007', true], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerValidClassName | ||
*/ | ||
public function testIsValidPhinxClassName(string $className, bool $valid): void | ||
{ | ||
$this->assertSame($valid, Util::isValidPhinxClassName($className)); | ||
} | ||
|
||
public function testGlobPath() | ||
{ | ||
$files = Util::glob(__DIR__ . '/_files/migrations/empty.txt'); | ||
$this->assertCount(1, $files); | ||
$this->assertEquals('empty.txt', basename($files[0])); | ||
|
||
$files = Util::glob(__DIR__ . '/_files/migrations/*.php'); | ||
$this->assertCount(3, $files); | ||
$this->assertEquals('20120111235330_test_migration.php', basename($files[0])); | ||
$this->assertEquals('20120116183504_test_migration_2.php', basename($files[1])); | ||
$this->assertEquals('not_a_migration.php', basename($files[2])); | ||
} | ||
|
||
public function testGlobAll() | ||
{ | ||
$files = Util::globAll([ | ||
__DIR__ . '/_files/migrations/*.php', | ||
__DIR__ . '/_files/migrations/subdirectory/*.txt', | ||
]); | ||
|
||
$this->assertCount(4, $files); | ||
$this->assertEquals('20120111235330_test_migration.php', basename($files[0])); | ||
$this->assertEquals('20120116183504_test_migration_2.php', basename($files[1])); | ||
$this->assertEquals('not_a_migration.php', basename($files[2])); | ||
$this->assertEquals('empty.txt', basename($files[3])); | ||
} | ||
|
||
public function testGetFiles() | ||
{ | ||
$files = Util::getFiles([ | ||
__DIR__ . '/_files/migrations', | ||
__DIR__ . '/_files/migrations/subdirectory', | ||
__DIR__ . '/_files/migrations/subdirectory', | ||
]); | ||
|
||
$this->assertCount(4, $files); | ||
$this->assertEquals('20120111235330_test_migration.php', basename($files[0])); | ||
$this->assertEquals('20120116183504_test_migration_2.php', basename($files[1])); | ||
$this->assertEquals('not_a_migration.php', basename($files[2])); | ||
$this->assertEquals('foobar.php', basename($files[3])); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/TestCase/Util/_files/migrations/20120111235330_test_migration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class TestMigration extends AbstractMigration | ||
Check failure on line 5 in tests/TestCase/Util/_files/migrations/20120111235330_test_migration.php GitHub Actions / cs-stan / Coding Standard & Static Analysis
|
||
{ | ||
/** | ||
* Migrate Up. | ||
*/ | ||
public function up() | ||
{ | ||
// do nothing | ||
} | ||
|
||
/** | ||
* Migrate Down. | ||
*/ | ||
public function down() | ||
{ | ||
// do nothing | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/TestCase/Util/_files/migrations/20120116183504_test_migration_2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class TestMigration2 extends AbstractMigration | ||
Check failure on line 5 in tests/TestCase/Util/_files/migrations/20120116183504_test_migration_2.php GitHub Actions / cs-stan / Coding Standard & Static Analysis
|
||
{ | ||
/** | ||
* Migrate Up. | ||
*/ | ||
public function up() | ||
{ | ||
// do nothing | ||
} | ||
|
||
/** | ||
* Migrate Down. | ||
*/ | ||
public function down() | ||
{ | ||
// do nothing | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'I shouldnt be run'; |
Empty file.
3 changes: 3 additions & 0 deletions
3
tests/TestCase/Util/_files/migrations/subdirectory/foobar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
echo 'I shouldnt be run'; |