Skip to content
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
19 changes: 19 additions & 0 deletions src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ abstract class AbstractMigration implements MigrationInterface
*/
final public function __construct(string $environment, int $version, ?InputInterface $input = null, ?OutputInterface $output = null)
{
$this->validateVersion($version);

$this->environment = $environment;
$this->version = $version;

Expand Down Expand Up @@ -370,4 +372,21 @@ public function shouldExecute(): bool
{
return true;
}

/**
* Makes sure the version int is within range for valid datetime.
* This is required to have a meaningful order in the overview.
*
* @param int $version Version
* @return void
*/
protected function validateVersion(int $version): void
{
$length = strlen((string)$version);
if ($length === 14) {
return;
}

throw new RuntimeException('Invalid version `' . $version . '`, should be in format `YYYYMMDDHHMMSS` (length of 14).');
}
}
44 changes: 26 additions & 18 deletions tests/Phinx/Migration/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AbstractMigrationTest extends TestCase
public function testAdapterMethods()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -30,14 +30,14 @@ public function testAdapterMethods()
public function testGetEnvironment()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);
$this->assertEquals('mockenv', $migrationStub->getEnvironment());
}

public function testSetOutputMethods()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub output
$outputStub = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
Expand All @@ -56,7 +56,7 @@ public function testGetInputMethodWithInjectedInput()
->getMock();

// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0, $inputStub, null]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405, $inputStub, null]);

// test methods
$this->assertNotNull($migrationStub->getInput());
Expand All @@ -70,7 +70,7 @@ public function testGetOutputMethodWithInjectedOutput()
->getMock();

// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0, null, $outputStub]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405, null, $outputStub]);

// test methods
$this->assertNotNull($migrationStub->getOutput());
Expand All @@ -79,7 +79,7 @@ public function testGetOutputMethodWithInjectedOutput()

public function testGetName()
{
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);
$this->assertStringContainsString('AbstractMigration', $migrationStub->getName());
}

Expand All @@ -94,7 +94,7 @@ public function testVersionMethods()
public function testExecute()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -111,7 +111,7 @@ public function testExecute()
public function testQuery()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -128,7 +128,7 @@ public function testQuery()
public function testFetchRow()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -145,7 +145,7 @@ public function testFetchRow()
public function testFetchAll()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -162,7 +162,7 @@ public function testFetchAll()
public function testCreateDatabase()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -179,7 +179,7 @@ public function testCreateDatabase()
public function testDropDatabase()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -196,7 +196,7 @@ public function testDropDatabase()
public function testCreateSchema()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -213,7 +213,7 @@ public function testCreateSchema()
public function testDropSchema()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -230,7 +230,7 @@ public function testDropSchema()
public function testHasTable()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -247,7 +247,7 @@ public function testHasTable()
public function testTableMethod()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

// stub adapter
$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\PdoAdapter')
Expand All @@ -264,7 +264,7 @@ public function testTableMethod()
public function testPostFlightCheckFail()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\MysqlAdapter')
->setConstructorArgs([[]])
Expand All @@ -287,7 +287,7 @@ public function testPostFlightCheckFail()
public function testPostFlightCheckSuccess()
{
// stub migration
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 0]);
$migrationStub = $this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 20230102030405]);

$adapterStub = $this->getMockBuilder('\Phinx\Db\Adapter\MysqlAdapter')
->setConstructorArgs([[]])
Expand All @@ -306,4 +306,12 @@ public function testPostFlightCheckSuccess()
// Dummy assert to prevent the test being marked as risky
$this->assertTrue(true);
}

public function testVersionOutOfRange(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid version `202312030335180`, should be in format `YYYYMMDDHHMMSS` (length of 14).');

$this->getMockForAbstractClass('\Phinx\Migration\AbstractMigration', ['mockenv', 202312030335180]);
}
}