diff --git a/src/actions/InstallCraft.php b/src/actions/InstallCraft.php new file mode 100644 index 0000000..a932c0e --- /dev/null +++ b/src/actions/InstallCraft.php @@ -0,0 +1,59 @@ + App::env('CRAFT_INSTALL_USERNAME') ?? 'user@example.com', + 'email' => App::env('CRAFT_INSTALL_EMAIL') ?? 'user@example.com', + 'password' => App::env('CRAFT_INSTALL_PASSWORD') ?? 'secret', + 'interactive' => App::env('CRAFT_INSTALL_INTERACTIVE') ?? '0', + ]; + + if (! file_exists(Craft::getAlias('@config/project/project.yaml'))) { + $args = array_merge($args, [ + 'siteName' => App::env('CRAFT_INSTALL_SITENAME') ?? '"Craft CMS"', + 'siteUrl' => App::env('CRAFT_INSTALL_SITEURL') ?? 'http://localhost:8080', + 'language' => App::env('CRAFT_INSTALL_LANGUAGE') ?? 'en-US', + ]); + } + + $getConfig = function (string $path) { + if (file_exists($path)) { + return require $path; + } + + return []; + }; + + $config = ArrayHelper::merge( + $getConfig(Craft::$app->getPath()->getVendorPath().'/craftcms/cms/src/config/app.php'), + $getConfig(Craft::$app->getPath()->getVendorPath().'/craftcms/cms/src/config/app.console.php'), + $getConfig(Craft::$app->getPath()->getConfigPath().'/app.php'), + $getConfig(Craft::$app->getPath()->getConfigPath().'/app.console.php'), + [ + 'id' => 'CraftCMSConsole', + 'name' => 'Craft CMS Console', + 'components' => [ + 'config' => Craft::$app->getConfig(), + ], + ], + ); + unset($config['class']); + + Craft::createObject(InstallController::class, [ + 'id' => 'foo', + 'module' => new Application($config), + 'config' => $args, + ])->runAction('craft'); + } +} diff --git a/src/test/TestCase.php b/src/test/TestCase.php index d8d6fa7..cbedacf 100644 --- a/src/test/TestCase.php +++ b/src/test/TestCase.php @@ -3,11 +3,12 @@ namespace markhuot\craftpest\test; use Craft; -use craft\helpers\App; use Illuminate\Support\Collection; use markhuot\craftpest\actions\CallSeeders; +use markhuot\craftpest\actions\InstallCraft; use markhuot\craftpest\interfaces\RenderCompiledClassesInterface; use Symfony\Component\Process\Process; +use function markhuot\craftpest\helpers\base\service; class TestCase extends \PHPUnit\Framework\TestCase { @@ -106,38 +107,7 @@ public function createApplication() protected function craftInstall() { - $args = [ - '--username='.(App::env('CRAFT_INSTALL_USERNAME') ?? 'user@example.com'), - '--email='.(App::env('CRAFT_INSTALL_EMAIL') ?? 'user@example.com'), - '--password='.(App::env('CRAFT_INSTALL_PASSWORD') ?? 'secret'), - '--interactive='.(App::env('CRAFT_INSTALL_INTERACTIVE') ?? '0'), - ]; - - if (! file_exists(Craft::getAlias('@config/project/project.yaml'))) { - $args = array_merge($args, [ - '--siteName='.(App::env('CRAFT_INSTALL_SITENAME') ?? '"Craft CMS"'), - '--siteUrl='.(App::env('CRAFT_INSTALL_SITEURL') ?? 'http://localhost:8080'), - '--language='.(App::env('CRAFT_INSTALL_LANGUAGE') ?? 'en-US'), - ]); - } - - $craftExePath = getenv('CRAFT_EXE_PATH') ?: './craft'; - $process = new Process([$craftExePath, 'install', ...$args]); - $process->setTty(Process::isTtySupported()); - $process->setTimeout(null); - $process->start(); - - foreach ($process as $type => $data) { - if ($type === $process::OUT) { - echo $data; - } else { - echo $data; - } - } - - if (! $process->isSuccessful()) { - throw new \Exception('Failed installing Craft'); - } + service(InstallCraft::class)(); } protected function craftMigrateAll()