From 7a2e6bb13ed88baa9530f84aa010f203c220d8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 8 May 2020 05:12:46 +0200 Subject: [PATCH] JobPipeline tests --- assets/TenancyServiceProvider.stub.php | 9 +-- src/Database/Models/Tenant.php | 6 +- src/Events/Listeners/JobPipeline.php | 17 +++++ tests/v3/JobPipelineTest.php | 88 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 12 deletions(-) diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index f3e230b4..693ffc8a 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -62,13 +62,8 @@ class TenancyServiceProvider extends ServiceProvider { foreach ($this->events() as $event => $listeners) { foreach (array_unique($listeners) as $listener) { - // Technically, the string|Closure typehint is not enforced by - // Laravel, but for type correctness, we wrap callables in - // simple Closures, to match Laravel's docblock typehint. - if (is_callable($listener) && !$listener instanceof Closure) { - $listener = function ($event) use ($listener) { - $listener($event); - }; + if ($listener instanceof JobPipeline) { + $listener = $listener->toClosure(); } Event::listen($event, $listener); diff --git a/src/Database/Models/Tenant.php b/src/Database/Models/Tenant.php index f65f414e..2f6eb7cb 100644 --- a/src/Database/Models/Tenant.php +++ b/src/Database/Models/Tenant.php @@ -29,11 +29,6 @@ class Tenant extends Model public $guarded = []; - public function domains() // todo not required - { - return $this->hasMany(Domain::class); - } - public static function internalPrefix(): string { return config('tenancy.database_prefix'); @@ -71,6 +66,7 @@ class Tenant extends Model public function run(callable $callback) { + // todo new logic with the manager $originalTenant = $this->manager->getTenant(); $this->manager->initializeTenancy($this); diff --git a/src/Events/Listeners/JobPipeline.php b/src/Events/Listeners/JobPipeline.php index 4ab09e4b..2d993a41 100644 --- a/src/Events/Listeners/JobPipeline.php +++ b/src/Events/Listeners/JobPipeline.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy\Events\Listeners; +use Closure; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Pipeline\Pipeline; @@ -71,4 +72,20 @@ class JobPipeline implements ShouldQueue ->through($this->jobs) ->thenReturn(); } + + /** + * Generate a closure that runs this listener. + * + * Technically, the string|Closure typehint is not enforced by + * Laravel, but for correct typing we wrap this callable in + * simple Closures, to match Laravel's docblock typehint. + * + * @return Closure + */ + public function toClosure(): Closure + { + return function (...$args) { + $this->handle(...$args); + }; + } } diff --git a/tests/v3/JobPipelineTest.php b/tests/v3/JobPipelineTest.php index e69de29b..e12ed9ee 100644 --- a/tests/v3/JobPipelineTest.php +++ b/tests/v3/JobPipelineTest.php @@ -0,0 +1,88 @@ +toClosure()); + + $this->assertFalse(app()->bound('foo')); + + Tenant::create(); + + $this->assertSame('bar', app('foo')); + } + + /** @test */ + public function job_pipeline_can_be_queued() + { + // todo: This does not work because of toClosure + + Queue::fake(); + + Event::listen(TenantCreated::class, JobPipeline::make([ + FooJob::class, + ])->queue(true)->toClosure()); + + Queue::assertNothingPushed(); + + Tenant::create(); + $this->assertFalse(app()->bound('foo')); + + Queue::assertPushed(JobPipeline::class); + } + + /** @test */ + public function job_pipeline_executes_jobs_and_passes_the_object_sequentially() + { + Event::listen(TenantCreated::class, JobPipeline::make([ + FirstJob::class, + SecondJob::class, + ])->send(function (TenantCreated $event) { + return $event->tenant; + })->toClosure()); + + $this->assertFalse(app()->bound('foo')); + + // todo: for some reason, SecondJob is not reached in the pipeline + Tenant::create(); + + $this->assertSame('first job changed property', app('foo')); + } +} + +class FooJob +{ + public function handle() + { + app()->instance('foo', 'bar'); + } +}; + +class FirstJob +{ + public function handle(Tenant $tenant) + { + $tenant->foo = 'first job changed property'; + } +} + +class SecondJob +{ + public function handle(Tenant $tenant) + { + app()->instance('foo', $tenant->foo); + } +}