Files
Gnuboard7/tests/Unit/Services/CoreUpdateServiceVendorModeTest.php
T
2026-04-20 20:37:49 +09:00

94 lines
3.2 KiB
PHP

<?php
namespace Tests\Unit\Services;
use App\Extension\Vendor\Exceptions\VendorInstallException;
use App\Extension\Vendor\VendorBundler;
use App\Extension\Vendor\VendorMode;
use App\Services\CoreUpdateService;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
/**
* CoreUpdateService::runVendorInstallInPending() 검증.
*
* VendorResolver 경유로 Composer/Bundled 모드 분기가 올바르게 동작하는지 확인.
*/
class CoreUpdateServiceVendorModeTest extends TestCase
{
private CoreUpdateService $service;
private string $pendingDir;
protected function setUp(): void
{
parent::setUp();
$this->service = app(CoreUpdateService::class);
$this->pendingDir = storage_path('app/test-core-vendor-pending-'.uniqid());
File::ensureDirectoryExists($this->pendingDir);
}
protected function tearDown(): void
{
if (File::isDirectory($this->pendingDir)) {
File::deleteDirectory($this->pendingDir);
}
parent::tearDown();
}
/**
* 테스트용 _pending 디렉토리에 vendor-bundle.zip 생성.
*/
private function createPendingWithBundle(): void
{
File::put($this->pendingDir.'/composer.json', json_encode([
'name' => 'test/core',
'require' => ['php' => '^8.2'],
]));
$zipPath = $this->pendingDir.'/vendor-bundle.zip';
$zip = new \ZipArchive;
$zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$zip->addFromString('vendor/autoload.php', '<?php // test autoload');
$zip->addFromString('vendor/laravel/framework/src/file.php', '<?php // test');
$zip->close();
File::put($this->pendingDir.'/vendor-bundle.json', json_encode([
'schema_version' => VendorBundler::SCHEMA_VERSION,
'zip_sha256' => hash_file('sha256', $zipPath),
'package_count' => 1,
]));
}
public function test_bundled_mode_extracts_vendor_bundle_zip(): void
{
$this->createPendingWithBundle();
$result = $this->service->runVendorInstallInPending($this->pendingDir, VendorMode::Bundled);
$this->assertSame(VendorMode::Bundled, $result->mode);
$this->assertSame('bundled', $result->strategy);
$this->assertFileExists($this->pendingDir.'/vendor/autoload.php');
}
public function test_bundled_mode_fails_when_no_bundle_present(): void
{
File::put($this->pendingDir.'/composer.json', json_encode(['name' => 'test/core']));
$this->expectException(VendorInstallException::class);
$this->service->runVendorInstallInPending($this->pendingDir, VendorMode::Bundled);
}
public function test_auto_mode_prefers_bundle_when_composer_cannot_execute(): void
{
$this->createPendingWithBundle();
// canExecuteComposer를 직접 제어할 수 없으므로 bundle 존재를 전제로
// Auto가 composer 가능하면 composer 콜백을 호출하려 시도할 것이고,
// composer 콜백은 실패할 것 — 대신 bundled로 해결되는 경로를 Bundled 명시로 시뮬레이션
$result = $this->service->runVendorInstallInPending($this->pendingDir, VendorMode::Bundled);
$this->assertSame('bundled', $result->strategy);
}
}