From 246c363f25afbf108727171156acdd02ab912ec1 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 11 Dec 2024 22:07:44 +0800 Subject: [PATCH 1/4] wip Signed-off-by: Mior Muhammad Zaki --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b8c2a9db..a32c542b 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "mockery/mockery": "^1.5.1", "phpstan/phpstan": "^2.0", "phpunit/phpunit": "^9.5.10", - "spatie/laravel-ray": "^1.32.4", + "spatie/laravel-ray": "^1.39", "symfony/process": "^6.0.9", "symfony/yaml": "^6.0.9", "vlucas/phpdotenv": "^5.4.1" From cdcc614fe4cabc77b8cc8e4b50ea40c489e7b7a8 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 11 Dec 2024 22:46:55 +0800 Subject: [PATCH 2/4] wip Signed-off-by: Mior Muhammad Zaki --- src/Foundation/Console/Actions/Action.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Foundation/Console/Actions/Action.php b/src/Foundation/Console/Actions/Action.php index 0786a4ea..4b56eb13 100644 --- a/src/Foundation/Console/Actions/Action.php +++ b/src/Foundation/Console/Actions/Action.php @@ -30,7 +30,7 @@ protected function pathLocation(string $path): string $path = str_replace(rtrim($this->workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, '', $path); $prefix = match ($this->workingPath) { - app()->basePath() => '@laravel', + base_path() => '@laravel', $packagePath => '.', default => '@' }; From 86dbc7b3f445f0f81b6fad0547aa03cfc4967cb0 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 12 Dec 2024 10:52:06 +0800 Subject: [PATCH 3/4] [7.x] Add `Orchestra\Testbench\transform_realpath_to_relative` function (#276) * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- src/Foundation/Console/Actions/Action.php | 35 ++++----------- .../Console/Actions/DeleteDirectories.php | 12 +++--- .../Console/Actions/DeleteFiles.php | 12 +++--- .../Console/Actions/EnsureDirectoryExists.php | 11 +++-- .../Console/Actions/GeneratesFile.php | 14 +++--- .../Console/CreateSqliteDbCommand.php | 1 - .../Console/DropSqliteDbCommand.php | 1 - .../Console/VendorPublishCommand.php | 32 +++----------- src/functions.php | 30 +++++++++++++ .../TransformRealpathToRelativeTest.php | 43 +++++++++++++++++++ tests/Helpers/TransformRelativePathTest.php | 5 +-- 11 files changed, 113 insertions(+), 83 deletions(-) create mode 100644 tests/Helpers/TransformRealpathToRelativeTest.php diff --git a/src/Foundation/Console/Actions/Action.php b/src/Foundation/Console/Actions/Action.php index 4b56eb13..eea21284 100644 --- a/src/Foundation/Console/Actions/Action.php +++ b/src/Foundation/Console/Actions/Action.php @@ -2,46 +2,27 @@ namespace Orchestra\Testbench\Foundation\Console\Actions; -use function Orchestra\Testbench\package_path; +use function Orchestra\Testbench\transform_realpath_to_relative; /** * @api */ abstract class Action { - /** - * Working path for the action. - * - * @var string|null - */ - public $workingPath; - /** * Normalise file location. * * @param string $path * @return string + * + * @deprecated + * + * @codeCoverageIgnore */ protected function pathLocation(string $path): string { - $packagePath = package_path(); - - if (! \is_null($this->workingPath)) { - $path = str_replace(rtrim($this->workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, '', $path); - - $prefix = match ($this->workingPath) { - base_path() => '@laravel', - $packagePath => '.', - default => '@' - }; - - return implode('/', [$prefix, ltrim($path, '/')]); - } - - if (str_starts_with($path, $packagePath)) { - return \sprintf('./%s', ltrim(str_replace($packagePath, '', $path), '/')); - } - - return $path; + return transform_realpath_to_relative( + $path, property_exists($this, 'workingPath') ? $this->workingPath : null + ); } } diff --git a/src/Foundation/Console/Actions/DeleteDirectories.php b/src/Foundation/Console/Actions/DeleteDirectories.php index 22f33c1b..6b390d9a 100644 --- a/src/Foundation/Console/Actions/DeleteDirectories.php +++ b/src/Foundation/Console/Actions/DeleteDirectories.php @@ -6,6 +6,8 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\LazyCollection; +use function Orchestra\Testbench\transform_realpath_to_relative; + /** * @api */ @@ -21,10 +23,8 @@ class DeleteDirectories extends Action public function __construct( public Filesystem $filesystem, public ?ComponentsFactory $components = null, - ?string $workingPath = null - ) { - $this->workingPath = $workingPath; - } + public ?string $workingPath = null + ) {} /** * Handle the action. @@ -38,7 +38,7 @@ public function handle(iterable $directories): void ->each(function ($directory) { if (! $this->filesystem->isDirectory($directory)) { $this->components?->twoColumnDetail( - \sprintf('Directory [%s] doesn\'t exists', $this->pathLocation($directory)), + \sprintf('Directory [%s] doesn\'t exists', transform_realpath_to_relative($directory, $this->workingPath)), 'SKIPPED' ); @@ -48,7 +48,7 @@ public function handle(iterable $directories): void $this->filesystem->deleteDirectory($directory); $this->components?->task( - \sprintf('Directory [%s] has been deleted', $this->pathLocation($directory)) + \sprintf('Directory [%s] has been deleted', transform_realpath_to_relative($directory, $this->workingPath)) ); }); } diff --git a/src/Foundation/Console/Actions/DeleteFiles.php b/src/Foundation/Console/Actions/DeleteFiles.php index 71fdc5b9..e21eefeb 100644 --- a/src/Foundation/Console/Actions/DeleteFiles.php +++ b/src/Foundation/Console/Actions/DeleteFiles.php @@ -6,6 +6,8 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\LazyCollection; +use function Orchestra\Testbench\transform_realpath_to_relative; + /** * @api */ @@ -21,10 +23,8 @@ class DeleteFiles extends Action public function __construct( public Filesystem $filesystem, public ?ComponentsFactory $components = null, - ?string $workingPath = null - ) { - $this->workingPath = $workingPath; - } + public ?string $workingPath = null + ) {} /** * Handle the action. @@ -39,7 +39,7 @@ public function handle(iterable $files): void ->each(function ($file) { if (! $this->filesystem->exists($file)) { $this->components?->twoColumnDetail( - \sprintf('File [%s] doesn\'t exists', $this->pathLocation($file)), + \sprintf('File [%s] doesn\'t exists', transform_realpath_to_relative($file, $this->workingPath)), 'SKIPPED' ); @@ -49,7 +49,7 @@ public function handle(iterable $files): void $this->filesystem->delete($file); $this->components?->task( - \sprintf('File [%s] has been deleted', $this->pathLocation($file)) + \sprintf('File [%s] has been deleted', transform_realpath_to_relative($file, $this->workingPath)) ); }); } diff --git a/src/Foundation/Console/Actions/EnsureDirectoryExists.php b/src/Foundation/Console/Actions/EnsureDirectoryExists.php index 407909da..d4c7e16b 100644 --- a/src/Foundation/Console/Actions/EnsureDirectoryExists.php +++ b/src/Foundation/Console/Actions/EnsureDirectoryExists.php @@ -7,6 +7,7 @@ use Illuminate\Support\LazyCollection; use function Orchestra\Testbench\join_paths; +use function Orchestra\Testbench\transform_realpath_to_relative; /** * @api @@ -23,10 +24,8 @@ class EnsureDirectoryExists extends Action public function __construct( public Filesystem $filesystem, public ?ComponentsFactory $components = null, - ?string $workingPath = null - ) { - $this->workingPath = $workingPath; - } + public ?string $workingPath = null + ) {} /** * Handle the action. @@ -40,7 +39,7 @@ public function handle(iterable $directories): void ->each(function ($directory) { if ($this->filesystem->isDirectory($directory)) { $this->components?->twoColumnDetail( - \sprintf('Directory [%s] already exists', $this->pathLocation($directory)), + \sprintf('Directory [%s] already exists', transform_realpath_to_relative($directory, $this->workingPath)), 'SKIPPED' ); @@ -50,7 +49,7 @@ public function handle(iterable $directories): void $this->filesystem->ensureDirectoryExists($directory, 0755, true); $this->filesystem->copy((string) realpath(join_paths(__DIR__, 'stubs', '.gitkeep')), join_paths($directory, '.gitkeep')); - $this->components?->task(\sprintf('Prepare [%s] directory', $this->pathLocation($directory))); + $this->components?->task(\sprintf('Prepare [%s] directory', transform_realpath_to_relative($directory, $this->workingPath))); }); } } diff --git a/src/Foundation/Console/Actions/GeneratesFile.php b/src/Foundation/Console/Actions/GeneratesFile.php index 031caa09..5d07f07d 100644 --- a/src/Foundation/Console/Actions/GeneratesFile.php +++ b/src/Foundation/Console/Actions/GeneratesFile.php @@ -5,6 +5,8 @@ use Illuminate\Console\View\Components\Factory as ComponentsFactory; use Illuminate\Filesystem\Filesystem; +use function Orchestra\Testbench\transform_realpath_to_relative; + /** * @api */ @@ -22,10 +24,8 @@ public function __construct( public Filesystem $filesystem, public ?ComponentsFactory $components = null, public bool $force = false, - ?string $workingPath = null - ) { - $this->workingPath = $workingPath; - } + public ?string $workingPath = null + ) {} /** * Handle the action. @@ -42,7 +42,7 @@ public function handle($from, $to): void if (! $this->filesystem->exists($from)) { $this->components?->twoColumnDetail( - \sprintf('Source file [%s] doesn\'t exists', $this->pathLocation($from)), + \sprintf('Source file [%s] doesn\'t exists', transform_realpath_to_relative($from, $this->workingPath)), 'SKIPPED' ); @@ -51,7 +51,7 @@ public function handle($from, $to): void if (! $this->force && $this->filesystem->exists($to)) { $this->components?->twoColumnDetail( - \sprintf('File [%s] already exists', $this->pathLocation($to)), + \sprintf('File [%s] already exists', transform_realpath_to_relative($to, $this->workingPath)), 'SKIPPED' ); @@ -67,7 +67,7 @@ public function handle($from, $to): void } $this->components?->task( - \sprintf('File [%s] generated', $this->pathLocation($to)) + \sprintf('File [%s] generated', transform_realpath_to_relative($to, $this->workingPath)) ); } } diff --git a/src/Foundation/Console/CreateSqliteDbCommand.php b/src/Foundation/Console/CreateSqliteDbCommand.php index 7a77963b..b4ad3e80 100644 --- a/src/Foundation/Console/CreateSqliteDbCommand.php +++ b/src/Foundation/Console/CreateSqliteDbCommand.php @@ -47,7 +47,6 @@ public function handle(Filesystem $filesystem) filesystem: $filesystem, components: $this->components, force: $force, - workingPath: $workingPath, ))->handle($from, $to); return Command::SUCCESS; diff --git a/src/Foundation/Console/DropSqliteDbCommand.php b/src/Foundation/Console/DropSqliteDbCommand.php index ba11576c..7f81239d 100644 --- a/src/Foundation/Console/DropSqliteDbCommand.php +++ b/src/Foundation/Console/DropSqliteDbCommand.php @@ -35,7 +35,6 @@ public function handle(Filesystem $filesystem) (new Actions\DeleteFiles( filesystem: $filesystem, components: $this->components, - workingPath: $workingPath, ))->handle( match ($this->option('all')) { true => [...$filesystem->glob(join_paths($databasePath, '*.sqlite'))], diff --git a/src/Foundation/Console/VendorPublishCommand.php b/src/Foundation/Console/VendorPublishCommand.php index be51876d..51eaa5b5 100644 --- a/src/Foundation/Console/VendorPublishCommand.php +++ b/src/Foundation/Console/VendorPublishCommand.php @@ -4,7 +4,7 @@ use Illuminate\Foundation\Console\VendorPublishCommand as Command; -use function Orchestra\Testbench\package_path; +use function Orchestra\Testbench\transform_realpath_to_relative; /** * @codeCoverageIgnore @@ -15,37 +15,19 @@ class VendorPublishCommand extends Command #[\Override] protected function status($from, $to, $type) { - $laravelPath = base_path(); - $packagePath = package_path(); - - $pathLocation = function ($path) use ($laravelPath, $packagePath) { - $path = (string) realpath($path); - + $format = function ($path) use ($type) { return match (true) { - str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path), - str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path), - default => $path, + $type === 'directory' && is_link($path) => $path, + $this->files->exists($path) => $path, + default => (string) realpath($path), }; }; - $fromLocation = $pathLocation($from); - $toLocation = $pathLocation($to); - - if ( - $type === 'directory' && - $fromLocation === $toLocation && - is_link($to) - ) { - $this->components->task('Synced directory'); - - return; - } - $this->components->task(\sprintf( 'Copying %s [%s] to [%s]', $type, - $fromLocation, - $toLocation, + transform_realpath_to_relative($format($from)), + transform_realpath_to_relative($format($to)), )); } } diff --git a/src/functions.php b/src/functions.php index c7f11fa5..63bafc12 100644 --- a/src/functions.php +++ b/src/functions.php @@ -219,6 +219,36 @@ function refresh_router_lookups(Router $router): void $router->getRoutes()->refreshNameLookups(); } +/** + * Transform realpath to alias path. + * + * @api + * + * @param string $path + * @param string|null $workingPath + * @return string + */ +function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string +{ + $separator = DIRECTORY_SEPARATOR; + + if (! \is_null($workingPath)) { + return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path); + } + + $laravelPath = base_path(); + $workbenchPath = workbench_path(); + $packagePath = package_path(); + + return match (true) { + str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path), + str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path), + str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path), + ! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]), + default => $path, + }; +} + /** * Transform relative path. * diff --git a/tests/Helpers/TransformRealpathToRelativeTest.php b/tests/Helpers/TransformRealpathToRelativeTest.php new file mode 100644 index 00000000..22c0097b --- /dev/null +++ b/tests/Helpers/TransformRealpathToRelativeTest.php @@ -0,0 +1,43 @@ +assertSame('Testbench.php', transform_realpath_to_relative('Testbench.php')); + + $this->assertSame( + join_paths('.', 'src', 'TestCase.php'), + transform_realpath_to_relative(package_path('src', 'TestCase.php')) + ); + + $this->assertSame( + join_paths('@laravel', 'composer.json'), + transform_realpath_to_relative(default_skeleton_path('composer.json')) + ); + + $this->assertSame( + join_paths('@workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'), + transform_realpath_to_relative(package_path('workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php')) + ); + } + + /** @test */ + public function it_can_use_transform_realpath_to_relative_using_custom_working_path() + { + $this->assertSame( + join_paths('@tests', 'Helpers', 'TransformRealpathToRelativeTest.php'), + transform_realpath_to_relative(__FILE__, package_path('tests'), '@tests') + ); + } +} diff --git a/tests/Helpers/TransformRelativePathTest.php b/tests/Helpers/TransformRelativePathTest.php index d014d056..afae4508 100644 --- a/tests/Helpers/TransformRelativePathTest.php +++ b/tests/Helpers/TransformRelativePathTest.php @@ -11,9 +11,6 @@ class TransformRelativePathTest extends TestCase /** @test */ public function it_can_use_transform_relative_path() { - $this->assertSame( - realpath(__DIR__.DIRECTORY_SEPARATOR.'TransformRelativePathTest.php'), - transform_relative_path('./TransformRelativePathTest.php', realpath(__DIR__)) - ); + $this->assertSame(__FILE__, transform_relative_path('./TransformRelativePathTest.php', __DIR__)); } } From 6dfb707fde4c3ef76c7a0b37d957fcff1e6a4b88 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 12 Dec 2024 11:43:16 +0800 Subject: [PATCH 4/4] [7.x] Test Improvements (#277) * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- .../Console/SyncSkeletonCommand.php | 2 +- src/Foundation/Console/TerminatingConsole.php | 7 ++- .../Console/TerminatingConsoleTest.php | 43 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/Foundation/Console/TerminatingConsoleTest.php diff --git a/src/Foundation/Console/SyncSkeletonCommand.php b/src/Foundation/Console/SyncSkeletonCommand.php index bd94b625..797127bf 100644 --- a/src/Foundation/Console/SyncSkeletonCommand.php +++ b/src/Foundation/Console/SyncSkeletonCommand.php @@ -32,7 +32,7 @@ protected function configure() { parent::configure(); - TerminatingConsole::purge(); + TerminatingConsole::flush(); } /** diff --git a/src/Foundation/Console/TerminatingConsole.php b/src/Foundation/Console/TerminatingConsole.php index ea765a9b..e47d680b 100644 --- a/src/Foundation/Console/TerminatingConsole.php +++ b/src/Foundation/Console/TerminatingConsole.php @@ -4,6 +4,9 @@ use Illuminate\Support\Collection; +/** + * @internal + */ final class TerminatingConsole { /** @@ -50,7 +53,7 @@ public static function handle(): void \call_user_func($callback); }); - self::purge(); + self::flush(); } /** @@ -58,7 +61,7 @@ public static function handle(): void * * @return void */ - public static function purge(): void + public static function flush(): void { self::$beforeTerminatingCallbacks = []; } diff --git a/tests/Foundation/Console/TerminatingConsoleTest.php b/tests/Foundation/Console/TerminatingConsoleTest.php new file mode 100644 index 00000000..754b9286 --- /dev/null +++ b/tests/Foundation/Console/TerminatingConsoleTest.php @@ -0,0 +1,43 @@ +assertFalse(isset($_SERVER['TerminatingConsole.before'])); + $this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenTrue'])); + $this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenFalse'])); + + TerminatingConsole::before(function () { + $_SERVER['TerminatingConsole.before'] = true; + }); + + TerminatingConsole::beforeWhen(true, function () { + $_SERVER['TerminatingConsole.beforeWhenTrue'] = true; + }); + + TerminatingConsole::beforeWhen(false, function () { + $_SERVER['TerminatingConsole.beforeWhenFalse'] = true; + }); + + TerminatingConsole::handle(); + + $this->assertTrue(isset($_SERVER['TerminatingConsole.before'])); + $this->assertTrue(isset($_SERVER['TerminatingConsole.beforeWhenTrue'])); + $this->assertFalse(isset($_SERVER['TerminatingConsole.beforeWhenFalse'])); + + unset( + $_SERVER['TerminatingConsole.before'], + $_SERVER['TerminatingConsole.beforeWhenTrue'], + $_SERVER['TerminatingConsole.beforeWhenFalse'], + ); + + TerminatingConsole::flush(); + } +}