v7.0.0-beta.1 release
This commit is contained in:
commit
6595fd0eb5
3967 files changed
+1875383
No files matched your search
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Helpers;
|
||||
|
||||
use App\Contracts\Extension\ModuleInterface;
|
||||
use App\Contracts\Extension\PluginInterface;
|
||||
use App\Contracts\Extension\StorageInterface;
|
||||
use Mockery;
|
||||
|
||||
/**
|
||||
* 확장 시스템 Mock 객체 생성 헬퍼 Trait
|
||||
*
|
||||
* Module, Plugin, Storage 등의 Mock 객체를 일관되게 생성합니다.
|
||||
* 빌더 패턴으로 필요한 부분만 커스터마이징 가능합니다.
|
||||
*
|
||||
* 사용 예시:
|
||||
* ```php
|
||||
* $module = $this->createMockModule([
|
||||
* 'identifier' => 'test-module',
|
||||
* 'configValues' => ['api_key' => 'test'],
|
||||
* 'storage' => ['exists' => true, 'get' => '{"key":"value"}'],
|
||||
* ]);
|
||||
* ```
|
||||
*/
|
||||
trait MocksExtensions
|
||||
{
|
||||
/**
|
||||
* Module Mock 객체를 생성합니다.
|
||||
*
|
||||
* @param array $config 설정 옵션
|
||||
* - identifier: 모듈 식별자 (기본: test-module-{uniqid})
|
||||
* - configValues: getConfigValues() 반환값
|
||||
* - settingsSchema: getSettingsSchema() 반환값
|
||||
* - settingsDefaultsPath: getSettingsDefaultsPath() 반환값
|
||||
* - withStorage: getStorage() 메서드 추가 여부 (기본: true)
|
||||
* - storage: StorageInterface Mock 설정 (배열)
|
||||
* @return ModuleInterface Mock 객체
|
||||
*/
|
||||
protected function createMockModule(array $config = []): ModuleInterface
|
||||
{
|
||||
$mock = Mockery::mock(ModuleInterface::class);
|
||||
|
||||
// 기본 메서드: getIdentifier
|
||||
$identifier = $config['identifier'] ?? 'test-module-'.uniqid();
|
||||
$mock->shouldReceive('getIdentifier')
|
||||
->andReturn($identifier);
|
||||
|
||||
// getConfigValues
|
||||
if (isset($config['configValues'])) {
|
||||
$mock->shouldReceive('getConfigValues')
|
||||
->andReturn($config['configValues']);
|
||||
}
|
||||
|
||||
// getSettingsSchema
|
||||
if (isset($config['settingsSchema'])) {
|
||||
$mock->shouldReceive('getSettingsSchema')
|
||||
->andReturn($config['settingsSchema']);
|
||||
} else {
|
||||
// 기본값: 빈 스키마
|
||||
$mock->shouldReceive('getSettingsSchema')
|
||||
->andReturn([]);
|
||||
}
|
||||
|
||||
// getSettingsDefaultsPath
|
||||
if (array_key_exists('settingsDefaultsPath', $config)) {
|
||||
$mock->shouldReceive('getSettingsDefaultsPath')
|
||||
->andReturn($config['settingsDefaultsPath']);
|
||||
}
|
||||
|
||||
// hasSettings
|
||||
if (isset($config['hasSettings'])) {
|
||||
$mock->shouldReceive('hasSettings')
|
||||
->andReturn($config['hasSettings']);
|
||||
}
|
||||
|
||||
// getStorage - StorageInterface Mock 자동 생성
|
||||
if ($config['withStorage'] ?? true) {
|
||||
$storageMock = $this->createMockStorage(
|
||||
array_merge(
|
||||
['identifier' => $identifier, 'type' => 'modules'],
|
||||
$config['storage'] ?? []
|
||||
)
|
||||
);
|
||||
$mock->shouldReceive('getStorage')
|
||||
->andReturn($storageMock);
|
||||
}
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin Mock 객체를 생성합니다.
|
||||
*
|
||||
* @param array $config 설정 옵션
|
||||
* - identifier: 플러그인 식별자 (기본: test-plugin-{uniqid})
|
||||
* - configValues: getConfigValues() 반환값
|
||||
* - settingsSchema: getSettingsSchema() 반환값
|
||||
* - withStorage: getStorage() 메서드 추가 여부 (기본: true)
|
||||
* - storage: StorageInterface Mock 설정 (배열)
|
||||
* @return PluginInterface Mock 객체
|
||||
*/
|
||||
protected function createMockPlugin(array $config = []): PluginInterface
|
||||
{
|
||||
$mock = Mockery::mock(PluginInterface::class);
|
||||
|
||||
// 기본 메서드: getIdentifier
|
||||
$identifier = $config['identifier'] ?? 'test-plugin-'.uniqid();
|
||||
$mock->shouldReceive('getIdentifier')
|
||||
->andReturn($identifier);
|
||||
|
||||
// getConfigValues
|
||||
if (isset($config['configValues'])) {
|
||||
$mock->shouldReceive('getConfigValues')
|
||||
->andReturn($config['configValues']);
|
||||
}
|
||||
|
||||
// getSettingsSchema
|
||||
if (isset($config['settingsSchema'])) {
|
||||
$mock->shouldReceive('getSettingsSchema')
|
||||
->andReturn($config['settingsSchema']);
|
||||
} else {
|
||||
// 기본값: 빈 스키마
|
||||
$mock->shouldReceive('getSettingsSchema')
|
||||
->andReturn([]);
|
||||
}
|
||||
|
||||
// hasSettings
|
||||
if (isset($config['hasSettings'])) {
|
||||
$mock->shouldReceive('hasSettings')
|
||||
->andReturn($config['hasSettings']);
|
||||
}
|
||||
|
||||
// getSettingsDefaultsPath
|
||||
if (array_key_exists('settingsDefaultsPath', $config)) {
|
||||
$mock->shouldReceive('getSettingsDefaultsPath')
|
||||
->andReturn($config['settingsDefaultsPath']);
|
||||
}
|
||||
|
||||
// getSettingsLayout
|
||||
if (isset($config['settingsLayout'])) {
|
||||
$mock->shouldReceive('getSettingsLayout')
|
||||
->andReturn($config['settingsLayout']);
|
||||
}
|
||||
|
||||
// getStorage - StorageInterface Mock 자동 생성
|
||||
if ($config['withStorage'] ?? true) {
|
||||
$storageMock = $this->createMockStorage(
|
||||
array_merge(
|
||||
['identifier' => $identifier, 'type' => 'plugins'],
|
||||
$config['storage'] ?? []
|
||||
)
|
||||
);
|
||||
$mock->shouldReceive('getStorage')
|
||||
->andReturn($storageMock);
|
||||
}
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* StorageInterface Mock 객체를 생성합니다.
|
||||
*
|
||||
* @param array $config 설정 옵션
|
||||
* - identifier: 확장 식별자
|
||||
* - type: 'modules' 또는 'plugins'
|
||||
* - disk: getDisk() 반환값 (기본: 'local')
|
||||
* - exists: exists() 반환값 (기본: false)
|
||||
* - get: get() 반환값 (기본: null)
|
||||
* - put: put() 반환값 (기본: true)
|
||||
* - delete: delete() 반환값 (기본: true)
|
||||
* - deleteDirectory: deleteDirectory() 반환값 (기본: true)
|
||||
* - deleteAll: deleteAll() 반환값 (기본: true)
|
||||
* - url: url() 반환값 (기본: null)
|
||||
* - files: files() 반환값 (기본: [])
|
||||
* - getBasePath: getBasePath() 반환값 (자동 생성)
|
||||
* @return StorageInterface Mock 객체
|
||||
*/
|
||||
protected function createMockStorage(array $config = []): StorageInterface
|
||||
{
|
||||
$mock = Mockery::mock(StorageInterface::class);
|
||||
|
||||
$disk = $config['disk'] ?? 'local';
|
||||
$identifier = $config['identifier'] ?? 'test-extension';
|
||||
$type = $config['type'] ?? 'modules';
|
||||
|
||||
// getDisk
|
||||
$mock->shouldReceive('getDisk')
|
||||
->andReturn($disk);
|
||||
|
||||
// exists
|
||||
if (isset($config['exists'])) {
|
||||
$mock->shouldReceive('exists')
|
||||
->andReturn($config['exists']);
|
||||
} else {
|
||||
// 기본: 항상 false 반환
|
||||
$mock->shouldReceive('exists')
|
||||
->andReturn(false);
|
||||
}
|
||||
|
||||
// get
|
||||
if (isset($config['get'])) {
|
||||
$mock->shouldReceive('get')
|
||||
->andReturn($config['get']);
|
||||
} else {
|
||||
// 기본: null 반환
|
||||
$mock->shouldReceive('get')
|
||||
->andReturn(null);
|
||||
}
|
||||
|
||||
// put
|
||||
if (isset($config['put'])) {
|
||||
$mock->shouldReceive('put')
|
||||
->andReturn($config['put']);
|
||||
} else {
|
||||
// 기본: true 반환
|
||||
$mock->shouldReceive('put')
|
||||
->andReturn(true);
|
||||
}
|
||||
|
||||
// delete
|
||||
if (isset($config['delete'])) {
|
||||
$mock->shouldReceive('delete')
|
||||
->andReturn($config['delete']);
|
||||
} else {
|
||||
$mock->shouldReceive('delete')
|
||||
->andReturn(true);
|
||||
}
|
||||
|
||||
// deleteDirectory
|
||||
if (isset($config['deleteDirectory'])) {
|
||||
$mock->shouldReceive('deleteDirectory')
|
||||
->andReturn($config['deleteDirectory']);
|
||||
} else {
|
||||
$mock->shouldReceive('deleteDirectory')
|
||||
->andReturn(true);
|
||||
}
|
||||
|
||||
// deleteAll
|
||||
if (isset($config['deleteAll'])) {
|
||||
$mock->shouldReceive('deleteAll')
|
||||
->andReturn($config['deleteAll']);
|
||||
} else {
|
||||
$mock->shouldReceive('deleteAll')
|
||||
->andReturn(true);
|
||||
}
|
||||
|
||||
// url
|
||||
if (isset($config['url'])) {
|
||||
$mock->shouldReceive('url')
|
||||
->andReturn($config['url']);
|
||||
} else {
|
||||
// 기본: null 반환 (private disk)
|
||||
$mock->shouldReceive('url')
|
||||
->andReturn(null);
|
||||
}
|
||||
|
||||
// files
|
||||
if (isset($config['files'])) {
|
||||
$mock->shouldReceive('files')
|
||||
->andReturn($config['files']);
|
||||
} else {
|
||||
$mock->shouldReceive('files')
|
||||
->andReturn([]);
|
||||
}
|
||||
|
||||
// getBasePath
|
||||
if (isset($config['getBasePath'])) {
|
||||
$mock->shouldReceive('getBasePath')
|
||||
->andReturn($config['getBasePath']);
|
||||
} else {
|
||||
// 자동 생성: storage/app/{type}/{identifier}/{category}
|
||||
$mock->shouldReceive('getBasePath')
|
||||
->andReturnUsing(function ($category) use ($type, $identifier) {
|
||||
return storage_path("app/{$type}/{$identifier}/{$category}");
|
||||
});
|
||||
}
|
||||
|
||||
return $mock;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Helpers;
|
||||
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Mockery;
|
||||
|
||||
/**
|
||||
* 확장 활성 디렉토리를 테스트 중 삭제로부터 보호하는 Trait
|
||||
*
|
||||
* ## File Facade Spy 패턴
|
||||
*
|
||||
* File 파사드의 deleteDirectory와 moveDirectory 메서드를 Mockery partial mock으로 감싸서,
|
||||
* 확장 활성 디렉토리 경로에 대한 삭제 및 이동 호출을 차단(no-op)합니다.
|
||||
* 나머지 모든 File 메서드와 다른 경로의 삭제/이동은 정상 동작합니다.
|
||||
*
|
||||
* moveDirectory 차단이 필요한 이유:
|
||||
* copyToActive()가 원자적 교체 시 활성 디렉토리를 _old_xxx로 rename한 뒤
|
||||
* _bundled 복사본으로 교체하는데, 이 rename이 차단되지 않으면
|
||||
* 테스트가 실제 활성 디렉토리를 파괴합니다.
|
||||
*
|
||||
* 확장 복사(copyToActive) 관련 테스트 작성/수정 시에도 이 패턴을 따라야 합니다:
|
||||
* - 실제 파일시스템 직접 조작 대신 File Facade를 통한 Mockery partial mock 사용
|
||||
* - File::getFacadeRoot() → Mockery::mock()->makePartial() → File::swap() 순서
|
||||
* - finally 블록에서 반드시 File::swap($original)로 원래 인스턴스 복원
|
||||
*
|
||||
* 차단 대상 경로 패턴: /(modules|plugins|templates)/[활성확장]
|
||||
* - modules/sirsoft-board → 차단 ✅
|
||||
* - plugins/sirsoft-daum_postcode → 차단 ✅
|
||||
* - templates/sirsoft-admin_basic → 차단 ✅
|
||||
* - modules/_bundled/sirsoft-board → 통과 (_ 접두사)
|
||||
* - modules/_pending/xxx → 통과 (_ 접두사)
|
||||
* - /tmp/any-other-path → 통과
|
||||
*
|
||||
* 사용법:
|
||||
* ```php
|
||||
* use Tests\Helpers\ProtectsExtensionDirectories;
|
||||
*
|
||||
* class MyTest extends TestCase
|
||||
* {
|
||||
* use ProtectsExtensionDirectories;
|
||||
*
|
||||
* protected function setUp(): void
|
||||
* {
|
||||
* parent::setUp();
|
||||
* // ... 정당한 파일 작업 (좀비 정리, 복사 등)
|
||||
* $this->setUpExtensionProtection();
|
||||
* // ... Manager 로드, DB 시딩 등
|
||||
* }
|
||||
*
|
||||
* protected function tearDown(): void
|
||||
* {
|
||||
* $this->tearDownExtensionProtection();
|
||||
* // ... 정리 작업
|
||||
* parent::tearDown();
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
trait ProtectsExtensionDirectories
|
||||
{
|
||||
private Filesystem $originalFilesystem;
|
||||
|
||||
/**
|
||||
* File 파사드 spy를 설정하여 확장 활성 디렉토리 삭제를 차단합니다.
|
||||
*
|
||||
* 차단 대상: modules/xxx, plugins/xxx, templates/xxx
|
||||
* (xxx가 _ 또는 . 으로 시작하지 않는 경우)
|
||||
* 통과 대상: modules/_bundled/xxx, modules/_pending/xxx 등
|
||||
*/
|
||||
protected function setUpExtensionProtection(): void
|
||||
{
|
||||
$this->originalFilesystem = File::getFacadeRoot();
|
||||
|
||||
$mock = Mockery::mock($this->originalFilesystem)->makePartial();
|
||||
|
||||
// 확장 활성 디렉토리 삭제 차단
|
||||
$mock->shouldReceive('deleteDirectory')
|
||||
->zeroOrMoreTimes()
|
||||
->andReturnUsing(function (string $directory, bool $preserve = false) {
|
||||
$normalized = str_replace('\\', '/', $directory);
|
||||
|
||||
if (preg_match('#/(modules|plugins|templates)/(?![\._])[^/]+$#', $normalized)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->originalFilesystem->deleteDirectory($directory, $preserve);
|
||||
});
|
||||
|
||||
// 확장 활성 디렉토리 이동(rename) 차단
|
||||
// copyToActive()의 원자적 교체가 활성 디렉토리를 _old_xxx로 rename하는 것을 방지
|
||||
$mock->shouldReceive('moveDirectory')
|
||||
->zeroOrMoreTimes()
|
||||
->andReturnUsing(function (string $from, string $to, bool $overwrite = false) {
|
||||
$normalized = str_replace('\\', '/', $from);
|
||||
|
||||
// 활성 디렉토리에서 밖으로 이동하는 것 차단 (예: templates/sirsoft-xxx → _pending/sirsoft-xxx_old_xxx)
|
||||
if (preg_match('#/(modules|plugins|templates)/(?![\._])[^/]+$#', $normalized)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->originalFilesystem->moveDirectory($from, $to, $overwrite);
|
||||
});
|
||||
|
||||
File::swap($mock);
|
||||
}
|
||||
|
||||
/**
|
||||
* File 파사드를 원래 인스턴스로 복원합니다.
|
||||
*/
|
||||
protected function tearDownExtensionProtection(): void
|
||||
{
|
||||
File::swap($this->originalFilesystem);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user