ModelGeneratorTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\MakerBundle\Tests\Unit\Generator;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\MakerBundle\Generator\ModelGenerator;
  6. use Symfony\Component\Console\Style\SymfonyStyle;
  7. use Symfony\Component\Console\Input\ArrayInput;
  8. use Symfony\Component\Console\Output\BufferedOutput;
  9. /**
  10. * Unit tests for ModelGenerator
  11. */
  12. class ModelGeneratorTest extends TestCase
  13. {
  14. private ModelGenerator $generator;
  15. private string $testDir;
  16. private array $extensionInfo;
  17. private SymfonyStyle $io;
  18. protected function setUp(): void
  19. {
  20. $this->testDir = sys_get_temp_dir() . '/sixshop_model_test_' . uniqid();
  21. if (!is_dir($this->testDir)) {
  22. mkdir($this->testDir, 0755, true);
  23. }
  24. $this->generator = new ModelGenerator();
  25. $this->extensionInfo = [
  26. 'namespace' => 'SixShop\\Test\\',
  27. 'packageName' => 'sixshop/test',
  28. 'id' => 'sixshop-test'
  29. ];
  30. // Create mock SymfonyStyle
  31. $input = new ArrayInput([]);
  32. $output = new BufferedOutput();
  33. $this->io = new SymfonyStyle($input, $output);
  34. }
  35. protected function tearDown(): void
  36. {
  37. if (is_dir($this->testDir)) {
  38. $this->removeDirectory($this->testDir);
  39. }
  40. }
  41. private function removeDirectory(string $dir): void
  42. {
  43. if (is_dir($dir)) {
  44. $files = array_diff(scandir($dir), ['.', '..']);
  45. foreach ($files as $file) {
  46. $path = $dir . DIRECTORY_SEPARATOR . $file;
  47. is_dir($path) ? $this->removeDirectory($path) : unlink($path);
  48. }
  49. rmdir($dir);
  50. }
  51. }
  52. public function testGenerateBasicModel(): void
  53. {
  54. $tableName = 'test_users';
  55. $tableComment = 'Test users table';
  56. $fields = [
  57. ['name' => 'id', 'type' => 'biginteger'],
  58. ['name' => 'name', 'type' => 'string', 'length' => 100],
  59. ['name' => 'email', 'type' => 'string', 'length' => 255]
  60. ];
  61. $result = $this->generator->generateModel(
  62. $tableName,
  63. $tableComment,
  64. $fields,
  65. $this->extensionInfo,
  66. $this->testDir,
  67. $this->io
  68. );
  69. $this->assertTrue($result);
  70. $modelFile = $this->testDir . '/src/Model/TestUsersModel.php';
  71. $this->assertFileExists($modelFile);
  72. $content = file_get_contents($modelFile);
  73. $this->assertStringContainsString('class TestUsersModel extends Model', $content);
  74. $this->assertStringContainsString('protected function getOptions(): array', $content);
  75. $this->assertStringContainsString("'name' => 'test_users'", $content);
  76. }
  77. public function testModelWithComplexFields(): void
  78. {
  79. $tableName = 'products';
  80. $tableComment = 'Products table';
  81. $fields = [
  82. ['name' => 'id', 'type' => 'biginteger'],
  83. ['name' => 'name', 'type' => 'string', 'length' => 200],
  84. ['name' => 'price', 'type' => 'decimal', 'precision' => 10, 'scale' => 2],
  85. ['name' => 'is_active', 'type' => 'boolean', 'default' => true],
  86. ['name' => 'config', 'type' => 'json'],
  87. ['name' => 'created_at', 'type' => 'timestamp'],
  88. ['name' => 'updated_at', 'type' => 'timestamp']
  89. ];
  90. $result = $this->generator->generateModel(
  91. $tableName,
  92. $tableComment,
  93. $fields,
  94. $this->extensionInfo,
  95. $this->testDir,
  96. $this->io
  97. );
  98. $this->assertTrue($result);
  99. $modelFile = $this->testDir . '/src/Model/ProductModel.php';
  100. $this->assertFileExists($modelFile);
  101. $content = file_get_contents($modelFile);
  102. // Check field type mappings
  103. $this->assertStringContainsString("'name' => 'string'", $content);
  104. $this->assertStringContainsString("'price' => 'float'", $content);
  105. $this->assertStringContainsString("'is_active' => 'boolean'", $content);
  106. $this->assertStringContainsString("'config' => 'json'", $content);
  107. // Check timestamp configuration
  108. $this->assertStringContainsString("'auto_timestamp' => true", $content);
  109. $this->assertStringContainsString("'create_time' => 'create_time'", $content);
  110. $this->assertStringContainsString("'update_time' => 'update_time'", $content);
  111. }
  112. public function testModelNamespaceGeneration(): void
  113. {
  114. $tableName = 'orders';
  115. $fields = [['name' => 'id', 'type' => 'biginteger']];
  116. $customExtensionInfo = [
  117. 'namespace' => 'MyVendor\\CustomPackage\\',
  118. 'packageName' => 'myvendor/custom-package',
  119. 'id' => 'myvendor-custom-package'
  120. ];
  121. $result = $this->generator->generateModel(
  122. $tableName,
  123. 'Orders table',
  124. $fields,
  125. $customExtensionInfo,
  126. $this->testDir,
  127. $this->io
  128. );
  129. $this->assertTrue($result);
  130. $modelFile = $this->testDir . '/src/Model/OrderModel.php';
  131. $this->assertFileExists($modelFile);
  132. $content = file_get_contents($modelFile);
  133. $this->assertStringContainsString('namespace MyVendor\\CustomPackage\\Model;', $content);
  134. $this->assertStringContainsString('class OrderModel extends Model', $content);
  135. }
  136. public function testModelWithTimestampFieldNaming(): void
  137. {
  138. $tableName = 'logs';
  139. $fields = [
  140. ['name' => 'id', 'type' => 'biginteger'],
  141. ['name' => 'message', 'type' => 'text'],
  142. ['name' => 'created_at', 'type' => 'timestamp'],
  143. ['name' => 'updated_at', 'type' => 'timestamp'],
  144. ['name' => 'deleted_at', 'type' => 'timestamp']
  145. ];
  146. $result = $this->generator->generateModel(
  147. $tableName,
  148. 'Logs table',
  149. $fields,
  150. $this->extensionInfo,
  151. $this->testDir,
  152. $this->io
  153. );
  154. $this->assertTrue($result);
  155. $modelFile = $this->testDir . '/src/Model/LogModel.php';
  156. $content = file_get_contents($modelFile);
  157. // Check that timestamp fields are mapped to '_time' suffix
  158. $this->assertStringContainsString("'create_time' => 'create_time'", $content);
  159. $this->assertStringContainsString("'update_time' => 'update_time'", $content);
  160. $this->assertStringContainsString("'delete_time' => 'delete_time'", $content);
  161. }
  162. public function testModelDirectoryCreation(): void
  163. {
  164. $tableName = 'categories';
  165. $fields = [['name' => 'id', 'type' => 'biginteger']];
  166. // Use a path that doesn't exist
  167. $newTestDir = $this->testDir . '/nonexistent/path';
  168. $result = $this->generator->generateModel(
  169. $tableName,
  170. 'Categories table',
  171. $fields,
  172. $this->extensionInfo,
  173. $newTestDir,
  174. $this->io
  175. );
  176. $this->assertTrue($result);
  177. $modelFile = $newTestDir . '/src/Model/CategoryModel.php';
  178. $this->assertFileExists($modelFile);
  179. // Check that directories were created
  180. $this->assertTrue(is_dir($newTestDir . '/src/Model'));
  181. }
  182. public function testModelValidationRules(): void
  183. {
  184. $tableName = 'users';
  185. $fields = [
  186. ['name' => 'id', 'type' => 'biginteger'],
  187. ['name' => 'username', 'type' => 'string', 'length' => 50, 'null' => false],
  188. ['name' => 'email', 'type' => 'string', 'length' => 255, 'null' => false],
  189. ['name' => 'age', 'type' => 'integer', 'null' => true]
  190. ];
  191. $result = $this->generator->generateModel(
  192. $tableName,
  193. 'Users table',
  194. $fields,
  195. $this->extensionInfo,
  196. $this->testDir,
  197. $this->io
  198. );
  199. $this->assertTrue($result);
  200. $modelFile = $this->testDir . '/src/Model/UserModel.php';
  201. $content = file_get_contents($modelFile);
  202. // Check validation rules are generated
  203. $this->assertStringContainsString("'validate' => [", $content);
  204. $this->assertStringContainsString("'username' => 'require|max:50'", $content);
  205. $this->assertStringContainsString("'email' => 'require|email|max:255'", $content);
  206. }
  207. }