Skip to content

Commit

Permalink
Merge pull request #27 from phalcon/fix/#8-column-type-time
Browse files Browse the repository at this point in the history
#8 - Column type time
  • Loading branch information
Jeckerson authored Jan 1, 2020
2 parents b5b9d5a + 9097c1b commit b6d751a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,12 @@ public static function getCompletedVersions(array $options): array

return array_flip($completedVersions);
}

/**
* In case we need to renew our DB connection or file
*/
public static function resetStorage(): void
{
self::$storage = null;
}
}
3 changes: 3 additions & 0 deletions src/Mvc/Model/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public static function generate(
case Column::TYPE_CHAR:
$fieldDefinition[] = "'type' => Column::TYPE_CHAR";
break;
case Column::TYPE_TIME:
$fieldDefinition[] = "'type' => Column::TYPE_TIME";
break;
case Column::TYPE_DATE:
$fieldDefinition[] = "'type' => Column::TYPE_DATE";
break;
Expand Down
95 changes: 95 additions & 0 deletions tests/Integration/ColumnTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);

namespace Phalcon\Migrations\Tests\Integration;

use Exception;
use Phalcon\Db\Column;
use Phalcon\Db\Enum;
use Phalcon\Helper\Arr;
use Phalcon\Migrations\Migrations;
use function Phalcon\Migrations\Tests\remove_dir;
use function Phalcon\Migrations\Tests\root_path;

final class ColumnTypesTest extends IntegrationTestCase
{
public function columnsDataProvider(): array
{
return [
[
'column_int',
[
'type' => Column::TYPE_INTEGER,
'size' => 10,
'unsigned' => true,
'notNull' => true,
'first' => true,
],
[0, 1, 123, 9000],
],
[
'column_time',
[
'type' => Column::TYPE_TIME,
],
['00:00:00', '23:59:55', '12:00:12'],
]
];
}

/**
* @dataProvider columnsDataProvider
*
* @param string $columnName
* @param array $definition
* @param array $values
*
* @throws \Phalcon\Migrations\Script\ScriptException
* @throws \Phalcon\Mvc\Model\Exception
* @throws Exception
*/
public function testColumnDefinition(string $columnName, array $definition, array $values): void
{
$tableName = $columnName . '_test';
$migrationsDir = root_path('tests/var/output/' . __FUNCTION__);

$this->db->createTable($tableName, getenv('TEST_DB_DATABASE'), [
'columns' => [
new Column($columnName, $definition),
],
]);

/**
* Generate | Drop | Run
*/
Migrations::generate([
'migrationsDir' => $migrationsDir,
'config' => self::$generateConfig,
'tableName' => $tableName,
]);
$this->db->query('DROP TABLE ' . $tableName);
Migrations::run([
'migrationsDir' => $migrationsDir,
'config' => self::$generateConfig,
'migrationsInDb' => true,
]);

/**
* Insert values
*/
foreach ($values as $value) {
$this->db->insert($tableName, [$value], [$columnName]);
}

Migrations::resetStorage();
remove_dir($migrationsDir);

/** @var Column $column */
$column = $this->db->describeColumns($tableName)[0];
$rows = $this->db->fetchAll("SELECT $columnName FROM $tableName", Enum::FETCH_ASSOC);
$rows = Arr::flatten($rows);

$this->assertSame($definition['type'], $column->getType());
$this->assertEquals($values, $rows);
}
}
3 changes: 3 additions & 0 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static function setUpBeforeClass(): void
'password' => getenv('TEST_DB_PASSWORD'),
'dbname' => getenv('TEST_DB_DATABASE'),
],
'application' => [
'logInDb' => true,
],
]);

ob_start();
Expand Down

0 comments on commit b6d751a

Please sign in to comment.