Skip to content

Commit b30018d

Browse files
Added tests
1 parent cb3f6d5 commit b30018d

File tree

9 files changed

+205
-63
lines changed

9 files changed

+205
-63
lines changed

tests/_support/Helper/Mysql.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use PDO;
1010
use Phalcon\Config;
1111
use Phalcon\Db\Adapter\Pdo\AbstractPdo;
12-
use Phalcon\Db\Adapter\PdoFactory;
12+
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
13+
use Phalcon\Migrations\Db\Dialect\DialectMysql;
1314
use Phalcon\Migrations\Migrations;
1415

1516
class Mysql extends Module
@@ -22,11 +23,8 @@ class Mysql extends Module
2223
public function _initialize()
2324
{
2425
/** @var AbstractPdo $db */
25-
self::$phalconDb = (new PdoFactory())
26-
->newInstance(
27-
'mysql',
28-
$this->getMigrationsConfig()->get('database')->toArray()
29-
);
26+
self::$phalconDb = new PdoMysql($this->getMigrationsConfig()->get('database')->toArray());
27+
self::$phalconDb->setDialect(new DialectMysql());
3028
}
3129

3230
public function _before(TestInterface $test)

tests/_support/Helper/Postgresql.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
use Codeception\TestInterface;
99
use Phalcon\Config;
1010
use Phalcon\Db\Adapter\Pdo\AbstractPdo;
11-
use Phalcon\Db\Adapter\PdoFactory;
11+
use Phalcon\Migrations\Db\Adapter\Pdo\PdoPostgresql;
12+
use Phalcon\Migrations\Db\Dialect\DialectPostgresql;
1213
use Phalcon\Migrations\Migrations;
1314

1415
class Postgresql extends Module
@@ -33,11 +34,8 @@ public function _initialize()
3334

3435
self::$defaultSchema = getenv('POSTGRES_TEST_DB_SCHEMA');
3536
/** @var AbstractPdo $db */
36-
self::$phalconDb = (new PdoFactory())
37-
->newInstance(
38-
'postgresql',
39-
$options
40-
);
37+
self::$phalconDb = new PdoPostgresql($options);
38+
self::$phalconDb->setDialect(new DialectPostgresql());
4139
}
4240

4341
public function _before(TestInterface $test)

tests/_support/MysqlTester.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ class MysqlTester extends \Codeception\Actor
2222
/**
2323
* Define custom actions here
2424
*/
25+
public function seeExceptionThrown($exception, $function): ?bool
26+
{
27+
try {
28+
$function();
29+
30+
return false;
31+
} catch (\Throwable $throwable) {
32+
return get_class($throwable) === $exception;
33+
}
34+
}
2535
}

tests/_support/PostgresqlTester.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@
1414
* @method void pause()
1515
*
1616
* @SuppressWarnings(PHPMD)
17-
*/
17+
*/
1818
class PostgresqlTester extends \Codeception\Actor
1919
{
2020
use _generated\PostgresqlTesterActions;
2121

22-
/**
23-
* Define custom actions here
24-
*/
22+
/**
23+
* Define custom actions here
24+
*/
25+
public function seeExceptionThrown($exception, $function): ?bool
26+
{
27+
try {
28+
$function();
29+
30+
return false;
31+
} catch (\Throwable $throwable) {
32+
return get_class($throwable) === $exception;
33+
}
34+
}
2535
}

tests/cli/GenerateCest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function generateFirstVersion(CliTester $I): void
5555
'size' => 10,
5656
'notNull' => true,
5757
]),
58+
new Column('num_point', [
59+
'type' => Column::TYPE_FLOAT,
60+
'notNull' => true,
61+
]),
5862
],
5963
]);
6064

tests/integration/Migration/Action/GenerateCest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ final class GenerateCest
2323
* @param IntegrationTester $I
2424
* @throws UnknownColumnTypeException
2525
*/
26-
public function construct(IntegrationTester $I): void
26+
public function constructMysql(IntegrationTester $I): void
2727
{
28-
$I->wantToTest('Migration\Action\Generate - __construct()');
28+
$I->wantToTest('Migration\Action\Generate - __construct(Mysql)');
2929

3030
$adapter = 'mysql';
3131
$class = new Generate($adapter);
@@ -38,4 +38,24 @@ public function construct(IntegrationTester $I): void
3838
$I->assertIsArray($class->getNumericColumns());
3939
$I->assertNull($class->getPrimaryColumnName());
4040
}
41+
42+
/**
43+
* @param IntegrationTester $I
44+
* @throws UnknownColumnTypeException
45+
*/
46+
public function constructPostgresql(IntegrationTester $I): void
47+
{
48+
$I->wantToTest('Migration\Action\Generate - __construct(Postgresql)');
49+
50+
$adapter = 'postgresql';
51+
$class = new Generate($adapter);
52+
53+
$I->assertSame($adapter, $class->getAdapter());
54+
$I->assertIsObject($class->getColumns());
55+
$I->assertIsObject($class->getIndexes());
56+
$I->assertIsObject($class->getReferences());
57+
$I->assertIsArray($class->getOptions(false));
58+
$I->assertIsArray($class->getNumericColumns());
59+
$I->assertNull($class->getPrimaryColumnName());
60+
}
4161
}

tests/mysql/Issue29Cest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use function codecept_data_dir;
2020

21-
class Issue2Cest
21+
class IssuesCest
2222
{
2323
/**
2424
* @param MysqlTester $I
@@ -41,4 +41,26 @@ public function testDisableEnableForeignKeyChecks(MysqlTester $I): void
4141
$I->assertTrue($I->getPhalconDb()->tableExists('client'));
4242
$I->assertArrayHasKey('fk_accessToken_client_1', $I->getPhalconDb()->describeReferences('accessToken'));
4343
}
44+
45+
/**
46+
* @param MysqlTester $I
47+
* @throws \Phalcon\Migrations\Script\ScriptException
48+
* @throws \Phalcon\Mvc\Model\Exception
49+
*/
50+
public function testIssue29(MysqlTester $I): void
51+
{
52+
$I->wantToTest('Issue #29 - Foreign key was created');
53+
54+
ob_start();
55+
Migrations::run([
56+
'migrationsDir' => codecept_data_dir('issues/29'),
57+
'config' => $I->getMigrationsConfig(),
58+
'migrationsInDb' => true,
59+
]);
60+
ob_clean();
61+
62+
$I->assertTrue($I->getPhalconDb()->tableExists('tasks'));
63+
$I->assertTrue($I->getPhalconDb()->tableExists('task_jobs'));
64+
$I->assertArrayHasKey('task_jobs_tasks_id_fk', $I->getPhalconDb()->describeReferences('task_jobs'));
65+
}
4466
}

tests/postgresql/IssuesCest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313

1414
namespace Phalcon\Migrations\Tests\Postgresql;
1515

16+
use Exception;
1617
use Phalcon\Db\Column;
18+
use Phalcon\Db\Index;
19+
use Phalcon\Migrations\Db\Adapter\Pdo\PdoPostgresql;
1720
use Phalcon\Migrations\Migrations;
1821
use PostgresqlTester;
1922

2023
final class IssuesCest
2124
{
2225
public function issue1(PostgresqlTester $I): void
2326
{
27+
$I->wantToTest('Issue #1 - Primary key was created');
28+
2429
$tableName = 'table_primary_test';
2530
$migrationsDir = codecept_output_dir(__FUNCTION__);
2631

@@ -56,4 +61,123 @@ public function issue1(PostgresqlTester $I): void
5661

5762
$I->assertSame(1, count($indexes));
5863
}
64+
65+
/**
66+
* @throws Exception
67+
*/
68+
public function testIssue111Fail(PostgresqlTester $I): void
69+
{
70+
$I->wantToTest('Issue #111 - Unrecognized PostgreSQL data type [FAIL]');
71+
72+
$tableName = 'pg_phalcon_double';
73+
$migrationsDir = codecept_output_dir(__FUNCTION__);
74+
75+
$I->seeExceptionThrown(
76+
Phalcon\Db\Exception::class,
77+
function () use ($I, $tableName) {
78+
$I->getPhalconDb()->createTable($tableName, $I->getDefaultSchema(), [
79+
'columns' => [
80+
new Column('point_double_column', [
81+
'type' => Column::TYPE_DOUBLE,
82+
'default' => 0,
83+
'notNull' => false,
84+
'comment' => "Double typed column",
85+
]),
86+
],
87+
]);
88+
}
89+
);
90+
91+
ob_start();
92+
Migrations::generate([
93+
'migrationsDir' => [
94+
$migrationsDir,
95+
],
96+
'config' => $I->getMigrationsConfig(),
97+
'tableName' => '@',
98+
]);
99+
$I->getPhalconDb()->dropTable($tableName);
100+
Migrations::run([
101+
'migrationsDir' => $migrationsDir,
102+
'config' => $I->getMigrationsConfig(),
103+
'migrationsInDb' => true,
104+
]);
105+
ob_clean();
106+
107+
$indexes = $I->getPhalconDb()->describeIndexes(Migrations::MIGRATION_LOG_TABLE);
108+
109+
$I->assertFalse($I->getPhalconDb()->tableExists($tableName, $I->getDefaultSchema()));
110+
$I->assertTrue($I->getPhalconDb()->tableExists(Migrations::MIGRATION_LOG_TABLE, $I->getDefaultSchema()));
111+
$I->assertSame(1, count($indexes));
112+
}
113+
114+
/**
115+
* @throws Exception
116+
*/
117+
public function testIssue111Fixed(PostgresqlTester $I): void
118+
{
119+
$I->wantToTest('Issue #111 - Unrecognized PostgreSQL data type [FIXED]');
120+
121+
$tableName = 'pg_phalcon_double';
122+
$migrationsDir = codecept_output_dir(__FUNCTION__);
123+
$I->getPhalconDb()->createTable($tableName, $I->getDefaultSchema(), [
124+
'columns' => [
125+
new Column('point_double_column', [
126+
'type' => Column::TYPE_FLOAT,
127+
'default' => 0,
128+
'notNull' => false,
129+
'comment' => "Double typed column",
130+
]),
131+
],
132+
]);
133+
134+
ob_start();
135+
Migrations::generate([
136+
'migrationsDir' => [
137+
$migrationsDir,
138+
],
139+
'config' => $I->getMigrationsConfig(),
140+
'tableName' => '@',
141+
]);
142+
$I->getPhalconDb()->dropTable($tableName);
143+
Migrations::run([
144+
'migrationsDir' => $migrationsDir,
145+
'config' => $I->getMigrationsConfig(),
146+
'migrationsInDb' => true,
147+
]);
148+
ob_clean();
149+
150+
$indexes = $I->getPhalconDb()->describeIndexes(Migrations::MIGRATION_LOG_TABLE);
151+
152+
$I->assertTrue($I->getPhalconDb()->tableExists($tableName, $I->getDefaultSchema()));
153+
$I->assertTrue($I->getPhalconDb()->tableExists(Migrations::MIGRATION_LOG_TABLE, $I->getDefaultSchema()));
154+
$I->assertSame(1, count($indexes));
155+
}
156+
157+
/**
158+
* @throws Exception
159+
*/
160+
public function testIssue112(PostgresqlTester $I): void
161+
{
162+
$I->wantToTest('Issue #112 - Index should be primary key, instead of Normal Index');
163+
164+
$tableName = 'pg_phalcon_primary_index';
165+
$I->getPhalconDb()->createTable($tableName, $I->getDefaultSchema(), [
166+
'columns' => [
167+
new Column('id', [
168+
'type' => Column::TYPE_INTEGER,
169+
'notNull' => true,
170+
'first' => true,
171+
]),
172+
],
173+
'indexes' => [
174+
new Index('pk_id_0', ['id'], 'PRIMARY KEY'),
175+
],
176+
]);
177+
178+
$indexes = $I->getPhalconDb()->describeIndexes($tableName, $I->getDefaultSchema());
179+
$index = array_shift($indexes);
180+
181+
$I->assertSame(PdoPostgresql::INDEX_TYPE_PRIMARY, $index->getType());
182+
}
59183
}

0 commit comments

Comments
 (0)