1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +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

@ -0,0 +1 @@
{"foo":"bar"}

View file

@ -0,0 +1 @@
{"tenant_id":"The current tenant id is: acme"}

View file

@ -4,15 +4,12 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Redis;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\TestResponse;
use Stancl\Tenancy\Tenant;
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;
/**
* Setup the test environment.
*
@ -22,7 +19,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
{
parent::setUp();
// Redis::connection('cache')->flushdb();
Redis::connection('default')->flushdb();
Redis::connection('cache')->flushdb();
file_put_contents(database_path('central.sqlite'), '');
$this->artisan('migrate:fresh', [
@ -31,14 +29,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'--realpath' => true,
]);
if ($this->autoCreateTenant) {
$this->createTenant();
}
if ($this->autoInitTenancy) {
$this->initTenancy();
}
TestResponse::macro('assertContent', function ($content) {
/** @var TestResponse $this */
@ -48,16 +38,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
});
}
public function createTenant($domains = ['test.localhost'])
{
Tenant::new()->withDomains($domains)->save();
}
public function initTenancy($domain = 'test.localhost')
{
return tenancy()->init($domain);
}
/**
* Define environment setup.
*
@ -75,25 +55,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'database.redis.options.prefix' => 'foo',
'database.redis.tenancy' => [
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
'port' => env('TENANCY_TEST_REDIS_PORT', 6379),
// Use the #14 Redis database unless specified otherwise.
// Make sure you don't store anything in this db!
'database' => env('TENANCY_TEST_REDIS_DB', 14),
'prefix' => 'abc', // unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation
],
'database.connections.central' => [
'driver' => 'sqlite',
'database' => database_path('central.sqlite'),
// 'database' => ':memory:',
],
'tenancy.database' => [
'template_connection' => 'central',
'prefix' => 'tenant',
'suffix' => '.sqlite',
],
'database.connections.sqlite.database' => ':memory:',
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
@ -132,7 +98,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
protected function getPackageAliases($app)
{
return [
'Tenant' => \Stancl\Tenancy\Facades\Tenant::class,
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
];
@ -165,11 +130,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int) (ceil($length / strlen($x))))), 1, $length);
}
public function isContainerized()
{
return env('CONTINUOUS_INTEGRATION') || env('DOCKER');
}
public function assertArrayIsSubset($subset, $array, string $message = ''): void
{
parent::assertTrue(array_intersect($subset, $array) == $subset, $message);

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