mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 21:34:04 +00:00
JobPipeline tests
This commit is contained in:
parent
bd9aad229b
commit
7a2e6bb13e
4 changed files with 108 additions and 12 deletions
|
|
@ -62,13 +62,8 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
foreach ($this->events() as $event => $listeners) {
|
foreach ($this->events() as $event => $listeners) {
|
||||||
foreach (array_unique($listeners) as $listener) {
|
foreach (array_unique($listeners) as $listener) {
|
||||||
// Technically, the string|Closure typehint is not enforced by
|
if ($listener instanceof JobPipeline) {
|
||||||
// Laravel, but for type correctness, we wrap callables in
|
$listener = $listener->toClosure();
|
||||||
// simple Closures, to match Laravel's docblock typehint.
|
|
||||||
if (is_callable($listener) && !$listener instanceof Closure) {
|
|
||||||
$listener = function ($event) use ($listener) {
|
|
||||||
$listener($event);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::listen($event, $listener);
|
Event::listen($event, $listener);
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,6 @@ class Tenant extends Model
|
||||||
|
|
||||||
public $guarded = [];
|
public $guarded = [];
|
||||||
|
|
||||||
public function domains() // todo not required
|
|
||||||
{
|
|
||||||
return $this->hasMany(Domain::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function internalPrefix(): string
|
public static function internalPrefix(): string
|
||||||
{
|
{
|
||||||
return config('tenancy.database_prefix');
|
return config('tenancy.database_prefix');
|
||||||
|
|
@ -71,6 +66,7 @@ class Tenant extends Model
|
||||||
|
|
||||||
public function run(callable $callback)
|
public function run(callable $callback)
|
||||||
{
|
{
|
||||||
|
// todo new logic with the manager
|
||||||
$originalTenant = $this->manager->getTenant();
|
$originalTenant = $this->manager->getTenant();
|
||||||
|
|
||||||
$this->manager->initializeTenancy($this);
|
$this->manager->initializeTenancy($this);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Events\Listeners;
|
namespace Stancl\Tenancy\Events\Listeners;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Pipeline\Pipeline;
|
use Illuminate\Pipeline\Pipeline;
|
||||||
|
|
||||||
|
|
@ -71,4 +72,20 @@ class JobPipeline implements ShouldQueue
|
||||||
->through($this->jobs)
|
->through($this->jobs)
|
||||||
->thenReturn();
|
->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);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\v3;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Stancl\Tenancy\Database\Models\Tenant;
|
||||||
|
use Stancl\Tenancy\Events\Listeners\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
|
class JobPipelineTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
public function job_pipeline_can_listen_to_any_event()
|
||||||
|
{
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
FooJob::class,
|
||||||
|
])->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue