From 4a3cfd5b0e159bff29d0092e55fc2c5060934bf3 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Thu, 19 Oct 2023 21:56:16 -0500 Subject: [PATCH] Work on migration table functionality, update unit tests --- .../Relationships/AbstractRelationship.php | 16 +++--- .../Relationships/RelationshipInterface.php | 12 ++-- src/Sql/Migrator.php | 4 +- tests/Record/RelationshipTest.php | 4 ++ ...{MigratorTest.php => MigratorFileTest.php} | 4 +- tests/Sql/MigratorTableTest.php | 56 +++++++++++++++++++ tests/TestAsset/Migrations.php | 10 ++++ tests/tmp/migrations2/.table | 1 + .../20191206142442_test_migration.php | 27 +++++++++ 9 files changed, 117 insertions(+), 17 deletions(-) rename tests/Sql/{MigratorTest.php => MigratorFileTest.php} (93%) create mode 100644 tests/Sql/MigratorTableTest.php create mode 100644 tests/TestAsset/Migrations.php create mode 100644 tests/tmp/migrations2/.table create mode 100755 tests/tmp/migrations2/20191206142442_test_migration.php diff --git a/src/Record/Relationships/AbstractRelationship.php b/src/Record/Relationships/AbstractRelationship.php index bc21df1..3973f5d 100644 --- a/src/Record/Relationships/AbstractRelationship.php +++ b/src/Record/Relationships/AbstractRelationship.php @@ -71,9 +71,9 @@ 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; } @@ -81,9 +81,9 @@ public function getForeignTable(): string /** * Get foreign key * - * @return string + * @return string|null */ - public function getForeignKey(): string + public function getForeignKey(): string|null { return $this->foreignKey; } @@ -91,9 +91,9 @@ public function getForeignKey(): string /** * Get options * - * @return array + * @return array|null */ - public function getOptions(): array + public function getOptions(): array|null { return $this->options; } @@ -101,9 +101,9 @@ public function getOptions(): array /** * Get child relationships * - * @return string + * @return string|null */ - public function getChildRelationships(): string + public function getChildRelationships(): string|null { return $this->children; } diff --git a/src/Record/Relationships/RelationshipInterface.php b/src/Record/Relationships/RelationshipInterface.php index beeb504..fa9e015 100644 --- a/src/Record/Relationships/RelationshipInterface.php +++ b/src/Record/Relationships/RelationshipInterface.php @@ -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 diff --git a/src/Sql/Migrator.php b/src/Sql/Migrator.php index 5dd3aaa..5b5f9c7 100644 --- a/src/Sql/Migrator.php +++ b/src/Sql/Migrator.php @@ -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; @@ -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')) : ''; } /** diff --git a/tests/Record/RelationshipTest.php b/tests/Record/RelationshipTest.php index 841f7db..e1b3678 100644 --- a/tests/Record/RelationshipTest.php +++ b/tests/Record/RelationshipTest.php @@ -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(); } diff --git a/tests/Sql/MigratorTest.php b/tests/Sql/MigratorFileTest.php similarity index 93% rename from tests/Sql/MigratorTest.php rename to tests/Sql/MigratorFileTest.php index 8c0c7b3..936ba8f 100644 --- a/tests/Sql/MigratorTest.php +++ b/tests/Sql/MigratorFileTest.php @@ -6,7 +6,7 @@ use Pop\Db\Sql\Migrator; use PHPUnit\Framework\TestCase; -class MigratorTest extends TestCase +class MigratorFileTest extends TestCase { protected $db = null; @@ -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(); } diff --git a/tests/Sql/MigratorTableTest.php b/tests/Sql/MigratorTableTest.php new file mode 100644 index 0000000..dd63597 --- /dev/null +++ b/tests/Sql/MigratorTableTest.php @@ -0,0 +1,56 @@ +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(); + } + +} \ No newline at end of file diff --git a/tests/TestAsset/Migrations.php b/tests/TestAsset/Migrations.php new file mode 100644 index 0000000..af824c9 --- /dev/null +++ b/tests/TestAsset/Migrations.php @@ -0,0 +1,10 @@ +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); + } + +} \ No newline at end of file