From 21d8f4c81867789dba16af5eb94315e6de33687c Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Sun, 17 Nov 2024 15:00:36 -0500 Subject: [PATCH] Try adding provider singleton tests back in. --- src/LaravelDDDServiceProvider.php | 13 ---- .../Providers/ApplicationServiceProvider.php | 6 +- .../Providers/InvoiceServiceProvider.php | 4 +- .../InfrastructureServiceProvider.php | 6 +- tests/Autoload/CommandTest.php | 30 -------- tests/Autoload/IgnoreTest.php | 20 ----- tests/Autoload/ProviderTest.php | 75 +++---------------- tests/AutoloadingTest.php | 40 ---------- 8 files changed, 22 insertions(+), 172 deletions(-) delete mode 100644 tests/AutoloadingTest.php diff --git a/src/LaravelDDDServiceProvider.php b/src/LaravelDDDServiceProvider.php index cbdd19a..cd21e3f 100644 --- a/src/LaravelDDDServiceProvider.php +++ b/src/LaravelDDDServiceProvider.php @@ -3,7 +3,6 @@ namespace Lunarstorm\LaravelDDD; use Illuminate\Database\Migrations\MigrationCreator; -use Illuminate\Foundation\Application; use Lunarstorm\LaravelDDD\Facades\Autoload; use Lunarstorm\LaravelDDD\Support\AutoloadManager; use Lunarstorm\LaravelDDD\Support\DomainMigration; @@ -129,18 +128,6 @@ protected function registerBindings() $this->app->bind('ddd.composer', ComposerManager::class); $this->app->bind('ddd.stubs', StubManager::class); - if ($this->app->runningUnitTests()) { - // $this->app->when(AutoloadManager::class) - // ->needs(Application::class) - // ->give(function () { - // return $this->app; - // }); - - $this->app->resolving(AutoloadManager::class, function (AutoloadManager $atuoloader, Application $app) { - // dump('App resolving autoloader'); - }); - } - return $this; } diff --git a/tests/.skeleton/src/Application/Providers/ApplicationServiceProvider.php b/tests/.skeleton/src/Application/Providers/ApplicationServiceProvider.php index 1b366e1..f5ec9d7 100644 --- a/tests/.skeleton/src/Application/Providers/ApplicationServiceProvider.php +++ b/tests/.skeleton/src/Application/Providers/ApplicationServiceProvider.php @@ -5,13 +5,14 @@ use Infrastructure\Models\AppSession; use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; +use Infrastructure\Support\Clipboard; class ApplicationServiceProvider extends ServiceProvider { public function register() { - $this->app->singleton('application-layer', function (Application $app) { - return 'application-layer-singleton'; + $this->app->singleton('application-singleton', function (Application $app) { + return 'application-singleton'; }); } @@ -23,5 +24,6 @@ public function register() public function boot() { AppSession::setSecret('application-secret'); + Clipboard::set('application-secret', 'application-secret'); } } diff --git a/tests/.skeleton/src/Domain/Invoicing/Providers/InvoiceServiceProvider.php b/tests/.skeleton/src/Domain/Invoicing/Providers/InvoiceServiceProvider.php index 5e257e5..7ddb52a 100644 --- a/tests/.skeleton/src/Domain/Invoicing/Providers/InvoiceServiceProvider.php +++ b/tests/.skeleton/src/Domain/Invoicing/Providers/InvoiceServiceProvider.php @@ -5,12 +5,13 @@ use Domain\Invoicing\Models\Invoice; use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; +use Infrastructure\Support\Clipboard; class InvoiceServiceProvider extends ServiceProvider { public function register() { - $this->app->singleton('invoicing', function (Application $app) { + $this->app->singleton('invoicing-singleton', function (Application $app) { return 'invoicing-singleton'; }); } @@ -23,5 +24,6 @@ public function register() public function boot() { Invoice::setSecret('invoice-secret'); + Clipboard::set('invoicing-secret', 'invoicing-secret'); } } diff --git a/tests/.skeleton/src/Infrastructure/Providers/InfrastructureServiceProvider.php b/tests/.skeleton/src/Infrastructure/Providers/InfrastructureServiceProvider.php index 5174324..45df08b 100644 --- a/tests/.skeleton/src/Infrastructure/Providers/InfrastructureServiceProvider.php +++ b/tests/.skeleton/src/Infrastructure/Providers/InfrastructureServiceProvider.php @@ -10,8 +10,8 @@ class InfrastructureServiceProvider extends ServiceProvider { public function register() { - $this->app->singleton('infrastructure-layer', function (Application $app) { - return 'infrastructure-layer-singleton'; + $this->app->singleton('infrastructure-singleton', function (Application $app) { + return 'infrastructure-singleton'; }); } @@ -22,6 +22,6 @@ public function register() */ public function boot() { - Clipboard::set('secret', 'infrastructure-secret'); + Clipboard::set('infrastructure-secret', 'infrastructure-secret'); } } diff --git a/tests/Autoload/CommandTest.php b/tests/Autoload/CommandTest.php index c66a756..da029af 100644 --- a/tests/Autoload/CommandTest.php +++ b/tests/Autoload/CommandTest.php @@ -3,7 +3,6 @@ use Illuminate\Support\Facades\Artisan; use Lunarstorm\LaravelDDD\Support\AutoloadManager; use Lunarstorm\LaravelDDD\Support\DomainCache; -use Lunarstorm\LaravelDDD\Support\Path; use Lunarstorm\LaravelDDD\Tests\BootsTestApplication; uses(BootsTestApplication::class); @@ -54,18 +53,8 @@ $mock = AutoloadManager::partialMock(); $mock->run(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $expected = array_values($this->commands); $registered = array_values($mock->getRegisteredCommands()); - expect($mock->discoverCommands())->toEqualCanonicalizing($expected); expect($expected)->each(fn ($item) => $item->toBeIn($registered)); expect($registered)->toHaveCount(count($expected)); }); @@ -80,15 +69,6 @@ $mock = AutoloadManager::partialMock(); $mock->run(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $registered = array_values($mock->getRegisteredCommands()); expect($registered)->toHaveCount(0); }); @@ -102,18 +82,8 @@ $mock = AutoloadManager::partialMock(); $mock->run(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $expected = array_values($this->commands); $registered = array_values($mock->getRegisteredCommands()); - expect($mock->discoverCommands())->toEqualCanonicalizing($expected); expect($expected)->each(fn ($item) => $item->toBeIn($registered)); expect($registered)->toHaveCount(count($expected)); }); diff --git a/tests/Autoload/IgnoreTest.php b/tests/Autoload/IgnoreTest.php index dbb526e..5be1acf 100644 --- a/tests/Autoload/IgnoreTest.php +++ b/tests/Autoload/IgnoreTest.php @@ -5,7 +5,6 @@ use Illuminate\Support\Str; use Lunarstorm\LaravelDDD\Facades\DDD; use Lunarstorm\LaravelDDD\Support\DomainCache; -use Lunarstorm\LaravelDDD\Support\Path; use Lunarstorm\LaravelDDD\Tests\BootsTestApplication; use Symfony\Component\Finder\SplFileInfo; @@ -36,25 +35,6 @@ 'policies' => true, 'migrations' => true, ]); - - expect(DDD::autoloader()->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - expect(config('ddd.autoload_ignore'))->toEqualCanonicalizing([ - 'Tests', - 'Database/Migrations', - ]); - - foreach ($this->providers as $provider) { - expect(class_exists($provider))->toBeTrue("{$provider} class does not exist"); - } - - foreach ($this->commands as $command) { - expect(class_exists($command))->toBeTrue("{$command} class does not exist"); - } }); afterEach(function () { diff --git a/tests/Autoload/ProviderTest.php b/tests/Autoload/ProviderTest.php index 753bc51..871eba6 100644 --- a/tests/Autoload/ProviderTest.php +++ b/tests/Autoload/ProviderTest.php @@ -3,7 +3,6 @@ use Illuminate\Support\Facades\Artisan; use Lunarstorm\LaravelDDD\Support\AutoloadManager; use Lunarstorm\LaravelDDD\Support\DomainCache; -use Lunarstorm\LaravelDDD\Support\Path; use Lunarstorm\LaravelDDD\Tests\BootsTestApplication; uses(BootsTestApplication::class); @@ -46,16 +45,11 @@ $mock = AutoloadManager::partialMock(); $mock->run(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - expect($mock->getRegisteredProviders())->toBeEmpty(); + + expect(fn () => app('invoicing-singleton'))->toThrow(Exception::class); + expect(fn () => app('application-singleton'))->toThrow(Exception::class); + expect(fn () => app('infrastructure-singleton'))->toThrow(Exception::class); }); }); @@ -66,50 +60,24 @@ $mock = AutoloadManager::partialMock(); $mock->shouldReceive('handleProviders')->once(); $mock->run(); - - expect(DomainCache::has('domain-providers'))->toBeFalse(); - - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); }); it('registers the providers', function () { config()->set('ddd.autoload.providers', true); - AutoloadManager::registeringProvider(function ($provider) { - dump('registering provider: '.$provider); - }); - $mock = AutoloadManager::partialMock(); $mock->run(); expect(DomainCache::has('domain-providers'))->toBeFalse(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $expected = array_values($this->providers); - - foreach ($expected as $provider) { - expect(class_exists($provider))->toBeTrue("class_exists false on expected {$provider}"); - } - $registered = array_values($mock->getRegisteredProviders()); - expect($expected)->each(fn ($item) => $item->toBeIn($registered)); expect($registered)->toHaveCount(count($expected)); + + expect(app('application-singleton'))->toEqual('application-singleton'); + expect(app('invoicing-singleton'))->toEqual('invoicing-singleton'); + expect(app('infrastructure-singleton'))->toEqual('infrastructure-singleton'); }); }); @@ -124,15 +92,6 @@ expect(DomainCache::has('domain-providers'))->toBeTrue(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $registered = array_values($mock->getRegisteredProviders()); expect($registered)->toHaveCount(0); }); @@ -146,23 +105,13 @@ $mock = AutoloadManager::partialMock(); $mock->run(); - expect($mock->getAllLayerPaths())->toEqualCanonicalizing([ - Path::normalize(base_path('src/Domain')), - Path::normalize(base_path('src/Application')), - Path::normalize(base_path('src/Infrastructure')), - ]); - - collect($mock->getAllLayerPaths()) - ->each(fn ($path) => expect(is_dir($path))->toBeTrue("{$path} is not a directory")); - $expected = array_values($this->providers); - - foreach ($expected as $provider) { - expect(class_exists($provider))->toBeTrue("class_exists false on expected {$provider}"); - } - $registered = array_values($mock->getRegisteredProviders()); expect($expected)->each(fn ($item) => $item->toBeIn($registered)); expect($registered)->toHaveCount(count($expected)); + + expect(app('application-singleton'))->toEqual('application-singleton'); + expect(app('invoicing-singleton'))->toEqual('invoicing-singleton'); + expect(app('infrastructure-singleton'))->toEqual('infrastructure-singleton'); }); }); diff --git a/tests/AutoloadingTest.php b/tests/AutoloadingTest.php deleted file mode 100644 index 8ed1eab..0000000 --- a/tests/AutoloadingTest.php +++ /dev/null @@ -1,40 +0,0 @@ - 'src/Domain', - // 'ddd.domain_namespace' => 'Domain', - // 'ddd.application_namespace' => 'Application', - // 'ddd.application_path' => 'src/Application', - // 'ddd.application_objects' => [ - // 'controller', - // 'request', - // 'middleware', - // ], - // 'ddd.layers' => [ - // 'Infrastructure' => 'src/Infrastructure', - // ], - // 'ddd.autoload_ignore' => [ - // 'Tests', - // 'Database/Migrations', - // ], - // 'cache.default' => 'file', - // ...static::$configValues, - // ]; - - // tap($app['config'], function (Repository $config) { - // foreach (static::$configValues as $key => $value) { - // $config->set($key, $value); - // } - // }); - // } -}