Skip to content

Commit 6473614

Browse files
committed
Update command
1 parent c1330b5 commit 6473614

File tree

2 files changed

+60
-78
lines changed

2 files changed

+60
-78
lines changed

src/Console/DeployCommand.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
use Carbon\CarbonImmutable;
66
use Illuminate\Console\Command;
7+
use Illuminate\Http\Client\RequestException;
78
use Illuminate\Support\Facades\Http;
9+
use Illuminate\Support\Str;
810
use SensitiveParameter;
911
use Symfony\Component\Console\Attribute\AsCommand;
1012
use Throwable;
1113

1214
use function config;
13-
use function strlen;
14-
use function substr;
1515

1616
/**
1717
* @internal
@@ -50,10 +50,10 @@ public function handle(): int
5050

5151
$tag = config('nightwatch.deployment') ?? '';
5252

53-
$baseUrl = $_SERVER['NIGHTWATCH_BASE_URL'] ?? 'https://nightwatch.laravel.com';
53+
$baseUrl = ! empty($_SERVER['NIGHTWATCH_BASE_URL']) ? $_SERVER['NIGHTWATCH_BASE_URL'] : 'https://nightwatch.laravel.com';
5454

5555
try {
56-
$response = Http::connectTimeout(5)
56+
Http::connectTimeout(5)
5757
->timeout(10)
5858
->withHeaders([
5959
'Authorization' => "Bearer {$this->token}",
@@ -62,23 +62,18 @@ public function handle(): int
6262
->post("{$baseUrl}/api/deployments", [
6363
'timestamp' => CarbonImmutable::now()->timestamp,
6464
'version' => $tag,
65-
]);
66-
67-
if ($response->successful()) {
68-
$this->info('Deployment successful');
65+
])
66+
->throw();
6967

70-
return 0;
71-
} else {
72-
$message = $response->body();
68+
$this->info('Deployment successful');
7369

74-
if (strlen($message) > 1005) {
75-
$message = substr($message, 0, 1000).'[...]';
76-
}
70+
return 0;
71+
} catch (RequestException $e) {
72+
$message = Str::limit($e->response->body(), 1000, '[...]');
7773

78-
$this->error("Deployment failed: {$response->status()} [{$message}]");
74+
$this->error("Deployment failed: {$e->getCode()} [{$message}]");
7975

80-
return 1;
81-
}
76+
return 1;
8277
} catch (Throwable $e) {
8378
$this->error("Deployment failed: [{$e->getMessage()}]");
8479

tests/Feature/Console/DeployCommandTest.php

Lines changed: 48 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,83 @@
22

33
namespace Tests\Feature\Console;
44

5-
use Illuminate\Process\Exceptions\ProcessTimedOutException;
5+
use Illuminate\Http\Client\Request;
66
use Illuminate\Support\Facades\Http;
7-
use Illuminate\Support\Facades\Process;
8-
use RuntimeException;
7+
use Orchestra\Testbench\Attributes\WithEnv;
98
use Tests\TestCase;
109

11-
use function sleep;
10+
use function now;
1211

1312
class DeployCommandTest extends TestCase
1413
{
14+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
15+
#[WithEnv('NIGHTWATCH_DEPLOY', 'v1.2.3')]
1516
public function test_it_can_run_the_deploy_command(): void
1617
{
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+
]);
3430

35-
$tries++;
36-
sleep(1);
37-
}
31+
$this->artisan('nightwatch:deploy')
32+
->expectsOutput('Deployment successful')
33+
->assertExitCode(0);
34+
}
3835

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+
]);
4451

45-
$this->assertStringContainsString('Deployment successful', $output);
52+
$this->artisan('nightwatch:deploy')
53+
->expectsOutput('Deployment successful')
54+
->assertExitCode(0);
4655
}
4756

57+
#[WithEnv('NIGHTWATCH_TOKEN', '')]
4858
public function test_it_fails_when_the_deploy_command_is_run_without_a_token(): void
4959
{
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);
7863
}
7964

65+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
8066
public function test_it_handles_http_errors(): void
8167
{
8268
Http::fake([
83-
$_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::response('Whoops!', 500),
69+
'*/api/deployments' => Http::response('Whoops!', 500),
8470
]);
8571

8672
$this->artisan('nightwatch:deploy')
8773
->expectsOutput('Deployment failed: 500 [Whoops!]')
8874
->assertExitCode(1);
8975
}
9076

91-
public function test_it_handles_throwable_errors(): void
77+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
78+
public function test_it_handles_connection_errors(): void
9279
{
9380
Http::fake([
94-
$_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::failedConnection('Whoops!'),
81+
'*/api/deployments' => Http::failedConnection('Whoops!'),
9582
]);
9683

9784
$this->artisan('nightwatch:deploy')

0 commit comments

Comments
 (0)