| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- declare(strict_types=1);
- namespace SixShop\MakerBundle\Tests\Unit\Generator;
- use PHPUnit\Framework\TestCase;
- use SixShop\MakerBundle\Generator\ModelGenerator;
- use Symfony\Component\Console\Style\SymfonyStyle;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Output\BufferedOutput;
- /**
- * Unit tests for ModelGenerator
- */
- class ModelGeneratorTest extends TestCase
- {
- private ModelGenerator $generator;
- private string $testDir;
- private array $extensionInfo;
- private SymfonyStyle $io;
- protected function setUp(): void
- {
- $this->testDir = sys_get_temp_dir() . '/sixshop_model_test_' . uniqid();
- if (!is_dir($this->testDir)) {
- mkdir($this->testDir, 0755, true);
- }
-
- $this->generator = new ModelGenerator();
- $this->extensionInfo = [
- 'namespace' => 'SixShop\\Test\\',
- 'packageName' => 'sixshop/test',
- 'id' => 'sixshop-test'
- ];
-
- // Create mock SymfonyStyle
- $input = new ArrayInput([]);
- $output = new BufferedOutput();
- $this->io = new SymfonyStyle($input, $output);
- }
- 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 testGenerateBasicModel(): void
- {
- $tableName = 'test_users';
- $tableComment = 'Test users table';
- $fields = [
- ['name' => 'id', 'type' => 'biginteger'],
- ['name' => 'name', 'type' => 'string', 'length' => 100],
- ['name' => 'email', 'type' => 'string', 'length' => 255]
- ];
- $result = $this->generator->generateModel(
- $tableName,
- $tableComment,
- $fields,
- $this->extensionInfo,
- $this->testDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $this->testDir . '/src/Model/TestUsersModel.php';
- $this->assertFileExists($modelFile);
-
- $content = file_get_contents($modelFile);
- $this->assertStringContainsString('class TestUsersModel extends Model', $content);
- $this->assertStringContainsString('protected function getOptions(): array', $content);
- $this->assertStringContainsString("'name' => 'test_users'", $content);
- }
- public function testModelWithComplexFields(): void
- {
- $tableName = 'products';
- $tableComment = 'Products table';
- $fields = [
- ['name' => 'id', 'type' => 'biginteger'],
- ['name' => 'name', 'type' => 'string', 'length' => 200],
- ['name' => 'price', 'type' => 'decimal', 'precision' => 10, 'scale' => 2],
- ['name' => 'is_active', 'type' => 'boolean', 'default' => true],
- ['name' => 'config', 'type' => 'json'],
- ['name' => 'created_at', 'type' => 'timestamp'],
- ['name' => 'updated_at', 'type' => 'timestamp']
- ];
- $result = $this->generator->generateModel(
- $tableName,
- $tableComment,
- $fields,
- $this->extensionInfo,
- $this->testDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $this->testDir . '/src/Model/ProductModel.php';
- $this->assertFileExists($modelFile);
-
- $content = file_get_contents($modelFile);
-
- // Check field type mappings
- $this->assertStringContainsString("'name' => 'string'", $content);
- $this->assertStringContainsString("'price' => 'float'", $content);
- $this->assertStringContainsString("'is_active' => 'boolean'", $content);
- $this->assertStringContainsString("'config' => 'json'", $content);
-
- // Check timestamp configuration
- $this->assertStringContainsString("'auto_timestamp' => true", $content);
- $this->assertStringContainsString("'create_time' => 'create_time'", $content);
- $this->assertStringContainsString("'update_time' => 'update_time'", $content);
- }
- public function testModelNamespaceGeneration(): void
- {
- $tableName = 'orders';
- $fields = [['name' => 'id', 'type' => 'biginteger']];
- $customExtensionInfo = [
- 'namespace' => 'MyVendor\\CustomPackage\\',
- 'packageName' => 'myvendor/custom-package',
- 'id' => 'myvendor-custom-package'
- ];
- $result = $this->generator->generateModel(
- $tableName,
- 'Orders table',
- $fields,
- $customExtensionInfo,
- $this->testDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $this->testDir . '/src/Model/OrderModel.php';
- $this->assertFileExists($modelFile);
-
- $content = file_get_contents($modelFile);
- $this->assertStringContainsString('namespace MyVendor\\CustomPackage\\Model;', $content);
- $this->assertStringContainsString('class OrderModel extends Model', $content);
- }
- public function testModelWithTimestampFieldNaming(): void
- {
- $tableName = 'logs';
- $fields = [
- ['name' => 'id', 'type' => 'biginteger'],
- ['name' => 'message', 'type' => 'text'],
- ['name' => 'created_at', 'type' => 'timestamp'],
- ['name' => 'updated_at', 'type' => 'timestamp'],
- ['name' => 'deleted_at', 'type' => 'timestamp']
- ];
- $result = $this->generator->generateModel(
- $tableName,
- 'Logs table',
- $fields,
- $this->extensionInfo,
- $this->testDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $this->testDir . '/src/Model/LogModel.php';
- $content = file_get_contents($modelFile);
-
- // Check that timestamp fields are mapped to '_time' suffix
- $this->assertStringContainsString("'create_time' => 'create_time'", $content);
- $this->assertStringContainsString("'update_time' => 'update_time'", $content);
- $this->assertStringContainsString("'delete_time' => 'delete_time'", $content);
- }
- public function testModelDirectoryCreation(): void
- {
- $tableName = 'categories';
- $fields = [['name' => 'id', 'type' => 'biginteger']];
- // Use a path that doesn't exist
- $newTestDir = $this->testDir . '/nonexistent/path';
-
- $result = $this->generator->generateModel(
- $tableName,
- 'Categories table',
- $fields,
- $this->extensionInfo,
- $newTestDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $newTestDir . '/src/Model/CategoryModel.php';
- $this->assertFileExists($modelFile);
-
- // Check that directories were created
- $this->assertTrue(is_dir($newTestDir . '/src/Model'));
- }
- public function testModelValidationRules(): void
- {
- $tableName = 'users';
- $fields = [
- ['name' => 'id', 'type' => 'biginteger'],
- ['name' => 'username', 'type' => 'string', 'length' => 50, 'null' => false],
- ['name' => 'email', 'type' => 'string', 'length' => 255, 'null' => false],
- ['name' => 'age', 'type' => 'integer', 'null' => true]
- ];
- $result = $this->generator->generateModel(
- $tableName,
- 'Users table',
- $fields,
- $this->extensionInfo,
- $this->testDir,
- $this->io
- );
- $this->assertTrue($result);
-
- $modelFile = $this->testDir . '/src/Model/UserModel.php';
- $content = file_get_contents($modelFile);
-
- // Check validation rules are generated
- $this->assertStringContainsString("'validate' => [", $content);
- $this->assertStringContainsString("'username' => 'require|max:50'", $content);
- $this->assertStringContainsString("'email' => 'require|email|max:255'", $content);
- }
- }
|