|
3 | 3 | namespace StellarWP\Slic\Test\Cli; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use StellarWP\Slic\Test\Support\Factories\Directory; |
6 | 7 |
|
7 | 8 | abstract class BaseTestCase extends TestCase { |
8 | 9 | protected ?string $initialDir = null; |
9 | 10 |
|
| 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 | + |
10 | 25 | public function setUp(): void { |
11 | 26 | parent::setUp(); |
12 | 27 | $this->initialDir = getcwd(); |
13 | 28 | } |
14 | 29 |
|
15 | 30 | public function tearDown(): void { |
16 | | - parent::tearDown(); |
| 31 | + // Restore working directory first so stack stop can resolve paths. |
17 | 32 | 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(); |
18 | 43 | } |
19 | 44 |
|
20 | 45 | /** |
21 | 46 | * Execute a slic command and return the output. |
22 | 47 | * |
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. |
24 | 50 | * |
25 | 51 | * @return string The command output. |
26 | 52 | */ |
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; |
30 | 62 |
|
31 | 63 | // Redirect stderr to stdout to capture all output. |
32 | 64 | return (string) shell_exec( $commandString . ' 2>&1' ); |
33 | 65 | } |
| 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 | + } |
34 | 100 | } |
0 commit comments