Skip to content

Commit 3851fb3

Browse files
committed
test: add comprehensive PHP version detection tests
Add 13 new black-box tests covering all PHP version detection paths: set/reset, staged versions, auto-detection from composer.json and slic.json, CLI override, .env.slic.local override, priority ordering, version normalization, and invalid format rejection. Move shared helpers (dockerMockEnv, setUpPluginsDir) to BaseTestCase and add tearDown stack cleanup to prevent orphaned registry entries.
1 parent a3a9153 commit 3851fb3

File tree

3 files changed

+395
-18
lines changed

3 files changed

+395
-18
lines changed

tests/Cli/BaseTestCase.php

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,98 @@
33
namespace StellarWP\Slic\Test\Cli;
44

55
use PHPUnit\Framework\TestCase;
6+
use StellarWP\Slic\Test\Support\Factories\Directory;
67

78
abstract class BaseTestCase extends TestCase {
89
protected ?string $initialDir = null;
910

11+
/**
12+
* Stack IDs created during the test, to be cleaned up in tearDown.
13+
*
14+
* @var string[]
15+
*/
16+
private array $createdStackIds = [];
17+
18+
private static string $dockerMockBin = '';
19+
20+
public static function setUpBeforeClass(): void {
21+
parent::setUpBeforeClass();
22+
self::$dockerMockBin = dirname( __DIR__ ) . '/_support/bin/docker-mock';
23+
}
24+
1025
public function setUp(): void {
1126
parent::setUp();
1227
$this->initialDir = getcwd();
1328
}
1429

1530
public function tearDown(): void {
16-
parent::tearDown();
31+
// Restore working directory first so stack stop can resolve paths.
1732
chdir( $this->initialDir );
33+
34+
// Unregister any stacks created during the test.
35+
foreach ( $this->createdStackIds as $stackId ) {
36+
$this->slicExec(
37+
'stack stop ' . escapeshellarg( $stackId ),
38+
$this->dockerMockEnv()
39+
);
40+
}
41+
42+
parent::tearDown();
1843
}
1944

2045
/**
2146
* Execute a slic command and return the output.
2247
*
23-
* @param string $command The command to execute, escaped if required.
48+
* @param string $command The command to execute, escaped if required.
49+
* @param array<string,string> $env Optional environment variables to set for the command.
2450
*
2551
* @return string The command output.
2652
*/
27-
protected function slicExec( string $command ): string {
28-
// Execute the command with NO_COLOR set to avoid color codes in the output.
29-
$commandString = 'NO_COLOR=1 php ' . escapeshellarg( dirname( __DIR__, 2 ) . '/slic.php' ) . ' ' . $command;
53+
protected function slicExec( string $command, array $env = [] ): string {
54+
$env['NO_COLOR'] = '1';
55+
56+
$envString = '';
57+
foreach ( $env as $key => $value ) {
58+
$envString .= $key . '=' . escapeshellarg( $value ) . ' ';
59+
}
60+
61+
$commandString = $envString . 'php ' . escapeshellarg( dirname( __DIR__, 2 ) . '/slic.php' ) . ' ' . $command;
3062

3163
// Redirect stderr to stdout to capture all output.
3264
return (string) shell_exec( $commandString . ' 2>&1' );
3365
}
66+
67+
/**
68+
* Returns env vars that mock docker binaries so that no real docker commands are executed.
69+
*
70+
* @return array<string,string>
71+
*/
72+
protected function dockerMockEnv(): array {
73+
return [
74+
'SLIC_DOCKER_BIN' => self::$dockerMockBin,
75+
'SLIC_DOCKER_COMPOSE_BIN' => self::$dockerMockBin,
76+
];
77+
}
78+
79+
/**
80+
* Creates a temporary plugins directory with a plugin, chdirs into it, and runs `slic here`.
81+
*
82+
* The created stack is automatically unregistered during tearDown.
83+
*
84+
* @param string $pluginName The name of the plugin directory to create.
85+
*
86+
* @return string The absolute path to the plugins directory.
87+
*/
88+
protected function setUpPluginsDir( string $pluginName = 'test-plugin' ): string {
89+
$pluginsDir = Directory::createTemp()
90+
->createPlugin( $pluginName )
91+
->getAbsolutePath();
92+
chdir( $pluginsDir );
93+
$this->slicExec( 'here' );
94+
95+
// Track the stack ID (resolved path) for cleanup.
96+
$this->createdStackIds[] = realpath( $pluginsDir );
97+
98+
return $pluginsDir;
99+
}
34100
}

0 commit comments

Comments
 (0)