ComposerGeneratorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace SixShop\MakerBundle\Tests\Unit\Generator;
  3. use PHPUnit\Framework\TestCase;
  4. use SixShop\MakerBundle\Generator\ComposerGenerator;
  5. use Symfony\Component\Console\Style\SymfonyStyle;
  6. class ComposerGeneratorTest extends TestCase
  7. {
  8. private $composerGenerator;
  9. private $io;
  10. private $testDir;
  11. protected function setUp(): void
  12. {
  13. $this->testDir = sys_get_temp_dir() . '/composer_test_' . uniqid();
  14. mkdir($this->testDir, 0777, true);
  15. // Create mock SymfonyStyle
  16. $this->io = $this->createMock(SymfonyStyle::class);
  17. $this->composerGenerator = new ComposerGenerator();
  18. }
  19. protected function tearDown(): void
  20. {
  21. if (is_dir($this->testDir)) {
  22. $this->removeDirectory($this->testDir);
  23. }
  24. }
  25. private function removeDirectory($dir)
  26. {
  27. if (!is_dir($dir)) {
  28. return;
  29. }
  30. $items = scandir($dir);
  31. foreach ($items as $item) {
  32. if ($item != '.' && $item != '..') {
  33. $path = $dir . DIRECTORY_SEPARATOR . $item;
  34. if (is_dir($path)) {
  35. $this->removeDirectory($path);
  36. } else {
  37. unlink($path);
  38. }
  39. }
  40. }
  41. rmdir($dir);
  42. }
  43. public function testGenerateContent()
  44. {
  45. $packageName = 'six-shop/test-extension';
  46. $namespace = 'SixShop\\TestExtension\\';
  47. $id = 'six-shop-test-extension';
  48. $description = 'A test extension package';
  49. $content = $this->composerGenerator->generateContent($packageName, $namespace, $id, $description);
  50. $this->assertIsString($content);
  51. $this->assertNotEmpty($content);
  52. // Test JSON structure
  53. $jsonData = json_decode($content, true);
  54. $this->assertIsArray($jsonData);
  55. $this->assertEquals(JSON_ERROR_NONE, json_last_error());
  56. // Test package information
  57. $this->assertEquals($packageName, $jsonData['name']);
  58. $this->assertEquals($description, $jsonData['description']);
  59. // Test PSR-4 autoloading
  60. $this->assertArrayHasKey('autoload', $jsonData);
  61. $this->assertArrayHasKey('psr-4', $jsonData['autoload']);
  62. $this->assertArrayHasKey($namespace, $jsonData['autoload']['psr-4']);
  63. $this->assertEquals('src', $jsonData['autoload']['psr-4'][$namespace]);
  64. // Test SixShop extension configuration
  65. $this->assertArrayHasKey('extra', $jsonData);
  66. $this->assertArrayHasKey('sixshop', $jsonData['extra']);
  67. $this->assertEquals($id, $jsonData['extra']['sixshop']['id']);
  68. }
  69. public function testSaveComposerFile()
  70. {
  71. $packageName = 'six-shop/test-extension';
  72. $content = '{"name": "six-shop/test-extension", "description": "Test"}';
  73. $this->io->expects($this->once())
  74. ->method('success')
  75. ->with($this->stringContains('composer.json 文件已成功生成'));
  76. $result = $this->composerGenerator->saveComposerFile($packageName, $content, $this->testDir, $this->io);
  77. $this->assertTrue($result);
  78. $this->assertFileExists($this->testDir . '/composer.json');
  79. $this->assertEquals($content, file_get_contents($this->testDir . '/composer.json'));
  80. }
  81. public function testSaveComposerFileFailure()
  82. {
  83. $packageName = 'six-shop/test-extension';
  84. $content = '{"name": "six-shop/test-extension"}';
  85. $invalidDir = '/invalid/directory/path';
  86. $this->io->expects($this->once())
  87. ->method('error')
  88. ->with($this->stringContains('无法创建目录'));
  89. $result = $this->composerGenerator->saveComposerFile($packageName, $content, $invalidDir, $this->io);
  90. $this->assertFalse($result);
  91. }
  92. public function testCreateComposerJson()
  93. {
  94. $packageName = 'six-shop/test-extension';
  95. $namespace = 'SixShop\\TestExtension\\';
  96. $id = 'six-shop-test-extension';
  97. $description = 'A test extension package';
  98. $this->io->expects($this->once())
  99. ->method('section')
  100. ->with('生成的 composer.json 内容:');
  101. $this->io->expects($this->once())
  102. ->method('text')
  103. ->with($this->isType('string'));
  104. $this->io->expects($this->once())
  105. ->method('confirm')
  106. ->with('是否将内容保存到 composer.json 文件?', true)
  107. ->willReturn(true);
  108. $this->io->expects($this->once())
  109. ->method('success')
  110. ->with($this->stringContains('composer.json 文件已成功生成'));
  111. $result = $this->composerGenerator->createComposerJson(
  112. $packageName,
  113. $namespace,
  114. $id,
  115. $description,
  116. $this->testDir,
  117. $this->io
  118. );
  119. $this->assertTrue($result);
  120. $this->assertFileExists($this->testDir . '/composer.json');
  121. }
  122. public function testCreateComposerJsonWithUserDecline()
  123. {
  124. $packageName = 'six-shop/test-extension';
  125. $namespace = 'SixShop\\TestExtension\\';
  126. $id = 'six-shop-test-extension';
  127. $description = 'A test extension package';
  128. $this->io->expects($this->once())
  129. ->method('confirm')
  130. ->with('是否将内容保存到 composer.json 文件?', true)
  131. ->willReturn(false);
  132. $result = $this->composerGenerator->createComposerJson(
  133. $packageName,
  134. $namespace,
  135. $id,
  136. $description,
  137. $this->testDir,
  138. $this->io
  139. );
  140. $this->assertTrue($result);
  141. $this->assertFileDoesNotExist($this->testDir . '/composer.json');
  142. }
  143. public function testGenerateContentWithDifferentNamespaces()
  144. {
  145. $testCases = [
  146. ['SixShop\\Hello\\', 'six-shop/hello'],
  147. ['MyCompany\\Extensions\\TestExt\\', 'mycompany/test-extension'],
  148. ['SingleNamespace\\', 'vendor/package']
  149. ];
  150. foreach ($testCases as [$namespace, $packageName]) {
  151. $content = $this->composerGenerator->generateContent($packageName, $namespace, 'test-id', 'Test description');
  152. $jsonData = json_decode($content, true);
  153. $this->assertArrayHasKey($namespace, $jsonData['autoload']['psr-4']);
  154. $this->assertEquals('src', $jsonData['autoload']['psr-4'][$namespace]);
  155. }
  156. }
  157. public function testJsonStructureCompliance()
  158. {
  159. $content = $this->composerGenerator->generateContent(
  160. 'six-shop/test-extension',
  161. 'SixShop\\TestExtension\\',
  162. 'test-extension-id',
  163. 'Test description'
  164. );
  165. $jsonData = json_decode($content, true);
  166. // Test required composer.json fields
  167. $this->assertArrayHasKey('name', $jsonData);
  168. $this->assertArrayHasKey('description', $jsonData);
  169. $this->assertArrayHasKey('type', $jsonData);
  170. $this->assertArrayHasKey('require', $jsonData);
  171. $this->assertArrayHasKey('autoload', $jsonData);
  172. $this->assertArrayHasKey('extra', $jsonData);
  173. // Test type field
  174. $this->assertEquals('sixshop-extension', $jsonData['type']);
  175. // Test minimum requirements
  176. $this->assertArrayHasKey('php', $jsonData['require']);
  177. $this->assertEquals('>=8.3', $jsonData['require']['php']);
  178. }
  179. public function testExtensionIdFormat()
  180. {
  181. $testCases = [
  182. 'six-shop/hello' => 'six-shop-hello',
  183. 'vendor/my-extension' => 'vendor-my-extension',
  184. 'company/test_pkg' => 'company-test_pkg'
  185. ];
  186. foreach ($testCases as $packageName => $expectedId) {
  187. $content = $this->composerGenerator->generateContent(
  188. $packageName,
  189. 'Test\\Namespace\\',
  190. $expectedId,
  191. 'Test description'
  192. );
  193. $jsonData = json_decode($content, true);
  194. $this->assertEquals($expectedId, $jsonData['extra']['sixshop']['id']);
  195. }
  196. }
  197. public function testGenerateContentErrorHandling()
  198. {
  199. // Test that the method works with valid parameters
  200. // Since the template exists, this should not throw an exception
  201. $content = $this->composerGenerator->generateContent(
  202. 'test/package',
  203. 'Test\\Namespace\\',
  204. 'test-id',
  205. 'Test description'
  206. );
  207. $this->assertIsString($content);
  208. $this->assertNotEmpty($content);
  209. }
  210. public function testSaveComposerFileDirectoryCreation()
  211. {
  212. $nestedDir = $this->testDir . '/nested/deep/directory';
  213. $packageName = 'six-shop/test-extension';
  214. $content = '{"name": "six-shop/test-extension"}';
  215. $this->io->expects($this->once())
  216. ->method('success');
  217. $result = $this->composerGenerator->saveComposerFile($packageName, $content, $nestedDir, $this->io);
  218. $this->assertTrue($result);
  219. $this->assertDirectoryExists($nestedDir);
  220. $this->assertFileExists($nestedDir . '/composer.json');
  221. }
  222. public function testCompletePackageJsonStructure()
  223. {
  224. $content = $this->composerGenerator->generateContent(
  225. 'six-shop/advanced-extension',
  226. 'SixShop\\AdvancedExtension\\',
  227. 'six-shop-advanced-extension',
  228. 'An advanced SixShop extension with comprehensive features'
  229. );
  230. $jsonData = json_decode($content, true);
  231. // Verify complete structure
  232. $expectedStructure = [
  233. 'name' => 'six-shop/advanced-extension',
  234. 'description' => 'An advanced SixShop extension with comprehensive features',
  235. 'type' => 'sixshop-extension',
  236. 'require' => [
  237. 'php' => '>=8.3',
  238. 'six-shop/core' => '>=0.6 <1.0'
  239. ],
  240. 'autoload' => [
  241. 'psr-4' => [
  242. 'SixShop\\AdvancedExtension\\' => 'src'
  243. ]
  244. ],
  245. 'extra' => [
  246. 'sixshop' => [
  247. 'id' => 'six-shop-advanced-extension',
  248. 'class' => 'SixShop\\AdvancedExtension\\Extension'
  249. ]
  250. ]
  251. ];
  252. $this->assertEquals($expectedStructure['name'], $jsonData['name']);
  253. $this->assertEquals($expectedStructure['description'], $jsonData['description']);
  254. $this->assertEquals($expectedStructure['type'], $jsonData['type']);
  255. $this->assertEquals($expectedStructure['require'], $jsonData['require']);
  256. $this->assertEquals($expectedStructure['autoload'], $jsonData['autoload']);
  257. $this->assertEquals($expectedStructure['extra'], $jsonData['extra']);
  258. }
  259. }