|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir command-line tool. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Cli\Tests\Integration\Command\Laravel; |
| 15 | + |
| 16 | +use Ymir\Cli\Command\Laravel\ArtisanCommand; |
| 17 | +use Ymir\Cli\Exception\InvalidInputException; |
| 18 | +use Ymir\Cli\Exception\Project\UnsupportedProjectException; |
| 19 | +use Ymir\Cli\Project\Type\LaravelProjectType; |
| 20 | +use Ymir\Cli\Resource\Definition\EnvironmentDefinition; |
| 21 | +use Ymir\Cli\Resource\Model\Environment; |
| 22 | +use Ymir\Cli\Resource\Model\Project; |
| 23 | +use Ymir\Cli\Resource\ResourceCollection; |
| 24 | +use Ymir\Cli\Tests\Factory\EnvironmentFactory; |
| 25 | +use Ymir\Cli\Tests\Integration\Command\TestCase; |
| 26 | + |
| 27 | +class ArtisanCommandTest extends TestCase |
| 28 | +{ |
| 29 | + public function testPerformExecutesArtisanCommand(): void |
| 30 | + { |
| 31 | + $this->setupActiveTeam(); |
| 32 | + $project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class); |
| 33 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 34 | + |
| 35 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 36 | + $this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate:status'])->andReturn(collect(['id' => 123])); |
| 37 | + $this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([ |
| 38 | + 'status' => 'completed', |
| 39 | + 'result' => [ |
| 40 | + 'exitCode' => 0, |
| 41 | + 'output' => 'artisan output', |
| 42 | + ], |
| 43 | + ])); |
| 44 | + |
| 45 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 46 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 47 | + ]))]); |
| 48 | + |
| 49 | + $tester = $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['migrate:status'], '--environment' => 'production']); |
| 50 | + |
| 51 | + $this->assertStringContainsString('Running "php artisan migrate:status" on "production" environment', $tester->getDisplay()); |
| 52 | + $this->assertStringContainsString('artisan output', $tester->getDisplay()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testPerformExecutesArtisanCommandAsynchronously(): void |
| 56 | + { |
| 57 | + $this->setupActiveTeam(); |
| 58 | + $project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class); |
| 59 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 60 | + |
| 61 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 62 | + $this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan cache:clear'])->andReturn(collect(['id' => 123])); |
| 63 | + |
| 64 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 65 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 66 | + ]))]); |
| 67 | + |
| 68 | + $tester = $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['cache:clear'], '--environment' => 'production', '--async' => true]); |
| 69 | + |
| 70 | + $this->assertStringContainsString('Running "php artisan cache:clear" asynchronously on "production" environment', $tester->getDisplay()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testPerformExecutesArtisanCommandInteractively(): void |
| 74 | + { |
| 75 | + $this->setupActiveTeam(); |
| 76 | + $project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class); |
| 77 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 78 | + |
| 79 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 80 | + $this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan route:list'])->andReturn(collect(['id' => 123])); |
| 81 | + $this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([ |
| 82 | + 'status' => 'completed', |
| 83 | + 'result' => [ |
| 84 | + 'exitCode' => 0, |
| 85 | + 'output' => 'route list output', |
| 86 | + ], |
| 87 | + ])); |
| 88 | + |
| 89 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 90 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 91 | + ]))]); |
| 92 | + |
| 93 | + $tester = $this->executeCommand(ArtisanCommand::NAME, [], ['production', 'route:list']); |
| 94 | + |
| 95 | + $this->assertStringContainsString('Running "php artisan route:list" on "production" environment', $tester->getDisplay()); |
| 96 | + $this->assertStringContainsString('route list output', $tester->getDisplay()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testPerformRemovesPhpArtisanPrefixFromCommand(): void |
| 100 | + { |
| 101 | + $this->setupActiveTeam(); |
| 102 | + $project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class); |
| 103 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 104 | + |
| 105 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 106 | + $this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate'])->andReturn(collect(['id' => 123])); |
| 107 | + $this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([ |
| 108 | + 'status' => 'completed', |
| 109 | + 'result' => [ |
| 110 | + 'exitCode' => 0, |
| 111 | + 'output' => 'migrate output', |
| 112 | + ], |
| 113 | + ])); |
| 114 | + |
| 115 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 116 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 117 | + ]))]); |
| 118 | + |
| 119 | + $tester = $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['php', 'artisan', 'migrate'], '--environment' => 'production']); |
| 120 | + |
| 121 | + $this->assertStringContainsString('Running "php artisan migrate" on "production" environment', $tester->getDisplay()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testPerformThrowsExceptionIfCommandIsTinker(): void |
| 125 | + { |
| 126 | + $this->expectException(InvalidInputException::class); |
| 127 | + $this->expectExceptionMessage('The "artisan tinker" command isn\'t available remotely'); |
| 128 | + |
| 129 | + $this->setupActiveTeam(); |
| 130 | + $project = $this->setupValidProject(1, 'project', [], 'laravel', LaravelProjectType::class); |
| 131 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 132 | + |
| 133 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 134 | + |
| 135 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 136 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 137 | + ]))]); |
| 138 | + |
| 139 | + $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['tinker'], '--environment' => 'production']); |
| 140 | + } |
| 141 | + |
| 142 | + public function testPerformThrowsExceptionIfCommandIsTinkerWithArguments(): void |
| 143 | + { |
| 144 | + $this->expectException(InvalidInputException::class); |
| 145 | + $this->expectExceptionMessage('The "artisan tinker --execute="dump(1);"" command isn\'t available remotely'); |
| 146 | + |
| 147 | + $this->setupActiveTeam(); |
| 148 | + $project = $this->setupValidProject(1, 'project', [], 'laravel', LaravelProjectType::class); |
| 149 | + $environment = EnvironmentFactory::create(['name' => 'production']); |
| 150 | + |
| 151 | + $this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment])); |
| 152 | + |
| 153 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([ |
| 154 | + Environment::class => function () { return new EnvironmentDefinition(); }, |
| 155 | + ]))]); |
| 156 | + |
| 157 | + $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['tinker', '--execute="dump(1);"'], '--environment' => 'production']); |
| 158 | + } |
| 159 | + |
| 160 | + public function testPerformThrowsExceptionIfProjectIsNotLaravel(): void |
| 161 | + { |
| 162 | + $this->expectException(UnsupportedProjectException::class); |
| 163 | + $this->expectExceptionMessage('You can only use this command with Laravel projects'); |
| 164 | + |
| 165 | + $this->setupValidProject(1, 'project', [], 'wordpress'); |
| 166 | + |
| 167 | + $this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory())]); |
| 168 | + |
| 169 | + $this->executeCommand(ArtisanCommand::NAME); |
| 170 | + } |
| 171 | +} |
0 commit comments