testDir = sys_get_temp_dir() . '/sixshop_test_' . uniqid(); if (!is_dir($this->testDir)) { mkdir($this->testDir, 0755, true); } $this->generator = new MigrationGenerator($this->testDir); } protected function tearDown(): void { if (is_dir($this->testDir)) { $this->removeDirectory($this->testDir); } } private function removeDirectory(string $dir): void { if (is_dir($dir)) { $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . DIRECTORY_SEPARATOR . $file; is_dir($path) ? $this->removeDirectory($path) : unlink($path); } rmdir($dir); } } public function testConstructorSetsCorrectPath(): void { $this->assertEquals($this->testDir, $this->generator->getMigrationsPath()); } public function testGetSupportedFieldTypes(): void { $supportedTypes = $this->generator->getSupportedFieldTypes(); $expectedTypes = [ 'integer', 'biginteger', 'string', 'text', 'boolean', 'decimal', 'float', 'datetime', 'timestamp', 'date', 'time', 'binary', 'json' ]; foreach ($expectedTypes as $type) { $this->assertContains($type, $supportedTypes); } } public function testGenerateCreateTableMigration(): void { $tableName = 'test_users'; $fields = [ [ 'name' => 'id', 'type' => 'biginteger', 'null' => false, 'comment' => 'Primary key' ], [ 'name' => 'username', 'type' => 'string', 'length' => 100, 'null' => false, 'unique' => true, 'comment' => 'User login name' ], [ 'name' => 'email', 'type' => 'string', 'length' => 255, 'null' => false, 'unique' => true, 'comment' => 'User email address' ] ]; $result = $this->generator->generateMigration($tableName, $fields, 'create'); $this->assertTrue($result); // Check if migration file was created $files = glob($this->testDir . '/*_create_test_users_table.php'); $this->assertCount(1, $files); // Check migration content $migrationFile = $files[0]; $content = file_get_contents($migrationFile); $this->assertStringContainsString('class CreateTestUsersTable', $content); $this->assertStringContainsString('$table = $this->table(\'test_users\')', $content); $this->assertStringContainsString('username', $content); $this->assertStringContainsString('email', $content); } public function testGenerateAddColumnMigration(): void { $tableName = 'test_users'; $fields = [ [ 'name' => 'phone', 'type' => 'string', 'length' => 20, 'null' => true, 'comment' => 'User phone number' ] ]; $result = $this->generator->generateMigration($tableName, $fields, 'add_column'); $this->assertTrue($result); // Check if migration file was created $files = glob($this->testDir . '/*_add_column_to_test_users_table.php'); $this->assertCount(1, $files); // Check migration content $migrationFile = $files[0]; $content = file_get_contents($migrationFile); $this->assertStringContainsString('class AddColumnToTestUsersTable', $content); $this->assertStringContainsString('phone', $content); } public function testGenerateDropColumnMigration(): void { $tableName = 'test_users'; $fields = [ ['name' => 'deprecated_field'] ]; $result = $this->generator->generateMigration($tableName, $fields, 'drop_column'); $this->assertTrue($result); // Check if migration file was created $files = glob($this->testDir . '/*_drop_column_from_test_users_table.php'); $this->assertCount(1, $files); // Check migration content $migrationFile = $files[0]; $content = file_get_contents($migrationFile); $this->assertStringContainsString('class DropColumnFromTestUsersTable', $content); $this->assertStringContainsString('deprecated_field', $content); } public function testDecimalFieldOptions(): void { $tableName = 'test_products'; $fields = [ [ 'name' => 'price', 'type' => 'decimal', 'precision' => 10, 'scale' => 2, 'null' => false, 'comment' => 'Product price' ] ]; $result = $this->generator->generateMigration($tableName, $fields, 'create'); $this->assertTrue($result); // Check migration content for decimal precision and scale $files = glob($this->testDir . '/*_create_test_products_table.php'); $this->assertCount(1, $files); $migrationFile = $files[0]; $content = file_get_contents($migrationFile); $this->assertStringContainsString('\'precision\' => 10', $content); $this->assertStringContainsString('\'scale\' => 2', $content); } public function testJsonFieldType(): void { $tableName = 'test_settings'; $fields = [ [ 'name' => 'config_data', 'type' => 'json', 'null' => true, 'comment' => 'Configuration JSON data' ] ]; $result = $this->generator->generateMigration($tableName, $fields, 'create'); $this->assertTrue($result); // Check migration content for JSON field $files = glob($this->testDir . '/*_create_test_settings_table.php'); $this->assertCount(1, $files); $migrationFile = $files[0]; $content = file_get_contents($migrationFile); $this->assertStringContainsString('config_data', $content); $this->assertStringContainsString('json', $content); } public function testMigrationPathCreation(): void { $newPath = sys_get_temp_dir() . '/sixshop_new_path_' . uniqid(); $generator = new MigrationGenerator($newPath); $fields = [['name' => 'test_field', 'type' => 'string']]; $result = $generator->generateMigration('test_table', $fields, 'create'); $this->assertTrue($result); $this->assertTrue(is_dir($newPath)); // Cleanup $this->removeDirectory($newPath); } public function testInvalidActionType(): void { $tableName = 'test_table'; $fields = [['name' => 'test_field', 'type' => 'string']]; $result = $this->generator->generateMigration($tableName, $fields, 'invalid_action'); // Should handle gracefully and return false or throw exception $this->assertFalse($result); } }