|
2 | 2 |
|
3 | 3 | namespace Tests\Feature\Console; |
4 | 4 |
|
5 | | -use Illuminate\Process\Exceptions\ProcessTimedOutException; |
| 5 | +use Illuminate\Http\Client\Request; |
6 | 6 | use Illuminate\Support\Facades\Http; |
7 | | -use Illuminate\Support\Facades\Process; |
8 | | -use RuntimeException; |
| 7 | +use Orchestra\Testbench\Attributes\WithEnv; |
9 | 8 | use Tests\TestCase; |
10 | 9 |
|
11 | | -use function sleep; |
| 10 | +use function now; |
12 | 11 |
|
13 | 12 | class DeployCommandTest extends TestCase |
14 | 13 | { |
| 14 | + #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] |
| 15 | + #[WithEnv('NIGHTWATCH_DEPLOY', 'v1.2.3')] |
15 | 16 | public function test_it_can_run_the_deploy_command(): void |
16 | 17 | { |
17 | | - $output = ''; |
18 | | - $process = Process::timeout(10)->start('NIGHTWATCH_DEPLOY="v1.2.3" \ |
19 | | - vendor/bin/testbench nightwatch:deploy' |
20 | | - ); |
21 | | - |
22 | | - try { |
23 | | - $result = $process->wait(function ($type, $o) use (&$output, $process) { |
24 | | - $output .= $o; |
25 | | - |
26 | | - $process->signal(SIGTERM); |
27 | | - |
28 | | - $tries = 0; |
29 | | - |
30 | | - while ($tries < 3) { |
31 | | - if (! $process->running()) { |
32 | | - return; |
33 | | - } |
| 18 | + $this->freezeTime(); |
| 19 | + Http::fake([ |
| 20 | + '*/api/deployments' => function (Request $request) { |
| 21 | + $this->assertEquals(['Bearer test-token'], $request->header('Authorization')); |
| 22 | + $this->assertEquals([ |
| 23 | + 'timestamp' => now()->getTimestamp(), |
| 24 | + 'version' => 'v1.2.3', |
| 25 | + ], $request->data()); |
| 26 | + |
| 27 | + return Http::response('OK'); |
| 28 | + }, |
| 29 | + ]); |
34 | 30 |
|
35 | | - $tries++; |
36 | | - sleep(1); |
37 | | - } |
| 31 | + $this->artisan('nightwatch:deploy') |
| 32 | + ->expectsOutput('Deployment successful') |
| 33 | + ->assertExitCode(0); |
| 34 | + } |
38 | 35 |
|
39 | | - $process->signal(SIGKILL); |
40 | | - }); |
41 | | - } catch (ProcessTimedOutException $e) { |
42 | | - throw new RuntimeException('Failed to deploy or stop the agent running. Output:'.PHP_EOL.$output, previous: $e); |
43 | | - } |
| 36 | + #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] |
| 37 | + public function test_it_can_run_the_deploy_command_without_a_version(): void |
| 38 | + { |
| 39 | + $this->freezeTime(); |
| 40 | + Http::fake([ |
| 41 | + '*/api/deployments' => function (Request $request) { |
| 42 | + $this->assertEquals(['Bearer test-token'], $request->header('Authorization')); |
| 43 | + $this->assertEquals([ |
| 44 | + 'timestamp' => now()->getTimestamp(), |
| 45 | + 'version' => '', |
| 46 | + ], $request->data()); |
| 47 | + |
| 48 | + return Http::response('OK'); |
| 49 | + }, |
| 50 | + ]); |
44 | 51 |
|
45 | | - $this->assertStringContainsString('Deployment successful', $output); |
| 52 | + $this->artisan('nightwatch:deploy') |
| 53 | + ->expectsOutput('Deployment successful') |
| 54 | + ->assertExitCode(0); |
46 | 55 | } |
47 | 56 |
|
| 57 | + #[WithEnv('NIGHTWATCH_TOKEN', '')] |
48 | 58 | public function test_it_fails_when_the_deploy_command_is_run_without_a_token(): void |
49 | 59 | { |
50 | | - $process = Process::timeout(10)->start('NIGHTWATCH_DEPLOY="v1.2.3" \ |
51 | | - NIGHTWATCH_TOKEN="" \ |
52 | | - vendor/bin/testbench nightwatch:deploy'); |
53 | | - |
54 | | - try { |
55 | | - $process->wait(function ($type, $o) use (&$output, $process) { |
56 | | - $output .= $o; |
57 | | - |
58 | | - $process->signal(SIGTERM); |
59 | | - |
60 | | - $tries = 0; |
61 | | - |
62 | | - while ($tries < 3) { |
63 | | - if (! $process->running()) { |
64 | | - return; |
65 | | - } |
66 | | - |
67 | | - $tries++; |
68 | | - sleep(1); |
69 | | - } |
70 | | - |
71 | | - $process->signal(SIGKILL); |
72 | | - }); |
73 | | - } catch (ProcessTimedOutException $e) { |
74 | | - throw new RuntimeException('Failed to deploy or stop the agent running. Output:'.PHP_EOL.$output, previous: $e); |
75 | | - } |
76 | | - |
77 | | - $this->assertStringContainsString('No NIGHTWATCH_TOKEN environment variable configured.', $process->output()); |
| 60 | + $this->artisan('nightwatch:deploy') |
| 61 | + ->expectsOutput('No NIGHTWATCH_TOKEN environment variable configured.') |
| 62 | + ->assertExitCode(1); |
78 | 63 | } |
79 | 64 |
|
| 65 | + #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] |
80 | 66 | public function test_it_handles_http_errors(): void |
81 | 67 | { |
82 | 68 | Http::fake([ |
83 | | - $_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::response('Whoops!', 500), |
| 69 | + '*/api/deployments' => Http::response('Whoops!', 500), |
84 | 70 | ]); |
85 | 71 |
|
86 | 72 | $this->artisan('nightwatch:deploy') |
87 | 73 | ->expectsOutput('Deployment failed: 500 [Whoops!]') |
88 | 74 | ->assertExitCode(1); |
89 | 75 | } |
90 | 76 |
|
91 | | - public function test_it_handles_throwable_errors(): void |
| 77 | + #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] |
| 78 | + public function test_it_handles_connection_errors(): void |
92 | 79 | { |
93 | 80 | Http::fake([ |
94 | | - $_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::failedConnection('Whoops!'), |
| 81 | + '*/api/deployments' => Http::failedConnection('Whoops!'), |
95 | 82 | ]); |
96 | 83 |
|
97 | 84 | $this->artisan('nightwatch:deploy') |
|
0 commit comments