Skip to content
Merged
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<php>
<ini name="memory_limit" value="2048M"/>
<env name="APP_ENV" value="testing"/>
<env name="APP_DEBUG" value="true"/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
Expand Down
16 changes: 15 additions & 1 deletion src/CloudServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace CraftCms\Cloud;

use Bref\LaravelBridge\BrefServiceProvider;
use CraftCms\Cms\Config\GeneralConfig;
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;

class CloudServiceProvider extends ServiceProvider
{
public function register(): void
{
// Craft Cloud sets CRAFT_CLOUD on every function
if (! filter_var($_SERVER['CRAFT_CLOUD'] ?? false, FILTER_VALIDATE_BOOL)) {
return;
}
Expand All @@ -21,6 +22,7 @@ public function register(): void
$this->app->register(BrefServiceProvider::class);

$this->configureCache();
$this->configureCraft();
$this->configureLogging();
$this->configureSession();
}
Expand All @@ -36,6 +38,18 @@ private function configureLogging(): void
Config::set('logging.default', 'stderr');
}

private function configureCraft(): void
{
$this->callAfterResolving(GeneralConfig::class, function (GeneralConfig $config): void {
$config->allowUpdates(false);
$config->ideHelperEnabled(false);
$config->backups->command(false)->restoreCommand(false)->onUpdate(false);
$config->queue->runAutomatically(false);
});

$this->callAfterResolving(ProjectConfig::class, fn (ProjectConfig $projectConfig) => $projectConfig->writeYamlAutomatically(false));
}

private function configureSession(): void
{
Config::set('session.driver', 'database');
Expand Down
38 changes: 37 additions & 1 deletion tests/Feature/CloudServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use Bref\LaravelBridge\BrefServiceProvider;
use CraftCms\Cms\Cms;
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use Illuminate\Cache\Events\CacheFailedOver;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
Expand All @@ -20,16 +22,32 @@
'cache.default' => 'file',
'logging.default' => 'daily',
'session.driver' => 'file',
])->and(craftSettings())->toBe([
'allowUpdates' => true,
'ideHelperEnabled' => true,
'backups.command' => null,
'backups.restoreCommand' => null,
'backups.onUpdate' => true,
'queue.runAutomatically' => true,
'projectConfig.writeYamlAutomatically' => true,
]);
});

it('overrides the cache, logs and sessions on Craft Cloud', function (): void {
it('overrides the cache, logs, sessions and Craft settings on Craft Cloud', function (): void {
$this->registerCloud(onCloud: true);

expect(config()->getMany(['cache.default', 'logging.default', 'session.driver']))->toBe([
'cache.default' => 'failover',
'logging.default' => 'stderr',
'session.driver' => 'database',
])->and(craftSettings())->toBe([
'allowUpdates' => false,
'ideHelperEnabled' => false,
'backups.command' => false,
'backups.restoreCommand' => false,
'backups.onUpdate' => false,
'queue.runAutomatically' => false,
'projectConfig.writeYamlAutomatically' => false,
]);
});

Expand All @@ -49,3 +67,21 @@
expect($this->app->getProvider(BrefServiceProvider::class) !== null)->toBe($onCloud)
->and($this->app->getProvider('Bref\\LaravelHealthCheck\\BrefHealthCheckServiceProvider'))->toBeNull();
})->with(['on Craft Cloud' => true, 'off Craft Cloud' => false]);

/**
* @return array<string, mixed>
*/
function craftSettings(): array
{
$config = Cms::config();

return [
'allowUpdates' => $config->allowUpdates,
'ideHelperEnabled' => $config->ideHelperEnabled,
'backups.command' => $config->backups->command,
'backups.restoreCommand' => $config->backups->restoreCommand,
'backups.onUpdate' => $config->backups->onUpdate,
'queue.runAutomatically' => $config->queue->runAutomatically,
'projectConfig.writeYamlAutomatically' => app(ProjectConfig::class)->writeYamlAutomatically,
];
}
79 changes: 79 additions & 0 deletions tests/Feature/QueueWorkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

use Aws\CommandInterface;
use Aws\MockHandler;
use Aws\Result;
use Bref\Context\Context;
use Bref\Event\Sqs\SqsEvent;
use Bref\LaravelBridge\Queue\QueueHandler;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

const QUEUE_URL = 'https://sqs.us-east-1.amazonaws.com/123456789012/jobs';

final class RecordingJob implements ShouldQueue
{
use Dispatchable;

/** @var list<string> */
public static array $handled = [];

public function __construct(public string $value) {}

public function handle(): void
{
self::$handled[] = $this->value;
}
}

beforeEach(function (): void {
$_SERVER['LAMBDA_TASK_ROOT'] = base_path();
$_SERVER['QUEUE_CONNECTION'] = 'sqs';
$_SERVER['SQS_QUEUE'] = QUEUE_URL;
$this->refreshApplication();

RecordingJob::$handled = [];
$this->sqsCalls = [];
$sqs = new MockHandler;
foreach (['SendMessage' => ['MessageId' => 'message-1'], 'DeleteMessage' => []] as $result) {
$sqs->append(function (CommandInterface $command) use ($result): Result {
$this->sqsCalls[] = $command;

return new Result($result);
});
}

config([
'queue.connections.sqs.key' => 'key',
'queue.connections.sqs.secret' => 'secret',
'queue.connections.sqs.handler' => $sqs,
'queue.failed.driver' => 'null',
'bref.log_jobs' => false,
]);
});

afterEach(function (): void {
unset($_SERVER['LAMBDA_TASK_ROOT'], $_SERVER['QUEUE_CONNECTION'], $_SERVER['SQS_QUEUE']);
});

it("runs Craft's queued jobs with Bref's QueueHandler on Craft Cloud", function (): void {
$this->registerCloud(onCloud: true);

RecordingJob::dispatch('hello');

$this->app->make(QueueHandler::class)->handleSqs(new SqsEvent(['Records' => [[
'eventSource' => 'aws:sqs',
'eventSourceARN' => 'arn:aws:sqs:us-east-1:123456789012:jobs',
'messageId' => 'message-1',
'receiptHandle' => 'receipt-1',
'body' => $this->sqsCalls[0]['MessageBody'],
'attributes' => ['ApproximateReceiveCount' => '1'],
'messageAttributes' => [],
]]]), new Context('request-1', (time() + 60) * 1000, 'arn', 'trace'));

expect(RecordingJob::$handled)->toBe(['hello'])
->and($this->sqsCalls[0]['QueueUrl'])->toBe(QUEUE_URL)
->and($this->sqsCalls[1]->getName())->toBe('DeleteMessage');
});