Skip to content

Commit

Permalink
Fix a few issues with test environment clean slate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 13, 2024
1 parent 3f34b56 commit 7c335d7
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 50 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-data": "^4.10"
},
"suggest": {
"spatie/laravel-data": "Recommended for Data Transfer Objects.",
"lorisleiva/laravel-actions": "Recommended for Actions."
},
"autoload": {
"psr-4": {
"Lunarstorm\\LaravelDDD\\": "src",
Expand All @@ -57,6 +61,7 @@
"analyse": "vendor/bin/phpstan analyse",
"test": "@composer dump-autoload && vendor/bin/pest",
"test-coverage": "@composer dump-autoload && vendor/bin/pest --coverage",
"purge-skeleton": "vendor/bin/testbench package:purge-skeleton",
"format": "vendor/bin/pint",
"lint": "vendor/bin/pint"
},
Expand Down
7 changes: 7 additions & 0 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lunarstorm\LaravelDDD;

use Illuminate\Database\Migrations\MigrationCreator;
use Lunarstorm\LaravelDDD\Support\DomainAutoloader;
use Lunarstorm\LaravelDDD\Support\DomainMigration;
use Spatie\LaravelPackageTools\Package;
Expand Down Expand Up @@ -97,6 +98,12 @@ protected function registerMigrations()
return new Commands\Migration\DomainMigrateMakeCommand($creator, $composer);
});

// $this->app->when(MigrationCreator::class)
// ->needs('$customStubPath')
// ->give(function ($app) {
// return $app->basePath('stubs');
// });

$this->loadMigrationsFrom(DomainMigration::paths());
}

Expand Down
5 changes: 5 additions & 0 deletions tests/.skeleton/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"Support\\": "src/Support"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": []
Expand Down
16 changes: 10 additions & 6 deletions tests/Autoload/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
'ddd.autoload.providers' => false,
]);

$this->afterApplicationCreated(function () {
(new DomainAutoloader)->autoload();
});
// $this->afterApplicationCreated(function () {
// (new DomainAutoloader)->autoload();
// });

(new DomainAutoloader)->autoload();
});

it('does not register the provider', function () {
Expand All @@ -33,9 +35,11 @@
'ddd.autoload.providers' => true,
]);

$this->afterApplicationCreated(function () {
(new DomainAutoloader)->autoload();
});
// $this->afterApplicationCreated(function () {
// (new DomainAutoloader)->autoload();
// });

(new DomainAutoloader)->autoload();
});

it('registers the provider', function () {
Expand Down
4 changes: 3 additions & 1 deletion tests/Command/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
$this->cleanSlate();
$this->setupTestApplication();
Artisan::call('config:clear');
});
$this->composerReload();
})->skip();

afterEach(function () {
$this->cleanSlate();
Artisan::call('config:clear');
$this->composerReload();
});

it('can run the config wizard', function () {
Expand Down
4 changes: 4 additions & 0 deletions tests/Command/InstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
$this->setupTestApplication();
});

afterEach(function () {
$this->setupTestApplication();
});

it('publishes config', function () {
$path = config_path('ddd.php');

Expand Down
2 changes: 2 additions & 0 deletions tests/Command/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Lunarstorm\LaravelDDD\Support\Path;

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

$this->artisan('ddd:model', [
'name' => 'Invoice',
'--domain' => 'Invoicing',
Expand Down
10 changes: 5 additions & 5 deletions tests/Command/CacheTest.php → tests/Command/OptimizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
DomainCache::clear();
});

it('can cache discovered domain providers, commands, migrations', function () {
it('can optimize discovered domain providers, commands, migrations', function () {
expect(DomainCache::get('domain-providers'))->toBeNull();
expect(DomainCache::get('domain-commands'))->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->toBeNull();

$this
->artisan('ddd:cache')
->artisan('ddd:optimize')
->expectsOutputToContain('Caching DDD providers, commands, migration paths.')
->expectsOutputToContain('domain providers')
->expectsOutputToContain('domain commands')
Expand Down Expand Up @@ -66,7 +66,7 @@
expect(DomainCache::get('domain-commands'))->toBeNull();
expect(DomainCache::get('domain-migration-paths'))->toBeNull();

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

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
Expand All @@ -88,7 +88,7 @@
});

describe('laravel optimize', function () {
test('optimize will include ddd:cache', function () {
test('optimize will include ddd:optimize', function () {
config(['cache.default' => 'file']);

expect(DomainCache::get('domain-providers'))->toBeNull();
Expand All @@ -105,7 +105,7 @@
test('optimize:clear will clear ddd cache', function () {
config(['cache.default' => 'file']);

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

expect(DomainCache::get('domain-providers'))->not->toBeNull();
expect(DomainCache::get('domain-commands'))->not->toBeNull();
Expand Down
6 changes: 3 additions & 3 deletions tests/Support/ResolveObjectSchemaUsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Lunarstorm\LaravelDDD\ValueObjects\ObjectSchema;

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

Config::set('ddd.domain_path', 'src/Domain');
Config::set('ddd.domain_namespace', 'Domain');

$this->setupTestApplication();
});

it('can register a custom namespace resolver', function () {
it('can register a custom object schema resolver', function () {
Config::set('ddd.application', [
'path' => 'src/App',
'namespace' => 'App',
Expand Down
56 changes: 21 additions & 35 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ protected function setUp(): void
$this->afterApplicationCreated(function () {
$this->cleanSlate();

// $this->updateComposer(
// set: [
// [['autoload', 'psr-4', 'App\\'], 'vendor/orchestra/testbench-core/laravel/app'],
// [['autoload', 'psr-4', 'Database\\Factories\\'], 'vendor/orchestra/testbench-core/laravel/database/factories'],
// [['autoload', 'psr-4', 'Database\\Seeders\\'], 'vendor/orchestra/testbench-core/laravel/database/seeders'],
// [['autoload', 'psr-4', 'Domain\\'], 'vendor/orchestra/testbench-core/laravel/src/Domain'],
// ],
// forget: [
// ['autoload', 'psr-4', 'Domains\\'],
// ['autoload', 'psr-4', 'Domain\\'],
// ]
// );

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Lunarstorm\\LaravelDDD\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
Expand All @@ -56,16 +43,6 @@ protected function defineEnvironment($app)
$config->set($key, $value);
}
});

// $this->updateComposer(
// set: [
// [['autoload', 'psr-4', 'App\\'], 'vendor/orchestra/testbench-core/laravel/app'],
// ],
// forget: [
// ['autoload', 'psr-4', 'Domains\\'],
// ['autoload', 'psr-4', 'Domain\\'],
// ]
// );
}

protected function getComposerFileContents()
Expand Down Expand Up @@ -142,25 +119,25 @@ protected function composerReload()

protected function cleanSlate()
{
File::copy(__DIR__.'/.skeleton/composer.json', base_path('composer.json'));

File::delete(base_path('config/ddd.php'));

File::cleanDirectory(app_path());
// File::cleanDirectory(app_path());
File::cleanDirectory(app_path('Models'));
File::cleanDirectory(base_path('database/factories'));
File::cleanDirectory(base_path('src'));

File::deleteDirectory(resource_path('stubs/ddd'));
File::deleteDirectory(base_path('stubs'));
File::deleteDirectory(base_path('Custom'));
File::cleanDirectory(base_path('src'));
File::deleteDirectory(base_path('src/Domain'));
File::deleteDirectory(base_path('src/Domains'));
File::deleteDirectory(base_path('src/App'));
// File::deleteDirectory(base_path('src/Domain'));
// File::deleteDirectory(base_path('src/Domains'));
// File::deleteDirectory(base_path('src/App'));
File::deleteDirectory(app_path('Policies'));
File::deleteDirectory(app_path('Modules'));
File::deleteDirectory(app_path('Models'));

File::deleteDirectory(base_path('bootstrap/cache/ddd'));

File::copy(__DIR__.'/.skeleton/composer.json', base_path('composer.json'));

return $this;
}

Expand All @@ -173,11 +150,20 @@ protected function cleanStubs()

protected function setupTestApplication()
{
File::copyDirectory(__DIR__.'/.skeleton/app', app_path());
$this->cleanSlate();

File::ensureDirectoryExists(app_path());
File::ensureDirectoryExists(app_path('Models'));

$skeletonAppFolders = glob(__DIR__.'/.skeleton/app/*', GLOB_ONLYDIR);

foreach ($skeletonAppFolders as $folder) {
File::copyDirectory($folder, app_path(basename($folder)));
}

File::copyDirectory(__DIR__.'/.skeleton/database', base_path('database'));
File::copyDirectory(__DIR__.'/.skeleton/src/Domain', base_path('src/Domain'));
File::copyDirectory(__DIR__.'/.skeleton/src', base_path('src'));
File::copy(__DIR__.'/.skeleton/bootstrap/providers.php', base_path('bootstrap/providers.php'));
File::ensureDirectoryExists(app_path('Models'));

$this->setDomainPathInComposer('Domain', 'src/Domain');

Expand Down

0 comments on commit 7c335d7

Please sign in to comment.