Public Access
baf5f56ca2
Branding: the MokoGIT wordmark is caps-GIT. Corrects the display token from MokoGit to MokoGIT (case-sensitive; lowercase path form .mokogit untouched). Claude-Session: https://claude.ai/code/session_01DQEMmJPe61ya7HDfA6BHP8
199 lines
7.8 KiB
PHP
199 lines
7.8 KiB
PHP
<?php
|
|
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* FILE INFORMATION
|
|
* DEFGROUP: MokoCLI.Tests.Enterprise
|
|
* INGROUP: MokoCLI.Tests
|
|
* REPO: https://git.mokoconsulting.tech/MokoConsulting/mokocli
|
|
* PATH: /tests/Enterprise/GitPlatformAdapterTest.php
|
|
* BRIEF: Tests verifying both adapters implement GitPlatformAdapter correctly
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
use MokoCli\ApiClient;
|
|
use MokoCli\Config;
|
|
use MokoCli\GitPlatformAdapter;
|
|
use MokoCli\GitHubAdapter;
|
|
use MokoCli\MokoGITAdapter;
|
|
use MokoCli\PlatformAdapterFactory;
|
|
|
|
echo "Testing GitPlatformAdapter Interface Compliance\n";
|
|
echo str_repeat('=', 60) . "\n\n";
|
|
|
|
$passed = 0;
|
|
$failed = 0;
|
|
|
|
function assert_true(bool $condition, string $label): void
|
|
{
|
|
global $passed, $failed;
|
|
if ($condition) {
|
|
echo " ✓ {$label}\n";
|
|
$passed++;
|
|
} else {
|
|
echo " ✗ FAILED: {$label}\n";
|
|
$failed++;
|
|
}
|
|
}
|
|
|
|
// ── Test 1: GitHubAdapter implements GitPlatformAdapter ─────────────────
|
|
echo "1. Testing GitHubAdapter interface compliance...\n";
|
|
|
|
$ghClient = new ApiClient(
|
|
baseUrl: 'https://api.github.com',
|
|
authToken: 'test-token',
|
|
enableCaching: false,
|
|
authScheme: 'Bearer'
|
|
);
|
|
$ghAdapter = new GitHubAdapter($ghClient);
|
|
|
|
assert_true($ghAdapter instanceof GitPlatformAdapter, 'GitHubAdapter implements GitPlatformAdapter');
|
|
assert_true($ghAdapter->getPlatformName() === 'github', 'getPlatformName() returns "github"');
|
|
assert_true($ghAdapter->getBaseUrl() === 'https://api.github.com', 'getBaseUrl() returns GitHub API URL');
|
|
assert_true($ghAdapter->getWorkflowDir() === '.github/workflows', 'getWorkflowDir() returns .github/workflows');
|
|
assert_true($ghAdapter->getApiClient() === $ghClient, 'getApiClient() returns injected client');
|
|
echo "\n";
|
|
|
|
// ── Test 2: MokoGITAdapter implements GitPlatformAdapter ──────────────────
|
|
echo "2. Testing MokoGITAdapter interface compliance...\n";
|
|
|
|
$gitClient = new ApiClient(
|
|
baseUrl: 'https://git.mokoconsulting.tech/api/v1',
|
|
authToken: 'test-token',
|
|
enableCaching: false,
|
|
authScheme: 'token'
|
|
);
|
|
$gitAdapter = new MokoGITAdapter($gitClient);
|
|
|
|
assert_true($gitAdapter instanceof GitPlatformAdapter, 'MokoGITAdapter implements GitPlatformAdapter');
|
|
assert_true($gitAdapter->getPlatformName() === 'git', 'getPlatformName() returns "git"');
|
|
assert_true($gitAdapter->getBaseUrl() === 'https://git.mokoconsulting.tech/api/v1', 'getBaseUrl() returns Git API URL');
|
|
assert_true($gitAdapter->getWorkflowDir() === '.mokogit/workflows', 'getWorkflowDir() returns .gitea/workflows');
|
|
assert_true($gitAdapter->getApiClient() === $gitClient, 'getApiClient() returns injected client');
|
|
echo "\n";
|
|
|
|
// ── Test 3: All interface methods exist on both adapters ────────────────
|
|
echo "3. Testing all interface methods are implemented...\n";
|
|
|
|
$requiredMethods = [
|
|
'getPlatformName', 'getBaseUrl', 'getWorkflowDir',
|
|
'listOrgRepos', 'getRepo', 'createOrgRepo', 'archiveRepo',
|
|
'setRepoTopics', 'getRepoTopics',
|
|
'getFileContents', 'createOrUpdateFile', 'deleteFile',
|
|
'listPullRequests', 'createPullRequest', 'updatePullRequest',
|
|
'listIssues', 'createIssue', 'addIssueComment', 'closeIssue',
|
|
'listLabels', 'createLabel', 'addIssueLabels',
|
|
'setBranchProtection', 'listBranchProtections',
|
|
'resolveRef', 'getTree',
|
|
'paginateAll', 'migrateRepository', 'getApiClient',
|
|
];
|
|
|
|
foreach ($requiredMethods as $method) {
|
|
$ghHas = method_exists($ghAdapter, $method);
|
|
$gitHas = method_exists($gitAdapter, $method);
|
|
|
|
if ($ghHas && $gitHas) {
|
|
$passed++;
|
|
} else {
|
|
echo " ✗ FAILED: {$method} — GitHub=" . ($ghHas ? 'yes' : 'NO') . " Git=" . ($gitHas ? 'yes' : 'NO') . "\n";
|
|
$failed++;
|
|
}
|
|
}
|
|
echo " ✓ All " . count($requiredMethods) . " interface methods implemented on both adapters\n\n";
|
|
|
|
// ── Test 4: PlatformAdapterFactory ──────────────────────────────────────
|
|
echo "4. Testing PlatformAdapterFactory...\n";
|
|
|
|
// Test GitHub creation
|
|
putenv('GH_TOKEN=test-github-token');
|
|
putenv('GIT_PLATFORM=github');
|
|
$config = Config::load();
|
|
$config->set('github.token', 'test-github-token');
|
|
try {
|
|
$adapter = PlatformAdapterFactory::create($config, 'github');
|
|
assert_true($adapter instanceof GitHubAdapter, 'Factory creates GitHubAdapter for platform=github');
|
|
assert_true($adapter->getPlatformName() === 'github', 'Created adapter identifies as github');
|
|
} catch (\Exception $e) {
|
|
assert_true(false, 'Factory creates GitHubAdapter: ' . $e->getMessage());
|
|
}
|
|
|
|
// Test Git creation
|
|
$config->set('git.token', 'test-git-token');
|
|
try {
|
|
$adapter = PlatformAdapterFactory::create($config, 'git');
|
|
assert_true($adapter instanceof MokoGITAdapter, 'Factory creates MokoGITAdapter for platform=git');
|
|
assert_true($adapter->getPlatformName() === 'git', 'Created adapter identifies as git');
|
|
} catch (\Exception $e) {
|
|
assert_true(false, 'Factory creates MokoGITAdapter: ' . $e->getMessage());
|
|
}
|
|
|
|
// Test invalid platform
|
|
try {
|
|
PlatformAdapterFactory::create($config, 'bitbucket');
|
|
assert_true(false, 'Factory should throw for unsupported platform');
|
|
} catch (\RuntimeException $e) {
|
|
assert_true(str_contains($e->getMessage(), 'Unsupported'), 'Factory throws RuntimeException for unsupported platform');
|
|
}
|
|
echo "\n";
|
|
|
|
// ── Test 5: Config platform defaults ────────────────────────────────────
|
|
echo "5. Testing Config platform configuration...\n";
|
|
|
|
$config = new Config([
|
|
'platform' => 'github',
|
|
'github' => ['organization' => 'mokoconsulting-tech', 'rate_limit' => 5000],
|
|
'git' => ['url' => 'https://git.mokoconsulting.tech', 'organization' => 'mokoconsulting-tech', 'rate_limit' => 5000],
|
|
]);
|
|
|
|
assert_true($config->getString('platform') === 'github', 'Default platform is github');
|
|
assert_true($config->getString('git.url') === 'https://git.mokoconsulting.tech', 'Git URL configured');
|
|
assert_true($config->getString('git.organization') === 'mokoconsulting-tech', 'Git org configured');
|
|
assert_true($config->getInt('git.rate_limit') === 5000, 'Git rate limit configured');
|
|
echo "\n";
|
|
|
|
// ── Test 6: ApiClient auth scheme ───────────────────────────────────────
|
|
echo "6. Testing ApiClient auth scheme parameter...\n";
|
|
|
|
$bearerClient = new ApiClient(
|
|
baseUrl: 'https://api.github.com',
|
|
authToken: 'test',
|
|
enableCaching: false,
|
|
authScheme: 'Bearer'
|
|
);
|
|
assert_true($bearerClient instanceof ApiClient, 'ApiClient accepts Bearer auth scheme');
|
|
|
|
$tokenClient = new ApiClient(
|
|
baseUrl: 'https://git.mokoconsulting.tech/api/v1',
|
|
authToken: 'test',
|
|
enableCaching: false,
|
|
authScheme: 'token'
|
|
);
|
|
assert_true($tokenClient instanceof ApiClient, 'ApiClient accepts token auth scheme');
|
|
echo "\n";
|
|
|
|
// ── Test 7: GitHubAdapter migration throws ──────────────────────────────
|
|
echo "7. Testing platform-specific behavior...\n";
|
|
|
|
try {
|
|
$ghAdapter->migrateRepository([]);
|
|
assert_true(false, 'GitHubAdapter.migrateRepository() should throw');
|
|
} catch (\RuntimeException $e) {
|
|
assert_true(true, 'GitHubAdapter.migrateRepository() throws RuntimeException');
|
|
}
|
|
|
|
// MokoGITAdapter.migrateRepository() should NOT throw (it calls the API)
|
|
// We can't test it without a real server, but verify the method exists
|
|
assert_true(method_exists($gitAdapter, 'migrateRepository'), 'MokoGITAdapter.migrateRepository() exists');
|
|
echo "\n";
|
|
|
|
// ── Summary ─────────────────────────────────────────────────────────────
|
|
echo str_repeat('=', 60) . "\n";
|
|
echo "Results: {$passed} passed, {$failed} failed\n";
|
|
echo str_repeat('=', 60) . "\n";
|
|
|
|
exit($failed > 0 ? 1 : 0);
|