Skip to content

Commit

Permalink
Try adding provider singleton tests back in.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 17, 2024
1 parent d5e0499 commit 21d8f4c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 172 deletions.
13 changes: 0 additions & 13 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
}

Expand All @@ -23,5 +24,6 @@ public function register()
public function boot()
{
AppSession::setSecret('application-secret');
Clipboard::set('application-secret', 'application-secret');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
}
Expand All @@ -23,5 +24,6 @@ public function register()
public function boot()
{
Invoice::setSecret('invoice-secret');
Clipboard::set('invoicing-secret', 'invoicing-secret');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
}

Expand All @@ -22,6 +22,6 @@ public function register()
*/
public function boot()
{
Clipboard::set('secret', 'infrastructure-secret');
Clipboard::set('infrastructure-secret', 'infrastructure-secret');
}
}
30 changes: 0 additions & 30 deletions tests/Autoload/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
Expand All @@ -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);
});
Expand All @@ -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));
});
Expand Down
20 changes: 0 additions & 20 deletions tests/Autoload/IgnoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 () {
Expand Down
75 changes: 12 additions & 63 deletions tests/Autoload/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
});

Expand All @@ -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');
});
});

Expand All @@ -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);
});
Expand All @@ -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');
});
});
40 changes: 0 additions & 40 deletions tests/AutoloadingTest.php

This file was deleted.

0 comments on commit 21d8f4c

Please sign in to comment.