Skip to content

Commit 98b7922

Browse files
committed
fix: prevent test timeouts by improving non-interactive mode checks
- Add PHPUNIT environment variable check before stream_isatty() in Dashboard, InputHandler - Fix test output capture by temporarily unsetting PHPUNIT in tests that check CLI output - Update test expectations for error messages in non-interactive mode - Remove unnecessary @runInSeparateProcess from tests using Application->run() directly - Add helper methods unsetPhpunitForOutput() and restorePhpunit() for cleaner test code
1 parent 47f10c8 commit 98b7922

File tree

8 files changed

+187
-46
lines changed

8 files changed

+187
-46
lines changed

src/cli/commands/TableCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ protected function create(): int
100100
$columnsOpt = $this->getOption('columns');
101101
if (!is_string($columnsOpt) || $columnsOpt === '') {
102102
// Try interactive prompt for columns definition
103-
$columnsOpt = static::readInput('Enter columns (e.g., id:int, name:string:nullable)', null);
103+
$inputColumns = static::readInput('Enter columns (e.g., id:int, name:string:nullable)', null);
104+
if ($inputColumns === '') {
105+
return $this->showError('Columns are required. Use --columns option or provide them interactively.');
106+
}
107+
$columnsOpt = $inputColumns;
104108
}
105109
$options = $this->collectCreateOptions();
106-
/** @var string $columnsOpt */
107-
$columns = $this->parseColumns($columnsOpt !== '' ? $columnsOpt : null);
110+
// At this point $columnsOpt is guaranteed to be a non-empty string
111+
$columns = $this->parseColumns($columnsOpt);
108112
if (empty($columns)) {
109113
return $this->showError('At least one column is required. Use --columns="id:int, name:string".');
110114
}

src/cli/ui/Dashboard.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,16 @@ public function __construct(PdoDb $db)
328328
public function run(): void
329329
{
330330
// Check if interactive
331-
if (!stream_isatty(STDOUT) || getenv('PDODB_NON_INTERACTIVE') !== false) {
331+
// Check for PDODB_NON_INTERACTIVE and PHPUNIT first (fastest check)
332+
$nonInteractive = getenv('PDODB_NON_INTERACTIVE') !== false
333+
|| getenv('PHPUNIT') !== false;
334+
335+
// Only check stream_isatty if not already in non-interactive mode (expensive check)
336+
if (!$nonInteractive) {
337+
$nonInteractive = !stream_isatty(STDOUT);
338+
}
339+
340+
if ($nonInteractive) {
332341
echo "TUI Dashboard requires an interactive terminal.\n";
333342
echo "Please run this command in a terminal with TTY support.\n";
334343
return;

src/cli/ui/InputHandler.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ class InputHandler
1818
*/
1919
public static function readKey(int $timeoutMicroseconds = 100000): ?string
2020
{
21-
// Check if stdin is available
22-
if (!stream_isatty(STDIN)) {
21+
// Check for non-interactive mode first (fastest check)
22+
$nonInteractive = getenv('PDODB_NON_INTERACTIVE') !== false
23+
|| getenv('PHPUNIT') !== false;
24+
25+
// Only check stream_isatty if not already in non-interactive mode (expensive check)
26+
if (!$nonInteractive) {
27+
$nonInteractive = !stream_isatty(STDIN);
28+
}
29+
30+
if ($nonInteractive) {
2331
return null;
2432
}
2533

@@ -257,7 +265,16 @@ public static function waitForKey(array $allowedKeys = []): string
257265
*/
258266
public static function hasKey(): bool
259267
{
260-
if (!stream_isatty(STDIN)) {
268+
// Check for non-interactive mode first (fastest check)
269+
$nonInteractive = getenv('PDODB_NON_INTERACTIVE') !== false
270+
|| getenv('PHPUNIT') !== false;
271+
272+
// Only check stream_isatty if not already in non-interactive mode (expensive check)
273+
if (!$nonInteractive) {
274+
$nonInteractive = !stream_isatty(STDIN);
275+
}
276+
277+
if ($nonInteractive) {
261278
return false;
262279
}
263280

tests/mariadb/UserCommandCliTests.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
namespace tommyknocker\pdodb\tests\mariadb;
66

7+
use PHPUnit\Framework\Attributes\PreserveGlobalState;
8+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
79
use tommyknocker\pdodb\cli\Application;
810
use tommyknocker\pdodb\exceptions\AuthenticationException;
911

1012
final class UserCommandCliTests extends BaseMariaDBTestCase
1113
{
12-
/**
13-
* @runInSeparateProcess
14-
*
15-
* @preserveGlobalState disabled
16-
*/
14+
#[PreserveGlobalState(false)]
15+
#[RunInSeparateProcess]
1716
public function testUserCreateCommandWithAllOptions(): void
1817
{
1918
putenv('PDODB_DRIVER=mariadb');

tests/mysql/UserCommandCliTests.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
namespace tommyknocker\pdodb\tests\mysql;
66

7+
use PHPUnit\Framework\Attributes\PreserveGlobalState;
8+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
79
use tommyknocker\pdodb\cli\Application;
810
use tommyknocker\pdodb\exceptions\AuthenticationException;
911

1012
final class UserCommandCliTests extends BaseMySQLTestCase
1113
{
12-
/**
13-
* @runInSeparateProcess
14-
*
15-
* @preserveGlobalState disabled
16-
*/
14+
#[PreserveGlobalState(false)]
15+
#[RunInSeparateProcess]
1716
public function testUserCreateCommandWithAllOptions(): void
1817
{
1918
putenv('PDODB_DRIVER=mysql');

tests/postgresql/UserCommandCliTests.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
namespace tommyknocker\pdodb\tests\postgresql;
66

7+
use PHPUnit\Framework\Attributes\PreserveGlobalState;
8+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
79
use tommyknocker\pdodb\cli\Application;
810
use tommyknocker\pdodb\exceptions\AuthenticationException;
911

1012
final class UserCommandCliTests extends BasePostgreSQLTestCase
1113
{
12-
/**
13-
* @runInSeparateProcess
14-
*
15-
* @preserveGlobalState disabled
16-
*/
14+
#[PreserveGlobalState(false)]
15+
#[RunInSeparateProcess]
1716
public function testUserCreateCommandWithAllOptions(): void
1817
{
1918
putenv('PDODB_DRIVER=pgsql');

0 commit comments

Comments
 (0)