Skip to content

Commit

Permalink
Refactor some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 15, 2024
1 parent 6cd00c4 commit e3f72ad
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 45 deletions.
64 changes: 40 additions & 24 deletions src/Support/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Autoloader

protected string $appNamespace;

protected array $discoveredCommands = [];
protected array $registeredCommands = [];

protected array $discoveredProviders = [];
protected array $registeredProviders = [];

protected bool $isBooted = false;

Expand All @@ -37,26 +37,37 @@ public function __construct()
$this->appNamespace = $this->resolveAppNamespace();
}

public function boot(): void
public function boot()
{
// if ($this->isBooted) {
// // dump('Autoloader Already booted');
// return;
// }

if (! config()->has('ddd.autoload')) {
return;
return $this;
}

// if ($this->isBooted) {
// return $this;
// }

$this
->when(config('ddd.autoload.providers') === true, fn ($autoloader) => $autoloader->handleProviders())
->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn ($autoloader) => $autoloader->handleCommands())
->when(config('ddd.autoload.policies') === true, fn ($autoloader) => $autoloader->handlePolicies())
->when(config('ddd.autoload.factories') === true, fn ($autoloader) => $autoloader->handleFactories());
->when(config('ddd.autoload.providers') === true, fn () => $this->handleProviders())
->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn () => $this->handleCommands())
->when(config('ddd.autoload.policies') === true, fn () => $this->handlePolicies())
->when(config('ddd.autoload.factories') === true, fn () => $this->handleFactories());

if (app()->runningInConsole()) {
ConsoleApplication::starting(function ($artisan) {
foreach ($this->registeredCommands as $command) {
$artisan->resolve($command);
}
});
}

$this->isBooted = true;

return $this;
}

public function run() {}

protected function normalizePaths($path): array
{
return collect($path)
Expand Down Expand Up @@ -86,8 +97,10 @@ protected function handleProviders()
? DomainCache::get('domain-providers')
: $this->discoverProviders();

$this->registeredProviders = [];

foreach ($providers as $provider) {
$this->discoveredProviders[$provider] = $provider;
$this->registeredProviders[$provider] = $provider;
app()->register($provider);
}

Expand All @@ -100,29 +113,32 @@ protected function handleCommands()
? DomainCache::get('domain-commands')
: $this->discoverCommands();

$this->registeredCommands = [];

foreach ($commands as $command) {
$this->discoveredCommands[$command] = $command;
$this->registeredCommands[$command] = $command;
$this->registerCommand($command);
}

return $this;
}

public function getDiscoveredCommands(): array
public function getRegisteredCommands(): array
{
return $this->discoveredCommands;
return $this->registeredCommands;
}

public function getDiscoveredProviders(): array
public function getRegisteredProviders(): array
{
return $this->discoveredProviders;
return $this->registeredProviders;
}

protected function registerCommand($class)
{
ConsoleApplication::starting(function ($artisan) use ($class) {
$artisan->resolve($class);
});
// ConsoleApplication::starting(function ($artisan) use ($class) {
// dump('resolving command', $class, $this->registeredCommands);
// $artisan->resolve($class);
// });
}

protected function handlePolicies()
Expand Down Expand Up @@ -222,8 +238,8 @@ protected function discoverCommands(): array
}

$paths = $this->normalizePaths(
$configValue === true ?
$this->getAllLayerPaths()
$configValue === true
? $this->getAllLayerPaths()
: $configValue
);

Expand Down
35 changes: 20 additions & 15 deletions tests/Autoload/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

uses(BootsTestApplication::class);

beforeEach(function () {
$this->setupTestApplication();
DomainCache::clear();
});

afterEach(function () {
DomainCache::clear();
});

describe('without autoload', function () {
it('does not register the command', function ($className, $command) {
beforeEach(function () {
DomainCache::clear();

$this->afterApplicationRefreshed(function () {
$this->setupTestApplication();
app('ddd.autoloader')->boot();
});
});

it('does not register the command', function ($className, $command) {
$this->refreshApplicationWithConfig([
'ddd.autoload.commands' => false,
]);
Expand All @@ -33,14 +33,19 @@
['Infrastructure\Commands\LogPrune', 'log:prune'],
['Application\Commands\ApplicationSync', 'application:sync'],
]);
})->skip();
});

describe('with autoload', function () {
it('registers existing commands', function ($className, $command, $output) {
beforeEach(function () {
DomainCache::clear();

$this->afterApplicationRefreshed(function () {
$this->setupTestApplication();
app('ddd.autoloader')->boot();
});
});

it('registers existing commands', function ($className, $command, $output) {
$this->refreshApplicationWithConfig([
'ddd.autoload.commands' => true,
]);
Expand All @@ -58,13 +63,13 @@
['Infrastructure\Commands\LogPrune', 'log:prune', 'System logs pruned!'],
['Application\Commands\ApplicationSync', 'application:sync', 'Application state synced!'],
]);
})->skip();
});

describe('caching', function () {
it('remembers the last cached state', function () {
DomainCache::set('domain-commands', []);

$this->afterApplicationRefreshed(function () {
$this->setupTestApplication();
DomainCache::set('domain-commands', []);
app('ddd.autoloader')->boot();
});

Expand All @@ -79,10 +84,10 @@
});

it('can bust the cache', function () {
DomainCache::set('domain-commands', []);
DomainCache::clear();

$this->afterApplicationRefreshed(function () {
$this->setupTestApplication();
DomainCache::set('domain-commands', []);
DomainCache::clear();
app('ddd.autoloader')->boot();
});

Expand All @@ -94,4 +99,4 @@
$this->artisan('log:prune')->assertSuccessful();
$this->artisan('application:sync')->assertSuccessful();
});
})->skip();
});
3 changes: 0 additions & 3 deletions tests/Autoload/ProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\Support\DomainCache;
use Lunarstorm\LaravelDDD\Tests\BootsTestApplication;

Expand All @@ -25,8 +24,6 @@
'ddd.autoload.providers' => false,
]);

expect(DDD::autoloader()->getDiscoveredProviders())->toBeEmpty();

expect(fn () => app($binding))->toThrow(Exception::class);
})->with([
['invoicing'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/OptimizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
});

it('can clear the cache', function () {
Artisan::call('ddd:cache');
Artisan::call('ddd:optimize');

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ protected function refreshApplicationWithConfig(array $config)
{
$this->appConfig = $config;

$this->refreshApplication();
// $this->afterApplicationRefreshed(fn () => $this->appConfig = []);

$this->afterApplicationRefreshed(fn () => $this->appConfig = []);
$this->reloadApplication();

$this->appConfig = [];

return $this;
}
Expand Down

0 comments on commit e3f72ad

Please sign in to comment.