v7.0.0-beta.2 release
This commit is contained in:
1 parent
6595fd0eb5
commit
fa8eba7a0d
1165 files changed
+107118
-762127
No files matched your search
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\Vendor;
|
||||
|
||||
use App\Extension\Vendor\EnvironmentDetector;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EnvironmentDetectorTest extends TestCase
|
||||
{
|
||||
private EnvironmentDetector $detector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->detector = new EnvironmentDetector;
|
||||
}
|
||||
|
||||
public function test_has_proc_open_reflects_function_availability(): void
|
||||
{
|
||||
$result = $this->detector->hasProcOpen();
|
||||
$this->assertIsBool($result);
|
||||
|
||||
// proc_open 은 disable_functions 로 차단되지 않은 일반 테스트 환경에서는 true
|
||||
if (function_exists('proc_open')) {
|
||||
$disabled = array_map('trim', explode(',', (string) ini_get('disable_functions')));
|
||||
if (! in_array('proc_open', $disabled, true)) {
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_has_zip_archive_returns_true_when_class_exists(): void
|
||||
{
|
||||
$this->assertSame(class_exists(\ZipArchive::class), $this->detector->hasZipArchive());
|
||||
}
|
||||
|
||||
public function test_find_composer_binary_returns_null_when_no_candidate_available(): void
|
||||
{
|
||||
$detector = new EnvironmentDetector;
|
||||
$detector->resetCache();
|
||||
|
||||
// 절대 존재하지 않는 경로를 hint로 주고 ENV/config도 비운 상태 가정
|
||||
// (실제 환경에 composer가 설치되어 있으면 PATH 검색 단계에서 발견될 수 있음)
|
||||
$original = config('process.composer_binary');
|
||||
config(['process.composer_binary' => null]);
|
||||
|
||||
try {
|
||||
$result = $detector->findComposerBinary('/nonexistent/path/to/composer');
|
||||
// 반환값은 실제 PATH에 composer가 있느냐에 따라 달라짐 — null 또는 문자열
|
||||
$this->assertTrue($result === null || is_string($result));
|
||||
} finally {
|
||||
config(['process.composer_binary' => $original]);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_find_composer_binary_accepts_hint_with_spaces(): void
|
||||
{
|
||||
$detector = new EnvironmentDetector;
|
||||
$detector->resetCache();
|
||||
|
||||
// 공백 포함 힌트 — 파일 존재 검사 스킵하고 그대로 사용
|
||||
$hint = 'php /custom/path/composer.phar';
|
||||
$result = $detector->findComposerBinary($hint);
|
||||
|
||||
$this->assertSame($hint, $result);
|
||||
}
|
||||
|
||||
public function test_summarize_returns_complete_report(): void
|
||||
{
|
||||
$report = $this->detector->summarize();
|
||||
|
||||
$this->assertArrayHasKey('proc_open', $report);
|
||||
$this->assertArrayHasKey('shell_exec', $report);
|
||||
$this->assertArrayHasKey('zip_archive', $report);
|
||||
$this->assertArrayHasKey('composer_binary', $report);
|
||||
$this->assertArrayHasKey('composer_executable', $report);
|
||||
$this->assertArrayHasKey('can_use_composer', $report);
|
||||
$this->assertArrayHasKey('can_use_bundle', $report);
|
||||
|
||||
$this->assertIsBool($report['proc_open']);
|
||||
$this->assertIsBool($report['zip_archive']);
|
||||
$this->assertIsBool($report['can_use_composer']);
|
||||
$this->assertIsBool($report['can_use_bundle']);
|
||||
}
|
||||
|
||||
public function test_reset_cache_clears_cached_values(): void
|
||||
{
|
||||
$this->detector->canExecuteComposer();
|
||||
$this->detector->resetCache();
|
||||
|
||||
// 재호출 시 예외 없이 동작해야 함
|
||||
$result = $this->detector->canExecuteComposer();
|
||||
$this->assertIsBool($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\Vendor;
|
||||
|
||||
use App\Extension\Vendor\Exceptions\VendorInstallException;
|
||||
use App\Extension\Vendor\VendorBundleInstaller;
|
||||
use App\Extension\Vendor\VendorBundler;
|
||||
use App\Extension\Vendor\VendorIntegrityChecker;
|
||||
use App\Extension\Vendor\VendorMode;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VendorBundleInstallerTest extends TestCase
|
||||
{
|
||||
private VendorBundleInstaller $installer;
|
||||
|
||||
private string $testDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->installer = new VendorBundleInstaller(new VendorIntegrityChecker);
|
||||
$this->testDir = storage_path('app/test-vendor-installer-'.uniqid());
|
||||
File::ensureDirectoryExists($this->testDir);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (File::isDirectory($this->testDir)) {
|
||||
File::deleteDirectory($this->testDir);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 vendor-bundle.zip + manifest 생성.
|
||||
*/
|
||||
private function createValidBundle(array $files = ['vendor/test/pkg/file.php' => '<?php // test']): void
|
||||
{
|
||||
$zipPath = $this->testDir.'/vendor-bundle.zip';
|
||||
$zip = new \ZipArchive;
|
||||
$this->assertTrue($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === true);
|
||||
foreach ($files as $name => $content) {
|
||||
$zip->addFromString($name, $content);
|
||||
}
|
||||
$zip->close();
|
||||
|
||||
$manifest = [
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'generated_at' => date('c'),
|
||||
'generator' => 'test',
|
||||
'target' => 'test',
|
||||
'zip_sha256' => hash_file('sha256', $zipPath),
|
||||
'zip_size' => filesize($zipPath),
|
||||
'package_count' => 1,
|
||||
'packages' => [['name' => 'test/pkg', 'version' => '1.0.0', 'type' => 'library']],
|
||||
];
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode($manifest));
|
||||
}
|
||||
|
||||
public function test_install_throws_when_zip_missing(): void
|
||||
{
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$this->installer->install($this->testDir, $this->testDir);
|
||||
}
|
||||
|
||||
public function test_install_extracts_zip_successfully(): void
|
||||
{
|
||||
$this->createValidBundle();
|
||||
|
||||
$result = $this->installer->install($this->testDir, $this->testDir);
|
||||
|
||||
$this->assertSame(VendorMode::Bundled, $result->mode);
|
||||
$this->assertSame('bundled', $result->strategy);
|
||||
$this->assertSame(1, $result->packageCount);
|
||||
$this->assertFileExists($this->testDir.'/vendor/test/pkg/file.php');
|
||||
}
|
||||
|
||||
public function test_install_throws_on_hash_mismatch(): void
|
||||
{
|
||||
$this->createValidBundle();
|
||||
|
||||
// manifest 조작 — 잘못된 해시
|
||||
$manifest = json_decode(file_get_contents($this->testDir.'/vendor-bundle.json'), true);
|
||||
$manifest['zip_sha256'] = hash('sha256', 'tampered');
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode($manifest));
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$this->installer->install($this->testDir, $this->testDir);
|
||||
}
|
||||
|
||||
public function test_install_rejects_unsafe_paths(): void
|
||||
{
|
||||
// zip slip 시도
|
||||
$zipPath = $this->testDir.'/vendor-bundle.zip';
|
||||
$zip = new \ZipArchive;
|
||||
$zip->open($zipPath, \ZipArchive::CREATE);
|
||||
$zip->addFromString('../evil.php', '<?php // evil');
|
||||
$zip->close();
|
||||
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'zip_sha256' => hash_file('sha256', $zipPath),
|
||||
'package_count' => 0,
|
||||
]));
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$this->installer->install($this->testDir, $this->testDir);
|
||||
}
|
||||
|
||||
public function test_install_backs_up_existing_vendor_and_deletes_on_success(): void
|
||||
{
|
||||
// 기존 vendor/ 배치
|
||||
File::ensureDirectoryExists($this->testDir.'/vendor/old');
|
||||
File::put($this->testDir.'/vendor/old/marker.txt', 'old vendor');
|
||||
|
||||
$this->createValidBundle();
|
||||
|
||||
$this->installer->install($this->testDir, $this->testDir);
|
||||
|
||||
// 새 vendor/test/pkg/file.php 존재
|
||||
$this->assertFileExists($this->testDir.'/vendor/test/pkg/file.php');
|
||||
|
||||
// 기존 vendor/old/ 는 zip 추출에 의해 덮어써지지 않으므로 제거되지 않음
|
||||
// (백업에서 복원되지는 않으나, 이는 zip 이 해당 경로를 포함하지 않기 때문)
|
||||
// 중요: 백업 디렉토리 (vendor/.bundle_backup_*) 는 성공 시 삭제되어야 함
|
||||
$backups = glob($this->testDir.'/vendor/.bundle_backup_*');
|
||||
$this->assertEmpty($backups, '성공 시 백업 디렉토리는 삭제되어야 함');
|
||||
|
||||
// targetDir (최상위) 에는 vendor.old.* 가 절대 생성되지 않아야 함 (공유 호스팅 호환)
|
||||
$legacyBackups = glob($this->testDir.'/vendor.old.*');
|
||||
$this->assertEmpty($legacyBackups, 'targetDir 에 legacy vendor.old.* 디렉토리가 생성되면 안 됨');
|
||||
}
|
||||
|
||||
public function test_install_succeeds_when_only_vendor_dir_is_writable(): void
|
||||
{
|
||||
// 공유 호스팅 시나리오 재현: targetDir 은 있지만 vendor/ 만 쓰기 가능한 환경.
|
||||
// 실제 권한 변경은 Windows 에서 검증 어려우므로, 대신 새 구조의 동작 원리를 검증:
|
||||
// vendor/ 가 이미 존재하고 zip 추출이 vendor/ 내부 항목만 조작하는지 확인.
|
||||
File::ensureDirectoryExists($this->testDir.'/vendor');
|
||||
File::put($this->testDir.'/vendor/legacy.txt', 'preexisting');
|
||||
|
||||
$this->createValidBundle();
|
||||
|
||||
$result = $this->installer->install($this->testDir, $this->testDir);
|
||||
|
||||
$this->assertSame('bundled', $result->strategy);
|
||||
$this->assertFileExists($this->testDir.'/vendor/test/pkg/file.php');
|
||||
|
||||
// 백업 디렉토리는 성공 후 정리되어야 함
|
||||
$backups = glob($this->testDir.'/vendor/.bundle_backup_*');
|
||||
$this->assertEmpty($backups);
|
||||
|
||||
// targetDir 루트에는 어떠한 백업/임시 디렉토리도 만들어지지 않아야 함
|
||||
// (vendor/ 내부 백업 방식이므로 루트는 오염되지 않음)
|
||||
$topLevelEntries = array_filter(scandir($this->testDir), fn ($e) => ! in_array($e, ['.', '..', 'vendor', 'vendor-bundle.zip', 'vendor-bundle.json'], true));
|
||||
$this->assertEmpty($topLevelEntries, 'targetDir 루트가 오염되지 않아야 함');
|
||||
}
|
||||
}
|
||||
+324
@@ -0,0 +1,324 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\Vendor;
|
||||
|
||||
use App\Extension\Vendor\Exceptions\VendorInstallException;
|
||||
use App\Extension\Vendor\VendorBundler;
|
||||
use App\Extension\Vendor\VendorIntegrityChecker;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VendorBundlerTest extends TestCase
|
||||
{
|
||||
private VendorBundler $bundler;
|
||||
|
||||
private VendorIntegrityChecker $checker;
|
||||
|
||||
private string $testDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->checker = new VendorIntegrityChecker;
|
||||
$this->bundler = new VendorBundler($this->checker);
|
||||
|
||||
// 테스트는 실제 composer 실행 없이 가짜 vendor/ 구조를 스테이징에 생성.
|
||||
// 이는 실제 빌드 흐름(스테이징 → composer install → zip)을 시뮬레이션하되,
|
||||
// 외부 네트워크/composer 의존 없이 단위 테스트 속도를 유지하기 위함.
|
||||
$this->bundler->setComposerInstallRunner(function (string $stagingDir): void {
|
||||
File::ensureDirectoryExists($stagingDir.'/vendor/test/lib');
|
||||
File::put($stagingDir.'/vendor/test/lib/file.php', '<?php // test');
|
||||
File::put($stagingDir.'/vendor/autoload.php', '<?php // autoload');
|
||||
});
|
||||
|
||||
$this->testDir = storage_path('app/test-vendor-bundler-'.uniqid());
|
||||
File::ensureDirectoryExists($this->testDir);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (File::isDirectory($this->testDir)) {
|
||||
File::deleteDirectory($this->testDir);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 테스트용 composer 프로젝트 구조 생성.
|
||||
*
|
||||
* 스테이징 기반 빌드에서는 소스 디렉토리에 vendor/ 가 없어도 되고,
|
||||
* composer install 은 setUp() 의 주입된 runner 가 대신 처리한다.
|
||||
* 따라서 소스에는 composer.json + composer.lock 만 배치한다.
|
||||
*/
|
||||
private function createFakeProject(): void
|
||||
{
|
||||
File::put($this->testDir.'/composer.json', json_encode([
|
||||
'name' => 'test/project',
|
||||
// VendorBundler 는 php/ext-* 만 있는 확장을 skip 하므로 외부 의존성 선언 필수
|
||||
'require' => ['php' => '^8.2', 'test/lib' => '^1.0'],
|
||||
]));
|
||||
File::put($this->testDir.'/composer.lock', json_encode([
|
||||
'packages' => [
|
||||
['name' => 'test/lib', 'version' => '1.0.0', 'type' => 'library'],
|
||||
['name' => 'test/other', 'version' => '2.0.0', 'type' => 'library'],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_build_throws_when_composer_json_missing(): void
|
||||
{
|
||||
$this->expectException(VendorInstallException::class);
|
||||
|
||||
// buildForCore의 내부 build()에 도달하는 private 테스트는 어려우므로
|
||||
// buildForExtension 을 통해 간접적으로 검증
|
||||
$this->bundler->buildForExtension('module', 'nonexistent-module-test');
|
||||
}
|
||||
|
||||
/**
|
||||
* build() private 메서드 호출 헬퍼.
|
||||
*
|
||||
* 테스트는 단일 testDir 을 소스/출력 동일하게 사용 (코어 케이스와 유사).
|
||||
*/
|
||||
private function invokeBuild(string $target, bool $force = false, ?string $sourcePath = null, ?string $outputPath = null): \App\Extension\Vendor\VendorBundleResult
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->bundler);
|
||||
$method = $reflection->getMethod('build');
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invoke(
|
||||
$this->bundler,
|
||||
$sourcePath ?? $this->testDir,
|
||||
$outputPath ?? $this->testDir,
|
||||
$target,
|
||||
$force,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_build_creates_zip_and_manifest(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
|
||||
$result = $this->invokeBuild('test:project');
|
||||
|
||||
$this->assertFalse($result->skipped);
|
||||
$this->assertFileExists($this->testDir.'/vendor-bundle.zip');
|
||||
$this->assertFileExists($this->testDir.'/vendor-bundle.json');
|
||||
$this->assertSame(2, $result->packageCount);
|
||||
$this->assertGreaterThan(0, $result->zipSize);
|
||||
}
|
||||
|
||||
public function test_build_skips_when_up_to_date(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
|
||||
// 1차 빌드
|
||||
$first = $this->invokeBuild('test:project');
|
||||
$this->assertFalse($first->skipped);
|
||||
|
||||
// 2차 빌드 — composer.json 변경 없음 → 스킵
|
||||
$second = $this->invokeBuild('test:project');
|
||||
$this->assertTrue($second->skipped);
|
||||
$this->assertSame('up-to-date', $second->reason);
|
||||
}
|
||||
|
||||
public function test_build_force_rebuilds_even_when_up_to_date(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
|
||||
$this->invokeBuild('test:project');
|
||||
$second = $this->invokeBuild('test:project', force: true);
|
||||
|
||||
$this->assertFalse($second->skipped);
|
||||
}
|
||||
|
||||
public function test_is_stale_returns_true_when_no_bundle(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
$this->assertTrue($this->bundler->isStale($this->testDir));
|
||||
}
|
||||
|
||||
public function test_is_stale_returns_false_after_build(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
$this->invokeBuild('test:project');
|
||||
|
||||
$this->assertFalse($this->bundler->isStale($this->testDir));
|
||||
}
|
||||
|
||||
public function test_is_stale_returns_true_when_composer_json_changed(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
$this->invokeBuild('test:project');
|
||||
|
||||
// composer.json 변경
|
||||
File::put($this->testDir.'/composer.json', json_encode([
|
||||
'name' => 'test/project',
|
||||
'require' => ['php' => '^8.2', 'new/pkg' => '^1.0'],
|
||||
]));
|
||||
|
||||
$this->assertTrue($this->bundler->isStale($this->testDir));
|
||||
}
|
||||
|
||||
public function test_built_manifest_has_valid_sha256_for_integrity_check(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
$this->invokeBuild('test:project');
|
||||
|
||||
$integrity = $this->checker->verify($this->testDir);
|
||||
$this->assertTrue($integrity->valid, 'errors: '.implode(',', $integrity->errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* php/ext-* 런타임 제약만 있는 확장은 skip 처리되고 예외가 발생하지 않아야 합니다.
|
||||
*/
|
||||
public function test_build_skips_when_no_external_dependencies(): void
|
||||
{
|
||||
// 외부 의존성 없는 composer.json (php 버전만)
|
||||
File::put($this->testDir.'/composer.json', json_encode([
|
||||
'name' => 'test/no-deps',
|
||||
'require' => ['php' => '^8.2'],
|
||||
]));
|
||||
|
||||
$result = $this->invokeBuild('test:no-deps');
|
||||
|
||||
$this->assertTrue($result->skipped);
|
||||
$this->assertSame('no-external-dependencies', $result->reason);
|
||||
$this->assertFileDoesNotExist($this->testDir.'/vendor-bundle.zip');
|
||||
}
|
||||
|
||||
/**
|
||||
* ext-* 확장만 요구하는 경우도 외부 의존성 없음으로 간주됩니다.
|
||||
*/
|
||||
public function test_build_skips_when_only_php_extensions_required(): void
|
||||
{
|
||||
File::put($this->testDir.'/composer.json', json_encode([
|
||||
'name' => 'test/ext-only',
|
||||
'require' => ['php' => '^8.2', 'ext-json' => '*', 'ext-zip' => '*'],
|
||||
]));
|
||||
|
||||
$result = $this->invokeBuild('test:ext-only');
|
||||
|
||||
$this->assertTrue($result->skipped);
|
||||
$this->assertSame('no-external-dependencies', $result->reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* 분리 구조 — sourcePath 와 outputPath 가 다른 경우 (확장 케이스).
|
||||
*
|
||||
* _bundled 디렉토리에는 composer.json 만 있고, vendor/ 는 활성 디렉토리에만 있는 상황을 재현.
|
||||
*/
|
||||
public function test_build_writes_output_to_different_path_than_source(): void
|
||||
{
|
||||
$sourceDir = $this->testDir.'/active';
|
||||
$outputDir = $this->testDir.'/bundled';
|
||||
File::ensureDirectoryExists($sourceDir);
|
||||
File::ensureDirectoryExists($outputDir);
|
||||
|
||||
// 활성 디렉토리 — composer.json + composer.lock (vendor/ 는 스테이징에서 생성)
|
||||
File::put($sourceDir.'/composer.json', json_encode([
|
||||
'name' => 'test/project',
|
||||
'require' => ['php' => '^8.2', 'test/lib' => '^1.0'],
|
||||
]));
|
||||
File::put($sourceDir.'/composer.lock', json_encode([
|
||||
'packages' => [
|
||||
['name' => 'test/lib', 'version' => '1.0.0', 'type' => 'library'],
|
||||
],
|
||||
]));
|
||||
|
||||
// _bundled 디렉토리 — composer.json 만 (vendor/ 없음)
|
||||
File::put($outputDir.'/composer.json', File::get($sourceDir.'/composer.json'));
|
||||
|
||||
$result = $this->invokeBuild('test:split', sourcePath: $sourceDir, outputPath: $outputDir);
|
||||
|
||||
$this->assertFalse($result->skipped);
|
||||
|
||||
// 출력이 outputDir 에만 존재해야 함 (sourceDir 오염 없음)
|
||||
$this->assertFileExists($outputDir.'/vendor-bundle.zip');
|
||||
$this->assertFileExists($outputDir.'/vendor-bundle.json');
|
||||
$this->assertFileDoesNotExist($sourceDir.'/vendor-bundle.zip');
|
||||
$this->assertFileDoesNotExist($sourceDir.'/vendor-bundle.json');
|
||||
}
|
||||
|
||||
/**
|
||||
* 외부 의존성 없는 확장은 isStale 이 항상 false 를 반환해야 합니다.
|
||||
*/
|
||||
public function test_is_stale_returns_false_when_no_external_dependencies(): void
|
||||
{
|
||||
File::put($this->testDir.'/composer.json', json_encode([
|
||||
'name' => 'test/no-deps',
|
||||
'require' => ['php' => '^8.2'],
|
||||
]));
|
||||
|
||||
$this->assertFalse($this->bundler->isStale($this->testDir));
|
||||
}
|
||||
|
||||
/**
|
||||
* 스테이징 기반 빌드 — 개발 vendor/ 가 dev 의존성을 포함해도 번들은 깨끗함.
|
||||
*
|
||||
* 회귀 방지: autoload_files.php 가 존재하지 않는 dev 패키지를 require 하는 버그
|
||||
* (244 브랜치 shared hosting 실전 테스트에서 발견된 이슈).
|
||||
*
|
||||
* 주입된 runner 는 "clean --no-dev vendor/" 를 시뮬레이션하여 번들 내용이
|
||||
* 스테이징 vendor/ 와 일치하는지 검증한다.
|
||||
*/
|
||||
public function test_build_uses_staging_vendor_not_source_vendor(): void
|
||||
{
|
||||
$this->createFakeProject();
|
||||
|
||||
// 소스 디렉토리에 dev 파일/디렉토리를 생성 — 번들에 포함되지 않아야 함
|
||||
File::ensureDirectoryExists($this->testDir.'/vendor/fakerphp/faker');
|
||||
File::put($this->testDir.'/vendor/fakerphp/faker/README.md', '# Faker dev package');
|
||||
File::ensureDirectoryExists($this->testDir.'/vendor/myclabs/deep-copy');
|
||||
File::put($this->testDir.'/vendor/myclabs/deep-copy/deep_copy.php', '<?php');
|
||||
|
||||
$this->invokeBuild('test:staging');
|
||||
|
||||
$zip = new \ZipArchive;
|
||||
$this->assertTrue($zip->open($this->testDir.'/vendor-bundle.zip') === true);
|
||||
$fileList = [];
|
||||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||||
$fileList[] = $zip->getNameIndex($i);
|
||||
}
|
||||
$zip->close();
|
||||
|
||||
// 스테이징 runner 가 생성한 test/lib 만 포함되어야 함
|
||||
$this->assertTrue(
|
||||
collect($fileList)->contains(fn ($f) => str_contains($f, 'vendor/test/lib')),
|
||||
'runner 가 생성한 test/lib 은 포함되어야 합니다'
|
||||
);
|
||||
|
||||
// 소스의 dev 디렉토리는 번들에 절대 포함되지 않아야 함
|
||||
$this->assertFalse(
|
||||
collect($fileList)->contains(fn ($f) => str_contains($f, 'fakerphp')),
|
||||
'소스의 dev 패키지 fakerphp 는 번들에 포함되면 안 됩니다 (스테이징이 아닌 소스 사용 금지)'
|
||||
);
|
||||
$this->assertFalse(
|
||||
collect($fileList)->contains(fn ($f) => str_contains($f, 'myclabs')),
|
||||
'소스의 dev 패키지 myclabs 는 번들에 포함되면 안 됩니다'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* composer 미설치 환경에서 명확한 예외 발생.
|
||||
*
|
||||
* EnvironmentDetector 가 composer 미실행으로 보고하도록 mock 을 주입하고,
|
||||
* composerInstallRunner 도 null 로 초기화한 뒤 빌드 시도.
|
||||
*/
|
||||
public function test_build_throws_when_composer_not_available(): void
|
||||
{
|
||||
$detector = \Mockery::mock(\App\Extension\Vendor\EnvironmentDetector::class);
|
||||
$detector->shouldReceive('canExecuteComposer')->andReturn(false);
|
||||
|
||||
$bundler = new VendorBundler($this->checker, $detector);
|
||||
// 기본 runner (proc_open) 사용하도록 override 안 함
|
||||
|
||||
$this->createFakeProject();
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
|
||||
$reflection = new \ReflectionClass($bundler);
|
||||
$method = $reflection->getMethod('build');
|
||||
$method->setAccessible(true);
|
||||
$method->invoke($bundler, $this->testDir, $this->testDir, 'test:no-composer', false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\Vendor;
|
||||
|
||||
use App\Extension\Vendor\VendorBundler;
|
||||
use App\Extension\Vendor\VendorIntegrityChecker;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VendorIntegrityCheckerTest extends TestCase
|
||||
{
|
||||
private VendorIntegrityChecker $checker;
|
||||
|
||||
private string $testDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->checker = new VendorIntegrityChecker;
|
||||
$this->testDir = storage_path('app/test-vendor-integrity-'.uniqid());
|
||||
File::ensureDirectoryExists($this->testDir);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (File::isDirectory($this->testDir)) {
|
||||
File::deleteDirectory($this->testDir);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_verify_fails_when_zip_missing(): void
|
||||
{
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('bundle_zip_missing', $result->errors);
|
||||
}
|
||||
|
||||
public function test_verify_fails_when_manifest_missing(): void
|
||||
{
|
||||
File::put($this->testDir.'/vendor-bundle.zip', 'dummy zip content');
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('bundle_manifest_missing', $result->errors);
|
||||
}
|
||||
|
||||
public function test_verify_fails_when_manifest_invalid_json(): void
|
||||
{
|
||||
File::put($this->testDir.'/vendor-bundle.zip', 'dummy');
|
||||
File::put($this->testDir.'/vendor-bundle.json', '{invalid json');
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('bundle_manifest_invalid', $result->errors);
|
||||
}
|
||||
|
||||
public function test_verify_fails_on_unsupported_schema_version(): void
|
||||
{
|
||||
File::put($this->testDir.'/vendor-bundle.zip', 'dummy');
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => '99.0',
|
||||
'zip_sha256' => hash('sha256', 'dummy'),
|
||||
]));
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('bundle_schema_unsupported', $result->errors);
|
||||
}
|
||||
|
||||
public function test_verify_fails_on_zip_hash_mismatch(): void
|
||||
{
|
||||
File::put($this->testDir.'/vendor-bundle.zip', 'actual content');
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'zip_sha256' => hash('sha256', 'different content'),
|
||||
]));
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('zip_hash_mismatch', $result->errors);
|
||||
}
|
||||
|
||||
public function test_verify_passes_on_matching_hash(): void
|
||||
{
|
||||
$content = 'matching zip content';
|
||||
File::put($this->testDir.'/vendor-bundle.zip', $content);
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'zip_sha256' => hash('sha256', $content),
|
||||
'package_count' => 42,
|
||||
]));
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertTrue($result->valid, 'errors: '.implode(',', $result->errors));
|
||||
$this->assertEmpty($result->errors);
|
||||
$this->assertSame(42, $result->meta['package_count']);
|
||||
}
|
||||
|
||||
public function test_verify_detects_composer_json_mismatch(): void
|
||||
{
|
||||
$zipContent = 'zip content';
|
||||
$composerJsonContent = '{"name":"new"}';
|
||||
$storedHash = hash('sha256', '{"name":"old"}');
|
||||
|
||||
File::put($this->testDir.'/vendor-bundle.zip', $zipContent);
|
||||
File::put($this->testDir.'/composer.json', $composerJsonContent);
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'zip_sha256' => hash('sha256', $zipContent),
|
||||
'composer_json_sha256' => $storedHash,
|
||||
]));
|
||||
|
||||
$result = $this->checker->verify($this->testDir);
|
||||
|
||||
$this->assertFalse($result->valid);
|
||||
$this->assertContains('composer_json_sha_mismatch', $result->errors);
|
||||
}
|
||||
|
||||
public function test_read_manifest_returns_null_when_missing(): void
|
||||
{
|
||||
$this->assertNull($this->checker->readManifest($this->testDir));
|
||||
}
|
||||
|
||||
public function test_read_manifest_returns_parsed_array(): void
|
||||
{
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode(['key' => 'value']));
|
||||
|
||||
$result = $this->checker->readManifest($this->testDir);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertSame('value', $result['key']);
|
||||
}
|
||||
|
||||
public function test_compute_file_hash_returns_sha256(): void
|
||||
{
|
||||
$path = $this->testDir.'/test.txt';
|
||||
File::put($path, 'hello');
|
||||
|
||||
$hash = $this->checker->computeFileHash($path);
|
||||
|
||||
$this->assertSame(hash('sha256', 'hello'), $hash);
|
||||
}
|
||||
}
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\Vendor;
|
||||
|
||||
use App\Extension\Vendor\EnvironmentDetector;
|
||||
use App\Extension\Vendor\Exceptions\VendorInstallException;
|
||||
use App\Extension\Vendor\VendorBundleInstaller;
|
||||
use App\Extension\Vendor\VendorBundler;
|
||||
use App\Extension\Vendor\VendorInstallContext;
|
||||
use App\Extension\Vendor\VendorInstallResult;
|
||||
use App\Extension\Vendor\VendorIntegrityChecker;
|
||||
use App\Extension\Vendor\VendorMode;
|
||||
use App\Extension\Vendor\VendorResolver;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VendorResolverTest extends TestCase
|
||||
{
|
||||
private string $testDir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->testDir = storage_path('app/test-vendor-resolver-'.uniqid());
|
||||
File::ensureDirectoryExists($this->testDir);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (File::isDirectory($this->testDir)) {
|
||||
File::deleteDirectory($this->testDir);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
private function makeResolver(bool $canExecuteComposer): VendorResolver
|
||||
{
|
||||
$env = new class($canExecuteComposer) extends EnvironmentDetector
|
||||
{
|
||||
public function __construct(private bool $canExec) {}
|
||||
|
||||
public function canExecuteComposer(?string $hint = null): bool
|
||||
{
|
||||
return $this->canExec;
|
||||
}
|
||||
};
|
||||
|
||||
$checker = new VendorIntegrityChecker;
|
||||
|
||||
return new VendorResolver(
|
||||
environmentDetector: $env,
|
||||
integrityChecker: $checker,
|
||||
bundleInstaller: new VendorBundleInstaller($checker),
|
||||
);
|
||||
}
|
||||
|
||||
private function createValidBundle(): void
|
||||
{
|
||||
$zipPath = $this->testDir.'/vendor-bundle.zip';
|
||||
$zip = new \ZipArchive;
|
||||
$zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
||||
$zip->addFromString('vendor/autoload.php', '<?php // test');
|
||||
$zip->close();
|
||||
|
||||
File::put($this->testDir.'/vendor-bundle.json', json_encode([
|
||||
'schema_version' => VendorBundler::SCHEMA_VERSION,
|
||||
'zip_sha256' => hash_file('sha256', $zipPath),
|
||||
'package_count' => 1,
|
||||
]));
|
||||
}
|
||||
|
||||
private function makeContext(VendorMode $requested = VendorMode::Auto, ?VendorMode $previous = null, string $operation = 'install'): VendorInstallContext
|
||||
{
|
||||
return new VendorInstallContext(
|
||||
target: 'module',
|
||||
identifier: 'test-mod',
|
||||
sourceDir: $this->testDir,
|
||||
targetDir: $this->testDir,
|
||||
requestedMode: $requested,
|
||||
previousMode: $previous,
|
||||
operation: $operation,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_resolves_explicit_composer_mode(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Composer);
|
||||
|
||||
$this->assertSame(VendorMode::Composer, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_resolves_explicit_bundled_mode(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Bundled);
|
||||
|
||||
$this->assertSame(VendorMode::Bundled, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_auto_resolves_to_composer_when_available(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext();
|
||||
|
||||
$this->assertSame(VendorMode::Composer, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_auto_resolves_to_bundled_when_composer_unavailable_but_bundle_exists(): void
|
||||
{
|
||||
$this->createValidBundle();
|
||||
$resolver = $this->makeResolver(canExecuteComposer: false);
|
||||
$ctx = $this->makeContext();
|
||||
|
||||
$this->assertSame(VendorMode::Bundled, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_auto_throws_when_neither_composer_nor_bundle_available(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: false);
|
||||
$ctx = $this->makeContext();
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$resolver->resolveMode($ctx);
|
||||
}
|
||||
|
||||
public function test_update_inherits_previous_mode(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(
|
||||
requested: VendorMode::Auto,
|
||||
previous: VendorMode::Bundled,
|
||||
operation: 'update',
|
||||
);
|
||||
|
||||
$this->assertSame(VendorMode::Bundled, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_update_does_not_inherit_auto_previous_mode(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(
|
||||
requested: VendorMode::Auto,
|
||||
previous: VendorMode::Auto,
|
||||
operation: 'update',
|
||||
);
|
||||
|
||||
// auto → auto 해석 → composer 가능하므로 composer 반환
|
||||
$this->assertSame(VendorMode::Composer, $resolver->resolveMode($ctx));
|
||||
}
|
||||
|
||||
public function test_install_throws_when_composer_mode_but_executor_missing(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Composer);
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$resolver->install($ctx, composerExecutor: null);
|
||||
}
|
||||
|
||||
public function test_install_delegates_to_composer_executor_callback(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Composer);
|
||||
|
||||
$called = false;
|
||||
$executor = function (VendorInstallContext $c) use (&$called) {
|
||||
$called = true;
|
||||
|
||||
return new VendorInstallResult(
|
||||
mode: VendorMode::Composer,
|
||||
strategy: 'composer',
|
||||
packageCount: 10,
|
||||
);
|
||||
};
|
||||
|
||||
$result = $resolver->install($ctx, $executor);
|
||||
|
||||
$this->assertTrue($called);
|
||||
$this->assertSame(VendorMode::Composer, $result->mode);
|
||||
$this->assertSame(10, $result->packageCount);
|
||||
}
|
||||
|
||||
public function test_install_executes_bundle_strategy_end_to_end(): void
|
||||
{
|
||||
$this->createValidBundle();
|
||||
$resolver = $this->makeResolver(canExecuteComposer: false);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Bundled);
|
||||
|
||||
$result = $resolver->install($ctx);
|
||||
|
||||
$this->assertSame(VendorMode::Bundled, $result->mode);
|
||||
$this->assertFileExists($this->testDir.'/vendor/autoload.php');
|
||||
}
|
||||
|
||||
public function test_install_fails_when_bundled_mode_but_no_zip(): void
|
||||
{
|
||||
$resolver = $this->makeResolver(canExecuteComposer: true);
|
||||
$ctx = $this->makeContext(requested: VendorMode::Bundled);
|
||||
|
||||
$this->expectException(VendorInstallException::class);
|
||||
$resolver->install($ctx);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user