diff --git a/src/Phinx/Util/Util.php b/src/Phinx/Util/Util.php index 74ed5e4e6..bea3436a1 100644 --- a/src/Phinx/Util/Util.php +++ b/src/Phinx/Util/Util.php @@ -90,8 +90,7 @@ public static function getVersionFromFileName(string $fileName): int { $matches = []; preg_match('/^[0-9]+/', basename($fileName), $matches); - - $value = (int)$matches[0]; + $value = (int)($matches[0] ?? null); if (!$value) { throw new RuntimeException(sprintf('Cannot get a valid version from filename `%s`', $fileName)); } diff --git a/tests/Phinx/Util/UtilTest.php b/tests/Phinx/Util/UtilTest.php index de68c6862..0bc2b1dcb 100644 --- a/tests/Phinx/Util/UtilTest.php +++ b/tests/Phinx/Util/UtilTest.php @@ -3,6 +3,7 @@ namespace Test\Phinx\Util; use Phinx\Util\Util; +use RuntimeException; use Test\Phinx\TestCase; class UtilTest extends TestCase @@ -47,6 +48,23 @@ public function testGetCurrentTimestamp() $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 [