|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Smoke test for workspace path CLI routing. |
| 4 | + * |
| 5 | + * Run: php tests/smoke-workspace-path-cli.php |
| 6 | + * |
| 7 | + * @package DataMachineCode\Tests |
| 8 | + */ |
| 9 | + |
| 10 | +declare( strict_types=1 ); |
| 11 | + |
| 12 | +namespace { |
| 13 | + if (! defined('ABSPATH') ) { |
| 14 | + define('ABSPATH', __DIR__); |
| 15 | + } |
| 16 | + |
| 17 | + class WP_CLI |
| 18 | + { |
| 19 | + public static array $logs = array(); |
| 20 | + public static array $successes = array(); |
| 21 | + |
| 22 | + public static function error( string $message ): void |
| 23 | + { |
| 24 | + throw new RuntimeException($message); |
| 25 | + } |
| 26 | + |
| 27 | + public static function success( string $message ): void |
| 28 | + { |
| 29 | + self::$successes[] = $message; |
| 30 | + } |
| 31 | + |
| 32 | + public static function log( string $message ): void |
| 33 | + { |
| 34 | + self::$logs[] = $message; |
| 35 | + } |
| 36 | + |
| 37 | + public static function warning( string $message ): void |
| 38 | + { |
| 39 | + self::$logs[] = 'warning: ' . $message; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + function is_wp_error( $_value ): bool |
| 44 | + { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + function wp_get_ability( string $name ) |
| 49 | + { |
| 50 | + return $GLOBALS['__abilities'][ $name ] ?? null; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +namespace DataMachine\Cli { |
| 55 | + class BaseCommand |
| 56 | + { |
| 57 | + protected function format_items( array $items, array $fields, array $assoc_args, string $default_sort = '' ): void |
| 58 | + { |
| 59 | + \WP_CLI::log('table:' . count($items) . ':' . implode(',', $fields)); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +namespace { |
| 65 | + include_once dirname(__DIR__) . '/inc/Cli/Commands/WorkspaceCommand.php'; |
| 66 | + |
| 67 | + function datamachine_code_workspace_path_cli_assert( bool $condition, string $message ): void |
| 68 | + { |
| 69 | + if ($condition ) { |
| 70 | + echo " [PASS] {$message}\n"; |
| 71 | + return; |
| 72 | + } |
| 73 | + echo " [FAIL] {$message}\n"; |
| 74 | + exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + class FakeWorkspacePathAbility |
| 78 | + { |
| 79 | + public array $last_input = array(); |
| 80 | + |
| 81 | + public function execute( array $input ): array |
| 82 | + { |
| 83 | + $this->last_input = $input; |
| 84 | + $name = (string) ( $input['name'] ?? '' ); |
| 85 | + |
| 86 | + return array( |
| 87 | + 'success' => true, |
| 88 | + 'path' => '' === $name ? '/workspace' : '/workspace/' . $name, |
| 89 | + 'exists' => true, |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + echo "=== smoke-workspace-path-cli ===\n"; |
| 95 | + |
| 96 | + $ability = new FakeWorkspacePathAbility(); |
| 97 | + $GLOBALS['__abilities']['datamachine-code/workspace-path'] = $ability; |
| 98 | + $command = new \DataMachineCode\Cli\Commands\WorkspaceCommand(); |
| 99 | + |
| 100 | + echo "\n[1] workspace path without a handle keeps root behavior\n"; |
| 101 | + WP_CLI::$logs = array(); |
| 102 | + $command->path(array(), array()); |
| 103 | + datamachine_code_workspace_path_cli_assert('/workspace' === ( WP_CLI::$logs[0] ?? null ), 'prints workspace root path'); |
| 104 | + datamachine_code_workspace_path_cli_assert(array( 'ensure' => false ) === $ability->last_input, 'root path sends only ensure flag'); |
| 105 | + |
| 106 | + echo "\n[2] workspace path accepts a primary repo handle\n"; |
| 107 | + WP_CLI::$logs = array(); |
| 108 | + $command->path(array( 'my-plugin' ), array()); |
| 109 | + datamachine_code_workspace_path_cli_assert('/workspace/my-plugin' === ( WP_CLI::$logs[0] ?? null ), 'prints primary checkout path'); |
| 110 | + datamachine_code_workspace_path_cli_assert('my-plugin' === ( $ability->last_input['name'] ?? null ), 'primary handle is passed to ability'); |
| 111 | + |
| 112 | + echo "\n[3] workspace path accepts a worktree handle\n"; |
| 113 | + WP_CLI::$logs = array(); |
| 114 | + $command->path(array( 'my-plugin@fix-foo' ), array()); |
| 115 | + datamachine_code_workspace_path_cli_assert('/workspace/my-plugin@fix-foo' === ( WP_CLI::$logs[0] ?? null ), 'prints worktree checkout path'); |
| 116 | + datamachine_code_workspace_path_cli_assert('my-plugin@fix-foo' === ( $ability->last_input['name'] ?? null ), 'worktree handle is passed to ability'); |
| 117 | + |
| 118 | + echo "\nResult: all passed\n"; |
| 119 | +} |
0 commit comments