mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 12:34:03 +00:00
convert test to pest
This commit is contained in:
parent
a7e1a17cd0
commit
cbdcec2bcd
2 changed files with 35 additions and 61 deletions
|
|
@ -1,11 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers;
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
use Illuminate\Bus\BatchRepository;
|
use Illuminate\Bus\BatchRepository;
|
||||||
use Illuminate\Bus\DatabaseBatchRepository;
|
use Illuminate\Bus\DatabaseBatchRepository;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use ReflectionClass;
|
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
|
||||||
|
|
||||||
use Illuminate\Bus\BatchRepository;
|
use Illuminate\Bus\BatchRepository;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use ReflectionClass;
|
|
||||||
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
|
@ -16,12 +10,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
class BatchTest extends TestCase
|
beforeEach(function () {
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
$this->app->singleton(BatchTenancyBootstrapper::class);
|
$this->app->singleton(BatchTenancyBootstrapper::class);
|
||||||
|
|
||||||
config([
|
config([
|
||||||
|
|
@ -33,39 +22,23 @@ class BatchTest extends TestCase
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
}
|
});
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function batch_repository_is_set_to_tenant_connection_and_reverted()
|
|
||||||
{
|
|
||||||
if (! version_compare(app()->version(), '8.0', '>=')) {
|
|
||||||
$this->markTestSkipped('Job batches are only supported in Laravel 8+');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
test('batch repository is set to tenant connection and reverted', function () {
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
$this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected initial connection to be central');
|
expect(getBatchRepositoryConnectionName())->toBe('central');
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
$this->assertEquals('tenant', $this->getBatchRepositoryConnectionName(), 'Expected tenant connection to be tenant');
|
expect(getBatchRepositoryConnectionName())->toBe('tenant');
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
$this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected the reverted connection to be central');
|
expect(getBatchRepositoryConnectionName())->toBe('central');
|
||||||
}
|
})->skip(fn() => version_compare(app()->version(), '8.0', '<'), 'Job batches are only supported in Laravel 8+');
|
||||||
|
|
||||||
|
function getBatchRepositoryConnectionName()
|
||||||
private function getBatchRepositoryConnectionName(): string
|
|
||||||
{
|
{
|
||||||
$batchRepository = app(BatchRepository::class);
|
return app(BatchRepository::class)->getConnection()->getName();
|
||||||
|
|
||||||
$batchRepositoryReflection = new ReflectionClass($batchRepository);
|
|
||||||
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
|
|
||||||
$connectionProperty->setAccessible(true);
|
|
||||||
$connection = $connectionProperty->getValue($batchRepository);
|
|
||||||
|
|
||||||
return $connection->getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue