| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- declare(strict_types=1);
- namespace SixShop\MakerBundle\Tests\Unit\Generator;
- use PHPUnit\Framework\TestCase;
- use SixShop\MakerBundle\Generator\MigrationGenerator;
- /**
- * Unit tests for MigrationGenerator
- */
- class MigrationGeneratorTest extends TestCase
- {
- private MigrationGenerator $generator;
- private string $testDir;
- protected function setUp(): void
- {
- $this->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);
- }
- }
|