|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Illuminate\Testing\PendingCommand; |
| 6 | +use Tests\TestCase; |
| 7 | + |
| 8 | +class CallArtisanCommandTest extends TestCase |
| 9 | +{ |
| 10 | + protected function setUp(): void |
| 11 | + { |
| 12 | + parent::setUp(); |
| 13 | + |
| 14 | + @unlink(app_path(static::resolvePath('Models/Category.php'))); |
| 15 | + @unlink(app_path(static::resolvePath('Http/Resources/CategoryResource.php'))); |
| 16 | + } |
| 17 | + |
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + parent::tearDown(); |
| 21 | + |
| 22 | + @unlink(app_path(static::resolvePath('Models/Category.php'))); |
| 23 | + @unlink(app_path(static::resolvePath('Http/Resources/CategoryResource.php'))); |
| 24 | + } |
| 25 | + |
| 26 | + private function expectsCommandChoice(PendingCommand $command, $answer): PendingCommand |
| 27 | + { |
| 28 | + $options = [ |
| 29 | + 'model.stub.php', |
| 30 | + static::resolvePath('pack/'), |
| 31 | + ]; |
| 32 | + |
| 33 | + $command->expectsChoice( |
| 34 | + '選擇要使用的 Stub 檔案', |
| 35 | + $answer, |
| 36 | + $options, |
| 37 | + ); |
| 38 | + |
| 39 | + return $command; |
| 40 | + } |
| 41 | + |
| 42 | + // ========================================================================= |
| 43 | + // = Tests |
| 44 | + // ========================================================================= |
| 45 | + |
| 46 | + public function test_call_artisan_command() |
| 47 | + { |
| 48 | + $expectedPutPath = app_path(static::resolvePath('Models/Category.php')); |
| 49 | + |
| 50 | + $command = $this->artisan('make:a...'); |
| 51 | + $command = $this->expectsCommandChoice($command, 'model.stub.php'); |
| 52 | + |
| 53 | + $command->expectsQuestion('請輸入要注入的名稱', 'Category'); |
| 54 | + |
| 55 | + $command->expectsOutput(sprintf('已建立 "%s"', $expectedPutPath)); |
| 56 | + $command->assertExitCode(0); |
| 57 | + $command->run(); |
| 58 | + |
| 59 | + $this->assertFileExists($expectedPutPath); |
| 60 | + $this->assertFileEquals( |
| 61 | + __DIR__ . '/fixtures/category_model.php', |
| 62 | + $expectedPutPath, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_call_artisan_command_in_already_exists_and_overwrite() |
| 67 | + { |
| 68 | + $expectedPutPath = app_path(static::resolvePath('Models/Category.php')); |
| 69 | + |
| 70 | + file_put_contents($expectedPutPath, '__EMPTY__'); |
| 71 | + |
| 72 | + $command = $this->artisan('make:a...'); |
| 73 | + $command = $this->expectsCommandChoice($command, 'model.stub.php'); |
| 74 | + |
| 75 | + $command->expectsQuestion('請輸入要注入的名稱', 'Category'); |
| 76 | + $command->expectsConfirmation( |
| 77 | + sprintf('%s 檔案已存在,是否要覆蓋?', $expectedPutPath), |
| 78 | + 'yes' |
| 79 | + ); |
| 80 | + |
| 81 | + $command->expectsOutput(sprintf('已建立 "%s"', $expectedPutPath)); |
| 82 | + $command->assertExitCode(0); |
| 83 | + $command->run(); |
| 84 | + |
| 85 | + $this->assertFileExists($expectedPutPath); |
| 86 | + $this->assertFileEquals( |
| 87 | + __DIR__ . '/fixtures/category_model.php', |
| 88 | + $expectedPutPath, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_call_artisan_command_in_already_exists_and_without_overwrite() |
| 93 | + { |
| 94 | + $expectedPutPath = app_path(static::resolvePath('Models/Category.php')); |
| 95 | + |
| 96 | + file_put_contents($expectedPutPath, '__EMPTY__'); |
| 97 | + |
| 98 | + $command = $this->artisan('make:a...'); |
| 99 | + $command = $this->expectsCommandChoice($command, 'model.stub.php'); |
| 100 | + |
| 101 | + $command->expectsQuestion('請輸入要注入的名稱', 'Category'); |
| 102 | + $command->expectsConfirmation( |
| 103 | + sprintf('%s 檔案已存在,是否要覆蓋?', $expectedPutPath), |
| 104 | + 'no' |
| 105 | + ); |
| 106 | + |
| 107 | + $command->expectsOutput( |
| 108 | + sprintf('略過處理 %s', $expectedPutPath) |
| 109 | + ); |
| 110 | + |
| 111 | + $command->assertExitCode(0); |
| 112 | + $command->run(); |
| 113 | + |
| 114 | + $this->assertFileExists($expectedPutPath); |
| 115 | + $this->assertEquals( |
| 116 | + '__EMPTY__', |
| 117 | + file_get_contents($expectedPutPath), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + public function test_call_artisan_command_with_filter() |
| 122 | + { |
| 123 | + $expectedPutPath = app_path(static::resolvePath('Models/Category.php')); |
| 124 | + |
| 125 | + $command = $this->artisan('make:a...', [ |
| 126 | + 'filter' => 'model', |
| 127 | + ]); |
| 128 | + |
| 129 | + $command->expectsChoice( |
| 130 | + '選擇要使用的 Stub 檔案', |
| 131 | + 'model.stub.php', |
| 132 | + ['model.stub.php'], |
| 133 | + ); |
| 134 | + |
| 135 | + $command->expectsQuestion('請輸入要注入的名稱', 'Category'); |
| 136 | + |
| 137 | + $command->expectsOutput(sprintf('已建立 "%s"', $expectedPutPath)); |
| 138 | + $command->assertExitCode(0); |
| 139 | + $command->run(); |
| 140 | + |
| 141 | + $this->assertFileExists($expectedPutPath); |
| 142 | + $this->assertFileEquals( |
| 143 | + __DIR__ . '/fixtures/category_model.php', |
| 144 | + $expectedPutPath, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + public function test_call_artisan_command_with_filter_and_not_found() |
| 149 | + { |
| 150 | + $command = $this->artisan('make:a...', [ |
| 151 | + 'filter' => 'foobar', |
| 152 | + ]); |
| 153 | + |
| 154 | + $command->expectsOutput('找不到符合的 Stub 檔案'); |
| 155 | + $command->assertExitCode(0); |
| 156 | + $command->run(); |
| 157 | + } |
| 158 | + |
| 159 | + public function test_call_artisan_command_with_directroy() |
| 160 | + { |
| 161 | + $expectedPutPath1 = app_path(static::resolvePath('Models/Category.php')); |
| 162 | + $expectedPutPath2 = app_path(static::resolvePath('Http/Resources/CategoryResource.php')); |
| 163 | + |
| 164 | + $command = $this->artisan('make:a...'); |
| 165 | + $command = $this->expectsCommandChoice( |
| 166 | + $command, |
| 167 | + static::resolvePath('pack/') |
| 168 | + ); |
| 169 | + |
| 170 | + $command->expectsQuestion('請輸入要注入的名稱', 'Category'); |
| 171 | + |
| 172 | + $command->expectsOutput(sprintf('已建立 "%s"', $expectedPutPath1)); |
| 173 | + $command->expectsOutput(sprintf('已建立 "%s"', $expectedPutPath2)); |
| 174 | + $command->assertExitCode(0); |
| 175 | + $command->run(); |
| 176 | + |
| 177 | + $this->assertFileExists($expectedPutPath1); |
| 178 | + $this->assertFileEquals( |
| 179 | + __DIR__ . '/fixtures/category_model.php', |
| 180 | + $expectedPutPath1, |
| 181 | + ); |
| 182 | + |
| 183 | + $this->assertFileExists($expectedPutPath2); |
| 184 | + $this->assertFileEquals( |
| 185 | + __DIR__ . '/fixtures/category_resource.php', |
| 186 | + $expectedPutPath2, |
| 187 | + ); |
| 188 | + } |
| 189 | +} |
0 commit comments