From ec54978e1b21a2b0ff5fe332b604e07f8051d7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Mon, 11 Nov 2024 14:53:52 +0100 Subject: [PATCH] Add integration test for the DbMigration class [MAILPOET-6314] --- .../integration/Migrator/DbMigrationTest.php | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 mailpoet/tests/integration/Migrator/DbMigrationTest.php diff --git a/mailpoet/tests/integration/Migrator/DbMigrationTest.php b/mailpoet/tests/integration/Migrator/DbMigrationTest.php new file mode 100644 index 0000000000..4ebd4e6d00 --- /dev/null +++ b/mailpoet/tests/integration/Migrator/DbMigrationTest.php @@ -0,0 +1,95 @@ +migration = new class($container) extends DbMigration { + public function run(): void { + } + + public function publicCreateTable(string $tableName, array $attributes): void { + $this->createTable($tableName, $attributes); + } + + public function publicTableExists(string $tableName): bool { + return $this->tableExists($tableName); + } + + public function publicColumnExists(string $tableName, string $columnName): bool { + return $this->columnExists($tableName, $columnName); + } + + public function publicIndexExists(string $tableName, string $indexName): bool { + return $this->indexExists($tableName, $indexName); + } + }; + } + + public function _after(): void { + parent::_after(); + $this->connection->executeStatement("DROP TABLE IF EXISTS {$this->tableName};"); + } + + public function testCreateTable(): void { + $attributes = [ + 'id INT PRIMARY KEY AUTO_INCREMENT', + 'name VARCHAR(255) NOT NULL', + ]; + $this->migration->publicCreateTable($this->tableName, $attributes); + + // Verify that the table was created by querying the database + $tableExists = $this->connection->executeQuery("SHOW TABLES LIKE ?", [Env::$dbPrefix . $this->tableName])->rowCount() > 0; + + // Assert that the table exists + $this->assertTrue($tableExists, "The table '$this->tableName' was not created."); + } + + public function testTableExists(): void { + $attributes = [ + 'id INT PRIMARY KEY AUTO_INCREMENT', + 'name VARCHAR(255) NOT NULL', + ]; + $this->migration->publicCreateTable($this->tableName, $attributes); + + $this->assertTrue($this->migration->publicTableExists(Env::$dbPrefix . $this->tableName)); + $this->assertFalse($this->migration->publicTableExists('non_existent_table')); + } + + public function testColumnExists(): void { + $attributes = [ + 'id INT PRIMARY KEY AUTO_INCREMENT', + 'name VARCHAR(255) NOT NULL', + ]; + $this->migration->publicCreateTable($this->tableName, $attributes); + + $this->assertTrue($this->migration->publicColumnExists(Env::$dbPrefix . $this->tableName, 'name')); + $this->assertFalse($this->migration->publicColumnExists(Env::$dbPrefix . $this->tableName, 'non_existent_column')); + } + + public function testIndexExists(): void { + $attributes = [ + 'id INT PRIMARY KEY AUTO_INCREMENT', + 'name VARCHAR(255) NOT NULL', + ]; + $this->migration->publicCreateTable($this->tableName, $attributes); + + // Add an index for testing + $this->connection->executeStatement("CREATE INDEX test_index ON " . Env::$dbPrefix . $this->tableName . " (name);"); + + $this->assertTrue($this->migration->publicIndexExists(Env::$dbPrefix . $this->tableName, 'test_index')); + $this->assertFalse($this->migration->publicIndexExists(Env::$dbPrefix . $this->tableName, 'non_existent_index')); + } +}