Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testcases for Util::getVersionFromFileName function #2153

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Phinx/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Phinx/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Test\Phinx\Util;

use Phinx\Util\Util;
use RuntimeException;
use Test\Phinx\TestCase;

class UtilTest extends TestCase
Expand Down Expand Up @@ -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 [
Expand Down