mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
[4.x] Migrate tests to Pest (#884)
* Add Pest dependencies * Add base Pest file * Convert test cases * Remove non-compound imports * Adopt expectation API * Optimize uses * Shift cleanup * phpunit -> pest * Fix tests in PR #884 PHPUnit to Pest Converter (#885) * fixed tests, remove method duplications, restore necessary inner classes * Update CommandsTest.php * temporary checks run on `shift-64622` on branch. * fixed `TestSeeder` class not resolved * fixed messed up names * removed `uses` from individual files and add it in `Pest` * extract tests to helpers * use pest dataset * Update AutomaticModeTest.php * newline * todo convention * resolve reviews * added `// todo@tests` * remove shift branch from CI workflow Co-authored-by: Samuel Štancl <samuel@archte.ch> * check if I have write permission * Convert newly added tests to Pest Co-authored-by: Shift <shift@laravelshift.com> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com>
This commit is contained in:
parent
69de181b7d
commit
b47c5549ef
32 changed files with 3010 additions and 3478 deletions
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
|
|
@ -12,113 +10,99 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class AutomaticModeTest extends TestCase
|
||||
beforeEach(function () {
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
test('context is switched when tenancy is initialized', function () {
|
||||
contextIsSwitchedWhenTenancyInitialized();
|
||||
});
|
||||
|
||||
test('context is reverted when tenancy is ended', function () {
|
||||
contextIsSwitchedWhenTenancyInitialized();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(app('tenancy_ended'))->toBe(true);
|
||||
});
|
||||
|
||||
test('context is switched when tenancy is reinitialized', function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
|
||||
|
||||
$tenant2 = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
expect(app('tenancy_initialized_for_tenant'))->toBe('foobar');
|
||||
});
|
||||
|
||||
test('central helper runs callbacks in the central state', function () {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
expect(tenant())->toBe(null);
|
||||
});
|
||||
|
||||
expect(tenant())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('central helper returns the value from the callback', function () {
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
$this->assertSame('foo', tenancy()->central(function () {
|
||||
return 'foo';
|
||||
}));
|
||||
});
|
||||
|
||||
test('central helper reverts back to tenant context', function () {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
expect(tenant())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('central helper doesnt change tenancy state when called in central context', function () {
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
expect(tenant())->toBeNull();
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
expect(tenancy()->initialized)->toBeFalse();
|
||||
expect(tenant())->toBeNull();
|
||||
});
|
||||
|
||||
// todo@tests
|
||||
function contextIsSwitchedWhenTenancyInitialized()
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
}
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
/** @test */
|
||||
public function context_is_switched_when_tenancy_is_initialized()
|
||||
{
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function context_is_reverted_when_tenancy_is_ended()
|
||||
{
|
||||
$this->context_is_switched_when_tenancy_is_initialized();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$this->assertSame(true, app('tenancy_ended'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function context_is_switched_when_tenancy_is_reinitialized()
|
||||
{
|
||||
config(['tenancy.bootstrappers' => [
|
||||
MyBootstrapper::class,
|
||||
]]);
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||
|
||||
$tenant2 = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_runs_callbacks_in_the_central_state()
|
||||
{
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
$this->assertSame(null, tenant());
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_returns_the_value_from_the_callback()
|
||||
{
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
$this->assertSame('foo', tenancy()->central(function () {
|
||||
return 'foo';
|
||||
}));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_reverts_back_to_tenant_context()
|
||||
{
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertSame($tenant, tenant());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function central_helper_doesnt_change_tenancy_state_when_called_in_central_context()
|
||||
{
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
|
||||
tenancy()->central(function () {
|
||||
//
|
||||
});
|
||||
|
||||
$this->assertFalse(tenancy()->initialized);
|
||||
$this->assertNull(tenant());
|
||||
}
|
||||
expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
|
||||
}
|
||||
|
||||
class MyBootstrapper implements TenancyBootstrapper
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue