mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat: adds shell
This commit is contained in:
@ -10,6 +10,7 @@ use Pest\Preset;
|
||||
use Pest\Support\ChainableClosure;
|
||||
use Pest\Support\ExceptionTrace;
|
||||
use Pest\Support\Reflection;
|
||||
use Pest\Support\Shell;
|
||||
use Pest\TestSuite;
|
||||
use PHPUnit\Framework\Attributes\PostCondition;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -477,4 +478,12 @@ trait Testable
|
||||
'notes' => self::$__latestNotes,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a shell for the test case.
|
||||
*/
|
||||
public function shell(): void
|
||||
{
|
||||
Shell::open();
|
||||
}
|
||||
}
|
||||
|
||||
99
src/Support/Shell.php
Normal file
99
src/Support/Shell.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Support;
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Illuminate\Support\Env;
|
||||
use Laravel\Tinker\ClassAliasAutoloader;
|
||||
use Psy\Configuration;
|
||||
use Psy\Shell as PsyShell;
|
||||
use Psy\VersionUpdater\Checker;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class Shell
|
||||
{
|
||||
/**
|
||||
* Creates a new interactive shell.
|
||||
*/
|
||||
public static function open(): void
|
||||
{
|
||||
$config = new Configuration;
|
||||
|
||||
$config->setUpdateCheck(Checker::NEVER);
|
||||
|
||||
$config->getPresenter()->addCasters(self::casters());
|
||||
|
||||
$shell = new PsyShell($config);
|
||||
|
||||
$loader = self::tinkered($shell);
|
||||
|
||||
try {
|
||||
$shell->run();
|
||||
} finally {
|
||||
$loader?->unregister();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the casters for the Psy Shell.
|
||||
*
|
||||
* @return array<string, callable>
|
||||
*/
|
||||
private static function casters(): array
|
||||
{
|
||||
$casters = [
|
||||
'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
|
||||
'Illuminate\Support\HtmlString' => 'Laravel\Tinker\TinkerCaster::castHtmlString',
|
||||
'Illuminate\Support\Stringable' => 'Laravel\Tinker\TinkerCaster::castStringable',
|
||||
];
|
||||
|
||||
if (class_exists('Illuminate\Database\Eloquent\Model')) {
|
||||
$casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
|
||||
}
|
||||
|
||||
if (class_exists('Illuminate\Process\ProcessResult')) {
|
||||
$casters['Illuminate\Process\ProcessResult'] = 'Laravel\Tinker\TinkerCaster::castProcessResult';
|
||||
}
|
||||
|
||||
if (class_exists('Illuminate\Foundation\Application')) {
|
||||
$casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
|
||||
}
|
||||
|
||||
if (function_exists('app') === false) {
|
||||
return $casters; // @phpstan-ignore-line
|
||||
}
|
||||
|
||||
$config = app()->make('config');
|
||||
|
||||
return array_merge($casters, (array) $config->get('tinker.casters', []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tinkers the current shell, if the Tinker package is available.
|
||||
*/
|
||||
private static function tinkered(PsyShell $shell): ?ClassLoader
|
||||
{
|
||||
if (function_exists('app') === false
|
||||
|| ! class_exists(Env::class)
|
||||
|| ! class_exists(ClassAliasAutoloader::class)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$path = Env::get('COMPOSER_VENDOR_DIR', app()->basePath().DIRECTORY_SEPARATOR.'vendor');
|
||||
|
||||
$path .= '/composer/autoload_classmap.php';
|
||||
|
||||
$config = app()->make('config');
|
||||
|
||||
$loader = ClassAliasAutoloader::register(
|
||||
$shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', [])
|
||||
);
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user