Skip to content

Commit

Permalink
Attempt to get the install/config/optimize tests working idempotently.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 17, 2024
1 parent 2d1b19f commit d3bc39a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class ConfigManager

protected string $stub;

public function __construct(public string $configPath)
public function __construct(public ?string $configPath = null)
{
$this->configPath = $configPath ?? app()->configPath('ddd.php');

$this->packageConfig = require DDD::packagePath('config/ddd.php');

$this->config = file_exists($configPath) ? require ($configPath) : $this->packageConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function registerBindings()
});

$this->app->scoped(ConfigManager::class, function () {
return new ConfigManager(config_path('ddd.php'));
return new ConfigManager($this->app->configPath('ddd.php'));
});

$this->app->scoped(StubManager::class, function () {
Expand Down
52 changes: 34 additions & 18 deletions tests/Command/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Database\Migrations\MigrationCreator;
use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\Tests\BootsTestApplication;

Expand All @@ -9,27 +9,29 @@
beforeEach(function () {
$this->setupTestApplication();

Artisan::call('config:clear');
Artisan::call('ddd:clear');
})->skip('causing issues in test suite');
// $this->app->when(MigrationCreator::class)
// ->needs('$customStubPath')
// ->give(fn() => $this->app->basePath('stubs'));

$this->originalComposerContents = file_get_contents(base_path('composer.json'));
});

afterEach(function () {
$this->cleanSlate();

Artisan::call('config:clear');
Artisan::call('ddd:clear');
file_put_contents(base_path('composer.json'), $this->originalComposerContents);
});

it('can run the config wizard', function () {
Artisan::call('config:cache');
$this->artisan('config:cache')->assertSuccessful()->execute();

expect(config('ddd.domain_path'))->toBe('src/Domain');
expect(config('ddd.domain_namespace'))->toBe('Domain');
expect(config('ddd.layers'))->toBe([
'Infrastructure' => 'src/Infrastructure',
]);

$path = config_path('ddd.php');
$configPath = config_path('ddd.php');

$this->artisan('ddd:config')
->expectsQuestion('Laravel-DDD Config Utility', 'wizard')
Expand All @@ -38,13 +40,13 @@
->expectsQuestion('Path to Application Layer', null)
->expectsQuestion('Custom Layers (Optional)', ['Support' => 'src/Support'])
->expectsOutput('Building configuration...')
->expectsOutput("Configuration updated: {$path}")
->expectsOutput("Configuration updated: {$configPath}")
->assertSuccessful()
->execute();

expect(file_exists($path))->toBeTrue();
expect(file_exists($configPath))->toBeTrue();

Artisan::call('config:cache');
$this->artisan('config:cache')->assertSuccessful()->execute();

expect(config('ddd.domain_path'))->toBe('src/CustomDomain');
expect(config('ddd.domain_namespace'))->toBe('CustomDomain');
Expand All @@ -53,36 +55,42 @@
expect(config('ddd.layers'))->toBe([
'Support' => 'src/Support',
]);

$this->artisan('config:clear')->assertSuccessful()->execute();

unlink($configPath);
});

it('can update and merge ddd.php with latest package version', function () {
$path = config_path('ddd.php');
$configPath = config_path('ddd.php');

$originalContents = <<<'PHP'
<?php
return [];
PHP;

file_put_contents($path, $originalContents);
file_put_contents($configPath, $originalContents);

$this->artisan('ddd:config')
->expectsQuestion('Laravel-DDD Config Utility', 'update')
->expectsQuestion('Are you sure you want to update ddd.php and merge with latest copy from the package?', true)
->expectsOutput('Merging ddd.php...')
->expectsOutput("Configuration updated: {$path}")
->expectsOutput("Configuration updated: {$configPath}")
->expectsOutput('Note: Some values may require manual adjustment.')
->assertSuccessful()
->execute();

$packageConfigContents = file_get_contents(DDD::packagePath('config/ddd.php'));

expect($updatedContents = file_get_contents($path))
expect($updatedContents = file_get_contents($configPath))
->not->toEqual($originalContents);

$updatedConfigArray = include $path;
$updatedConfigArray = include $configPath;
$packageConfigArray = include DDD::packagePath('config/ddd.php');

expect($updatedConfigArray)->toHaveKeys(array_keys($packageConfigArray));

unlink($configPath);
});

it('can sync composer.json from ddd.php ', function () {
Expand All @@ -107,7 +115,7 @@

file_put_contents(config_path('ddd.php'), $configContent);

Artisan::call('config:cache');
$this->artisan('config:cache')->assertSuccessful()->execute();

$composerContents = file_get_contents(base_path('composer.json'));

Expand Down Expand Up @@ -146,6 +154,10 @@
$composerContents = file_get_contents(base_path('composer.json'));

expect($composerContents)->toContain(...$fragments);

$this->artisan('config:clear')->assertSuccessful()->execute();

unlink(config_path('ddd.php'));
});

it('can detect domain namespace from composer.json', function () {
Expand All @@ -156,6 +168,8 @@
$sampleComposer
);

$configPath = config_path('ddd.php');

$this->artisan('ddd:config')
->expectsQuestion('Laravel-DDD Config Utility', 'detect')
->expectsOutputToContain(...[
Expand All @@ -166,12 +180,14 @@
'Domain',
])
->expectsQuestion('Update configuration with these values?', true)
->expectsOutput('Configuration updated: '.config_path('ddd.php'))
->expectsOutput('Configuration updated: '.$configPath)
->assertSuccessful()
->execute();

$configValues = DDD::config()->get();

expect(data_get($configValues, 'domain_path'))->toBe('lib/CustomDomain');
expect(data_get($configValues, 'domain_namespace'))->toBe('Domain');

unlink($configPath);
});
4 changes: 1 addition & 3 deletions tests/Command/InstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
use Illuminate\Support\Facades\Config;

beforeEach(function () {
$this->setupTestApplication();

$this->originalComposerContents = file_get_contents(base_path('composer.json'));
$this->setupTestApplication();
});

afterEach(function () {
// $this->setupTestApplication()->composerReload();
file_put_contents(base_path('composer.json'), $this->originalComposerContents);
});

Expand Down
27 changes: 15 additions & 12 deletions tests/Command/OptimizeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Lunarstorm\LaravelDDD\Support\DomainCache;
use Lunarstorm\LaravelDDD\Tests\BootsTestApplication;
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;
Expand All @@ -10,15 +9,15 @@
beforeEach(function () {
$this->setupTestApplication();

$this->artisan('optimize:clear')->execute();

DomainCache::clear();

$this->originalComposerContents = file_get_contents(base_path('composer.json'));
});

afterEach(function () {
$this->artisan('optimize:clear')->execute();

DomainCache::clear();

file_put_contents(base_path('composer.json'), $this->originalComposerContents);
});

it('can optimize discovered domain providers, commands, migrations', function () {
Expand Down Expand Up @@ -46,7 +45,7 @@
});

it('can clear the cache', function () {
Artisan::call('ddd:optimize');
$this->artisan('ddd:optimize')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
Expand All @@ -67,20 +66,20 @@
expect(DomainCache::get('domain-commands'))->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->toBeNull();

$this->artisan('ddd:optimize')->execute();
$this->artisan('ddd:optimize')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->not->toBeNull();

$this->artisan('cache:clear')->execute();
$this->artisan('cache:clear')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->not->toBeNull();

if (Feature::LaravelPackageOptimizeCommands->missing()) {
$this->artisan('optimize:clear')->execute();
$this->artisan('optimize:clear')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
Expand All @@ -94,24 +93,28 @@
expect(DomainCache::get('domain-commands'))->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->toBeNull();

$this->artisan('optimize')->execute();
$this->artisan('optimize')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->not->toBeNull();

$this->artisan('optimize:clear')->assertSuccessful()->execute();
});

test('optimize:clear will clear ddd cache', function () {
$this->artisan('ddd:optimize')->execute();
$this->artisan('ddd:optimize')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->not->toBeNull();

$this->artisan('optimize:clear')->execute();
$this->artisan('optimize:clear')->assertSuccessful()->execute();

expect(DomainCache::get('domain-providers'))->toBeNull();
expect(DomainCache::get('domain-commands'))->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->toBeNull();

$this->artisan('optimize:clear')->assertSuccessful()->execute();
});
})->skipOnLaravelVersionsBelow(Feature::LaravelPackageOptimizeCommands->value);
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ protected function cleanSlate()

File::cleanDirectory($basePath.'/app/Models');
File::cleanDirectory($basePath.'/database/factories');
File::cleanDirectory($basePath.'/bootstrap/cache');
File::cleanDirectory($basePath.'/bootstrap/cache/ddd');

File::deleteDirectory($basePath.'/src');
File::deleteDirectory($basePath.'/resources/stubs/ddd');
File::deleteDirectory($basePath.'/stubs');
File::deleteDirectory($basePath.'/Custom');
File::deleteDirectory($basePath.'/Other');
File::deleteDirectory($basePath.'/app/Policies');
File::deleteDirectory($basePath.'/app/Modules');

Expand Down

0 comments on commit d3bc39a

Please sign in to comment.