Skip to content

Commit

Permalink
Work on migration table functionality, update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 20, 2023
1 parent 1e830fb commit 4a3cfd5
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/Record/Relationships/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,39 @@ public function __construct(string $foreignTable, string $foreignKey, ?array $op
/**
* Get foreign table class
*
* @return string
* @return string|null
*/
public function getForeignTable(): string
public function getForeignTable(): string|null
{
return $this->foreignTable;
}

/**
* Get foreign key
*
* @return string
* @return string|null
*/
public function getForeignKey(): string
public function getForeignKey(): string|null
{
return $this->foreignKey;
}

/**
* Get options
*
* @return array
* @return array|null
*/
public function getOptions(): array
public function getOptions(): array|null
{
return $this->options;
}

/**
* Get child relationships
*
* @return string
* @return string|null
*/
public function getChildRelationships(): string
public function getChildRelationships(): string|null
{
return $this->children;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Record/Relationships/RelationshipInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ interface RelationshipInterface
/**
* Get foreign table
*
* @return string
* @return string|null
*/
public function getForeignTable(): string;
public function getForeignTable(): string|null;

/**
* Get foreign key
*
* @return string
* @return string|null
*/
public function getForeignKey(): string;
public function getForeignKey(): string|null;

/**
* Get options
*
* @return array
* @return array|null
*/
public function getOptions(): array;
public function getOptions(): array|null;

/**
* Get eager relationships
Expand Down
4 changes: 2 additions & 2 deletions src/Sql/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function isFile(): bool
public function isTable(): bool
{
if (file_exists($this->path . DIRECTORY_SEPARATOR . '.table')) {
$table = file_get_contents($this->path . DIRECTORY_SEPARATOR . '.table');
$table = trim(file_get_contents($this->path . DIRECTORY_SEPARATOR . '.table'));
return (class_exists($table) && is_subclass_of($table, 'Pop\Db\Record'));
} else {
return false;
Expand Down Expand Up @@ -310,7 +310,7 @@ public function hasTable(): bool
public function getTable(): string
{
return (file_exists($this->path . DIRECTORY_SEPARATOR . '.table')) ?
file_get_contents($this->path . DIRECTORY_SEPARATOR . '.table') : '';
trim(file_get_contents($this->path . DIRECTORY_SEPARATOR . '.table')) : '';
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Record/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public function testBelongsToRelationship()
$relationship = new Relationships\BelongsTo($info, 'Pop\Db\Test\TestAsset\People', 'id');
$this->assertInstanceOf('Pop\Db\Test\TestAsset\PeopleInfo', $relationship->getChild());
$this->assertIsArray($relationship->getEagerRelationships([1]));
$this->assertEquals('Pop\Db\Test\TestAsset\People', $relationship->getForeignTable());
$this->assertEmpty($relationship->getOptions());
$relationship->setChildRelationships('TestChild');
$this->assertEquals('TestChild', $relationship->getChildRelationships());
$this->db->disconnect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Pop\Db\Sql\Migrator;
use PHPUnit\Framework\TestCase;

class MigratorTest extends TestCase
class MigratorFileTest extends TestCase
{

protected $db = null;
Expand All @@ -29,6 +29,8 @@ public function testConstructor()
$this->assertInstanceOf('Pop\Db\Adapter\Mysql', $migrator->db());
$this->assertEquals(__DIR__ . '/../tmp/migrations', $migrator->getPath());
$this->assertNull($migrator->getCurrent());
$this->assertFalse($migrator->hasTable());
$this->assertEquals('', $migrator->getTable());
$this->db->disconnect();
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Sql/MigratorTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Pop\Db\Test\Sql;

use Pop\Db\Db;
use Pop\Db\Sql\Migrator;
use PHPUnit\Framework\TestCase;

class MigratorTableTest extends TestCase
{

protected $db = null;

public function setUp(): void
{
$this->db = Db::mysqlConnect([
'database' => $_ENV['MYSQL_DB'],
'username' => $_ENV['MYSQL_USER'],
'password' => $_ENV['MYSQL_PASS'],
'host' => $_ENV['MYSQL_HOST']
]);
\Pop\Db\Test\TestAsset\Migrations::setDb($this->db);
}

public function testConstructor()
{
$migrator = new Migrator($this->db, __DIR__ . '/../tmp/migrations2');
$this->assertInstanceOf('Pop\Db\Sql\Migrator', $migrator);
$this->assertInstanceOf('Pop\Db\Adapter\Mysql', $migrator->getDb());
$this->assertInstanceOf('Pop\Db\Adapter\Mysql', $migrator->db());
$this->assertEquals(__DIR__ . '/../tmp/migrations2', $migrator->getPath());
$this->assertNull($migrator->getCurrent());
$this->assertTrue($migrator->hasTable());
$this->assertEquals('Pop\Db\Test\TestAsset\Migrations', $migrator->getTable());
$this->db->disconnect();
}

public function testRun()
{
$migrator = new Migrator($this->db, __DIR__ . '/../tmp/migrations2');
$this->assertFalse($this->db->hasTable('test_users'));
$migrator->runAll();
$this->assertTrue($this->db->hasTable('test_users'));
$this->db->disconnect();
}

public function testRollback()
{
$migrator = new Migrator($this->db, __DIR__ . '/../tmp/migrations2');
$this->assertTrue($this->db->hasTable('test_users'));
$migrator->rollbackAll();
$this->assertFalse($this->db->hasTable('test_users'));
$this->db->disconnect();
}

}
10 changes: 10 additions & 0 deletions tests/TestAsset/Migrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Pop\Db\Test\TestAsset;

use Pop\Db\Record;

class Migrations extends Record
{

}
1 change: 1 addition & 0 deletions tests/tmp/migrations2/.table
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pop\Db\Test\TestAsset\Migrations
27 changes: 27 additions & 0 deletions tests/tmp/migrations2/20191206142442_test_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Pop\Db\Sql\Migration\AbstractMigration;

class TestMigration extends AbstractMigration
{

public function up(): void
{
$schema = $this->db->createSchema();
$schema->create('test_users')
->int('id', 16)
->varchar('username', 255)
->varchar('password', 255)
->primary('id');

$this->db->query($schema);
}

public function down(): void
{
$schema = $this->db->createSchema();
$schema->drop('test_users');
$this->db->query($schema);
}

}

0 comments on commit 4a3cfd5

Please sign in to comment.