-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExitCodePolicyTest.php
More file actions
151 lines (126 loc) · 5.79 KB
/
ExitCodePolicyTest.php
File metadata and controls
151 lines (126 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
namespace Tests\Commands;
use DurableWorkflow\Cli\Commands\BaseCommand;
use DurableWorkflow\Cli\Support\ExitCode;
use DurableWorkflow\Cli\Support\NetworkException;
use DurableWorkflow\Cli\Support\ServerClient;
use DurableWorkflow\Cli\Support\ServerHttpException;
use DurableWorkflow\Cli\Support\TimeoutException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class ExitCodePolicyTest extends TestCase
{
/**
* @dataProvider httpStatusCases
*/
public function test_http_status_maps_to_expected_exit_code(int $status, int $expected): void
{
self::assertSame($expected, ExitCode::fromHttpStatus($status));
}
/**
* @return iterable<string, array{int, int}>
*/
public static function httpStatusCases(): iterable
{
yield '401 unauthorized → AUTH' => [401, ExitCode::AUTH];
yield '403 forbidden → AUTH' => [403, ExitCode::AUTH];
yield '404 not found → NOT_FOUND' => [404, ExitCode::NOT_FOUND];
yield '408 request timeout → TIMEOUT' => [408, ExitCode::TIMEOUT];
yield '400 bad request → INVALID' => [400, ExitCode::INVALID];
yield '422 unprocessable → INVALID' => [422, ExitCode::INVALID];
yield '500 server error → SERVER' => [500, ExitCode::SERVER];
yield '503 service unavailable → SERVER' => [503, ExitCode::SERVER];
yield '200 ok → FAILURE (non-error status)' => [200, ExitCode::FAILURE];
}
public function test_base_command_translates_network_exception_to_network_exit_code(): void
{
$command = new ThrowingBaseCommand(new NetworkException('Connection refused'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::NETWORK, $tester->execute([]));
self::assertStringContainsString('Connection refused', $tester->getDisplay());
self::assertStringContainsString('Next steps:', $tester->getDisplay());
self::assertStringContainsString('dw doctor --output=json', $tester->getDisplay());
}
public function test_base_command_translates_timeout_exception_to_timeout_exit_code(): void
{
$command = new ThrowingBaseCommand(new TimeoutException('deadline exceeded'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::TIMEOUT, $tester->execute([]));
self::assertStringContainsString('deadline exceeded', $tester->getDisplay());
}
public function test_base_command_translates_auth_http_status_to_auth_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('Unauthorized', 401));
$tester = new CommandTester($command);
self::assertSame(ExitCode::AUTH, $tester->execute([]));
self::assertStringContainsString('Unauthorized', $tester->getDisplay());
self::assertStringContainsString('Check the selected environment, auth token source, and namespace permissions.', $tester->getDisplay());
}
public function test_base_command_translates_not_found_http_status_to_not_found_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('Workflow not found', 404));
$tester = new CommandTester($command);
self::assertSame(ExitCode::NOT_FOUND, $tester->execute([]));
self::assertStringContainsString('dw throwing --help', $tester->getDisplay());
}
public function test_base_command_translates_server_http_status_to_server_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('internal', 502));
$tester = new CommandTester($command);
self::assertSame(ExitCode::SERVER, $tester->execute([]));
self::assertStringContainsString('dw server:health --output=json', $tester->getDisplay());
}
public function test_base_command_translates_validation_http_status_to_invalid_exit_code(): void
{
$command = new ThrowingBaseCommand(new ServerHttpException('bad input', 422));
$tester = new CommandTester($command);
self::assertSame(ExitCode::INVALID, $tester->execute([]));
self::assertStringContainsString('Inspect the request options', $tester->getDisplay());
}
public function test_base_command_translates_unexpected_throwable_to_failure_exit_code(): void
{
$command = new ThrowingBaseCommand(new \RuntimeException('boom'));
$tester = new CommandTester($command);
self::assertSame(ExitCode::FAILURE, $tester->execute([]));
self::assertStringContainsString('boom', $tester->getDisplay());
}
public function test_exit_codes_remain_distinct(): void
{
$values = [
ExitCode::SUCCESS,
ExitCode::FAILURE,
ExitCode::INVALID,
ExitCode::NETWORK,
ExitCode::AUTH,
ExitCode::NOT_FOUND,
ExitCode::SERVER,
ExitCode::TIMEOUT,
];
self::assertCount(count($values), array_unique($values), 'exit codes must remain distinct');
}
public function test_symfony_canonical_codes_preserved(): void
{
self::assertSame(Command::SUCCESS, ExitCode::SUCCESS);
self::assertSame(Command::FAILURE, ExitCode::FAILURE);
self::assertSame(Command::INVALID, ExitCode::INVALID);
}
}
class ThrowingBaseCommand extends BaseCommand
{
public function __construct(private readonly \Throwable $toThrow)
{
parent::__construct('throwing');
}
protected function client(InputInterface $input): ServerClient
{
throw new \LogicException('not used');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
throw $this->toThrow;
}
}