Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install craft in the same process so ENV vars are carried through #38

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/actions/InstallCraft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace markhuot\craftpest\actions;

use Craft;
use craft\console\Application;
use craft\console\controllers\InstallController;
use craft\helpers\App;
use craft\helpers\ArrayHelper;

class InstallCraft
{
public function __invoke(): void
{
$args = [
'username' => App::env('CRAFT_INSTALL_USERNAME') ?? '[email protected]',
'email' => App::env('CRAFT_INSTALL_EMAIL') ?? '[email protected]',
'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');
}
}
37 changes: 4 additions & 33 deletions src/test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
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
{
use ActingAs,
Expand Down Expand Up @@ -106,38 +108,7 @@ public function createApplication()

protected function craftInstall()
{
$args = [
'--username='.(App::env('CRAFT_INSTALL_USERNAME') ?? '[email protected]'),
'--email='.(App::env('CRAFT_INSTALL_EMAIL') ?? '[email protected]'),
'--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()
Expand Down
7 changes: 7 additions & 0 deletions tests/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
->text->toBe(['one', 'two', 'three']);
});

it('can count selectors', function () {
get('/selectors')
->expectSelector('#first ul li')
->toHaveCount(3)
->text->toBe(['one', 'two', 'three']);
});

it('selects multiple matching nodes via assertion API', function () {
get('/selectors')
->querySelector('#first ul li')
Expand Down
Loading