1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 18:24:04 +00:00

Queue tests

This commit is contained in:
Samuel Štancl 2020-05-11 05:22:55 +02:00
parent 00bb0d06b3
commit 86a98b2bc8
13 changed files with 216 additions and 124 deletions

View file

@ -178,9 +178,5 @@ class BootstrapperTest extends TestCase
$this->assertFalse(Storage::disk('public')->exists('abc'));
}
/** @test */
public function queue_data_is_separated()
{
// todo
}
// for queues see QueueTest
}

View file

@ -23,7 +23,6 @@ class JobPipelineTest extends TestCase
config(['queue.default' => 'redis']);
file_put_contents(__DIR__ . '/../Etc/tmp/jobpipelinetest.json', '{}');
$this->valuestore = Valuestore::make(__DIR__ . '/../Etc/tmp/jobpipelinetest.json')->flush();
}
@ -64,6 +63,22 @@ class JobPipelineTest extends TestCase
});
}
/** @test */
public function job_pipelines_run_when_queued()
{
Event::listen(TenantCreated::class, JobPipeline::make([
FooJob::class,
])->send(function () {
return $this->valuestore;
})->shouldBeQueued(true)->toListener());
$this->assertFalse($this->valuestore->has('foo'));
Tenant::create();
$this->artisan('queue:work --once');
$this->assertSame('bar', $this->valuestore->get('foo'));
}
/** @test */
public function job_pipeline_executes_jobs_and_passes_the_object_sequentially()
{
@ -159,6 +174,7 @@ class JobWithMultipleArguments
public function handle()
{
// we dont queue this job so no need to use valuestore here
app()->instance('test_args', [$this->first, $this->second]);
}
}

136
tests/v3/QueueTest.php Normal file
View file

@ -0,0 +1,136 @@
<?php
namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Event;
use Spatie\Valuestore\Valuestore;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper;
use Stancl\Tenancy\Tests\TestCase;
class QueueTest extends TestCase
{
public $mockConsoleOutput = false;
/** @var Valuestore */
protected $valuestore;
public function setUp(): void
{
parent::setUp();
config([
'tenancy.bootstrappers' => [
QueueTenancyBootstrapper::class,
],
'queue.default' => 'redis',
]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
$this->valuestore = Valuestore::make(__DIR__ . '/../Etc/tmp/queuetest.json')->flush();
}
/** @test */
public function tenant_id_is_passed_to_tenant_queues()
{
$tenant = Tenant::create();
tenancy()->initialize($tenant);
Event::fake();
dispatch(new TestJob($this->valuestore));
Event::assertDispatched(JobProcessing::class, function ($event) {
return $event->job->payload()['tenant_id'] === tenant('id');
});
}
/** @test */
public function tenant_id_is_not_passed_to_central_queues()
{
$tenant = Tenant::create();
tenancy()->initialize($tenant);
Event::fake();
config(['queue.connections.central' => [
'driver' => 'sync',
'central' => true,
]]);
dispatch(new TestJob($this->valuestore))->onConnection('central');
Event::assertDispatched(JobProcessing::class, function ($event) {
return ! isset($event->job->payload()['tenant_id']);
});
}
/** @test */
public function tenancy_is_initialized_inside_queues()
{
$tenant = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
dispatch(new TestJob($this->valuestore));
$this->assertFalse($this->valuestore->has('tenant_id'));
$this->artisan('queue:work --once');
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
}
/** @test */
public function the_tenant_used_by_the_job_doesnt_change_when_the_current_tenant_changes()
{
$tenant1 = Tenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant1);
dispatch(new TestJob($this->valuestore));
$tenant2 = Tenant::create([
'id' => 'foobar',
]);
tenancy()->initialize($tenant2);
$this->assertFalse($this->valuestore->has('tenant_id'));
$this->artisan('queue:work --once');
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
}
}
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var Valuestore */
protected $valuestore;
public function __construct(Valuestore $valuestore)
{
$this->valuestore = $valuestore;
}
public function handle()
{
$this->valuestore->put('tenant_id', "The current tenant id is: " . tenant('id'));
}
}

View file

@ -130,6 +130,8 @@ class TenantModelTest extends TestCase
$this->assertTrue(tenant() instanceof AnotherTenant);
}
// todo test that tenant can be created even in another DB context - that the central trait works
}
class MyTenant extends Tenant