|
8 | 8 | use PHPUnit\Framework\Attributes\CoversFunction; |
9 | 9 | use PHPUnit\Framework\Attributes\DataProvider; |
10 | 10 | use PHPUnit\Framework\Attributes\Group; |
| 11 | +use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Tests for output formatter functions. |
14 | 15 | * |
15 | 16 | * @phpcs:disable Drupal.Classes.FullyQualifiedNamespace.UseStatementMissing |
16 | 17 | */ |
| 18 | +#[RunTestsInSeparateProcesses] |
17 | 19 | #[CoversFunction('DrevOps\VortexTooling\note')] |
18 | 20 | #[CoversFunction('DrevOps\VortexTooling\task')] |
19 | 21 | #[CoversFunction('DrevOps\VortexTooling\info')] |
|
24 | 26 | #[Group('helpers')] |
25 | 27 | class FormatterTest extends UnitTestCase { |
26 | 28 |
|
27 | | - protected function setUp(): void { |
28 | | - parent::setUp(); |
| 29 | + #[DataProvider('dataProviderOutputFormatters')] |
| 30 | + public function testOutputFormatters(string $function, ?bool $is_tty, string $expected_output): void { |
| 31 | + // Mock posix_isatty() BEFORE loading helpers.php. |
| 32 | + // Only mock for functions that use term_supports_color (not 'note'). |
| 33 | + if ($is_tty !== NULL) { |
| 34 | + // Set TERM to a valid terminal type (not 'dumb') so color check proceeds. |
| 35 | + $this->envSet('TERM', 'xterm-256color'); |
| 36 | + $this->mockPosixIsatty($is_tty); |
| 37 | + } |
29 | 38 |
|
30 | 39 | require_once __DIR__ . '/../../src/helpers.php'; |
31 | | - } |
32 | | - |
33 | | - #[DataProvider('dataProviderOutputFormatters')] |
34 | | - public function testOutputFormatters(string $function, string $expected_prefix): void { |
35 | | - putenv('TERM=dumb'); |
36 | 40 |
|
37 | 41 | ob_start(); |
38 | 42 | $callable = 'DrevOps\\VortexTooling\\' . $function; |
39 | 43 | // @phpstan-ignore-next-line argument.type |
40 | 44 | call_user_func($callable, 'Test message %s', 'arg'); |
41 | 45 | $output = ob_get_clean(); |
42 | | - $this->assertIsString($output); |
43 | | - |
44 | | - if ($function === 'note') { |
45 | | - $this->assertEquals(" Test message arg\n", $output); |
46 | | - } |
47 | | - else { |
48 | | - $this->assertEquals($expected_prefix . "Test message arg\n", $output); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - public static function dataProviderOutputFormatters(): array { |
53 | | - return [ |
54 | | - 'note' => ['note', ' '], |
55 | | - 'task' => ['task', '[TASK] '], |
56 | | - 'info' => ['info', '[INFO] '], |
57 | | - 'pass' => ['pass', '[ OK ] '], |
58 | | - 'fail_no_exit' => ['fail_no_exit', '[FAIL] '], |
59 | | - ]; |
60 | | - } |
61 | 46 |
|
62 | | - #[DataProvider('dataProviderOutputFormattersWithColor')] |
63 | | - public function testOutputFormattersWithColor(string $function, string $expected_prefix): void { |
64 | | - putenv('TERM=xterm-256color'); |
65 | | - |
66 | | - ob_start(); |
67 | | - $callable = 'DrevOps\\VortexTooling\\' . $function; |
68 | | - // @phpstan-ignore-next-line argument.type |
69 | | - call_user_func($callable, 'Test'); |
70 | | - $output = ob_get_clean(); |
71 | 47 | $this->assertIsString($output); |
72 | | - |
73 | | - $this->assertStringContainsString($expected_prefix, $output); |
74 | | - $this->assertStringContainsString('Test', $output); |
| 48 | + $this->assertEquals($expected_output, $output); |
75 | 49 | } |
76 | 50 |
|
77 | | - public static function dataProviderOutputFormattersWithColor(): array { |
| 51 | + public static function dataProviderOutputFormatters(): array { |
78 | 52 | return [ |
79 | | - 'task' => ['task', '[TASK]'], |
80 | | - 'info' => ['info', '[INFO]'], |
81 | | - 'pass' => ['pass', '[ OK ]'], |
| 53 | + // Note - does not use term_supports_color, always plain output. |
| 54 | + 'note' => ['note', NULL, " Test message arg\n"], |
| 55 | + // Task - blue (34m). |
| 56 | + 'task, no color' => ['task', FALSE, "[TASK] Test message arg\n"], |
| 57 | + 'task, with color' => ['task', TRUE, "\033[34m[TASK] Test message arg\033[0m\n"], |
| 58 | + // Info - cyan (36m). |
| 59 | + 'info, no color' => ['info', FALSE, "[INFO] Test message arg\n"], |
| 60 | + 'info, with color' => ['info', TRUE, "\033[36m[INFO] Test message arg\033[0m\n"], |
| 61 | + // Pass - green (32m). |
| 62 | + 'pass, no color' => ['pass', FALSE, "[ OK ] Test message arg\n"], |
| 63 | + 'pass, with color' => ['pass', TRUE, "\033[32m[ OK ] Test message arg\033[0m\n"], |
| 64 | + // Fail_no_exit - red (31m). |
| 65 | + 'fail_no_exit, no color' => ['fail_no_exit', FALSE, "[FAIL] Test message arg\n"], |
| 66 | + 'fail_no_exit, with color' => ['fail_no_exit', TRUE, "\033[31m[FAIL] Test message arg\033[0m\n"], |
82 | 67 | ]; |
83 | 68 | } |
84 | 69 |
|
85 | | - public function testFail(): void { |
| 70 | + #[DataProvider('dataProviderFail')] |
| 71 | + public function testFail(bool $is_tty, string $expected_output): void { |
| 72 | + // Set TERM to a valid terminal type and mock posix_isatty BEFORE loading. |
| 73 | + $this->envSet('TERM', 'xterm-256color'); |
| 74 | + $this->mockPosixIsatty($is_tty); |
86 | 75 | $this->mockQuit(1); |
87 | 76 |
|
| 77 | + require_once __DIR__ . '/../../src/helpers.php'; |
| 78 | + |
88 | 79 | $this->expectException(QuitErrorException::class); |
89 | 80 | $this->expectExceptionCode(1); |
90 | 81 |
|
91 | 82 | try { |
92 | 83 | ob_start(); |
93 | | - \DrevOps\VortexTooling\fail('Test failure'); |
| 84 | + \DrevOps\VortexTooling\fail('Test failure %s', 'message'); |
94 | 85 | } |
95 | 86 | finally { |
96 | | - ob_end_clean(); |
| 87 | + $output = ob_get_clean(); |
| 88 | + $this->assertEquals($expected_output, $output); |
97 | 89 | } |
98 | 90 | } |
99 | 91 |
|
| 92 | + public static function dataProviderFail(): array { |
| 93 | + return [ |
| 94 | + 'no color' => [FALSE, "[FAIL] Test failure message\n"], |
| 95 | + 'with color' => [TRUE, "\033[31m[FAIL] Test failure message\033[0m\n"], |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
100 | 99 | #[DataProvider('dataProviderTermSupportsColor')] |
101 | 100 | public function testTermSupportsColor(string|bool $term_value, bool $expected): void { |
| 101 | + require_once __DIR__ . '/../../src/helpers.php'; |
| 102 | + |
102 | 103 | if ($term_value === FALSE) { |
103 | | - putenv('TERM'); |
| 104 | + $this->envUnset('TERM'); |
104 | 105 | } |
105 | 106 | else { |
106 | | - putenv('TERM=' . $term_value); |
| 107 | + $this->envSet('TERM', (string) $term_value); |
107 | 108 | } |
108 | 109 |
|
109 | 110 | $result = \DrevOps\VortexTooling\term_supports_color(); |
|
0 commit comments