Skip to content

Commit a90b76c

Browse files
committed
Update command
1 parent c1330b5 commit a90b76c

File tree

2 files changed

+61
-78
lines changed

2 files changed

+61
-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: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,84 @@
22

33
namespace Tests\Feature\Console;
44

5-
use Illuminate\Process\Exceptions\ProcessTimedOutException;
5+
use Illuminate\Http\Client\ConnectionException;
6+
use Illuminate\Http\Client\Request;
67
use Illuminate\Support\Facades\Http;
7-
use Illuminate\Support\Facades\Process;
8-
use RuntimeException;
8+
use Orchestra\Testbench\Attributes\WithEnv;
99
use Tests\TestCase;
1010

11-
use function sleep;
11+
use function now;
1212

1313
class DeployCommandTest extends TestCase
1414
{
15+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
16+
#[WithEnv('NIGHTWATCH_DEPLOY', 'v1.2.3')]
1517
public function test_it_can_run_the_deploy_command(): void
1618
{
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-
}
19+
$this->freezeTime();
20+
Http::fake([
21+
'*/api/deployments' => function (Request $request) {
22+
$this->assertEquals(['Bearer test-token'], $request->header('Authorization'));
23+
$this->assertEquals([
24+
'timestamp' => now()->getTimestamp(),
25+
'version' => 'v1.2.3',
26+
], $request->data());
27+
28+
return Http::response('OK');
29+
},
30+
]);
3431

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

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-
}
37+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
38+
public function test_it_can_run_the_deploy_command_without_a_version(): void
39+
{
40+
$this->freezeTime();
41+
Http::fake([
42+
'*/api/deployments' => function (Request $request) {
43+
$this->assertEquals(['Bearer test-token'], $request->header('Authorization'));
44+
$this->assertEquals([
45+
'timestamp' => now()->getTimestamp(),
46+
'version' => '',
47+
], $request->data());
48+
49+
return Http::response('OK');
50+
},
51+
]);
4452

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

58+
#[WithEnv('NIGHTWATCH_TOKEN', '')]
4859
public function test_it_fails_when_the_deploy_command_is_run_without_a_token(): void
4960
{
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());
61+
$this->artisan('nightwatch:deploy')
62+
->expectsOutput('No NIGHTWATCH_TOKEN environment variable configured.')
63+
->assertExitCode(1);
7864
}
7965

66+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
8067
public function test_it_handles_http_errors(): void
8168
{
8269
Http::fake([
83-
$_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::response('Whoops!', 500),
70+
'*/api/deployments' => Http::response('Whoops!', 500),
8471
]);
8572

8673
$this->artisan('nightwatch:deploy')
8774
->expectsOutput('Deployment failed: 500 [Whoops!]')
8875
->assertExitCode(1);
8976
}
9077

91-
public function test_it_handles_throwable_errors(): void
78+
#[WithEnv('NIGHTWATCH_TOKEN', 'test-token')]
79+
public function test_it_handles_connection_errors(): void
9280
{
9381
Http::fake([
94-
$_SERVER['NIGHTWATCH_BASE_URL'].'/api/deployments' => Http::failedConnection('Whoops!'),
82+
'*/api/deployments' => fn () => throw new ConnectionException('Whoops!'),
9583
]);
9684

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

0 commit comments

Comments
 (0)