mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:04: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\Database\Eloquent\Model;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
|
@ -14,309 +12,293 @@ use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|||
use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
|
||||
|
||||
class SingleDatabaseTenancyTest extends TestCase
|
||||
beforeEach(function () {
|
||||
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
||||
|
||||
Schema::create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
|
||||
$table->string('tenant_id');
|
||||
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
|
||||
Schema::create('comments', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
|
||||
$table->unsignedInteger('post_id');
|
||||
|
||||
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
|
||||
config(['tenancy.tenant_model' => Tenant::class]);
|
||||
});
|
||||
|
||||
test('primary models are scoped to the current tenant', function () {
|
||||
primaryModelsScopedToCurrentTenant();
|
||||
});
|
||||
|
||||
test('primary models are not scoped in the central context', function () {
|
||||
primaryModelsScopedToCurrentTenant();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(Post::count())->toBe(2);
|
||||
});
|
||||
|
||||
test('secondary models are scoped to the current tenant when accessed via primary model', function () {
|
||||
secondaryModelsAreScopedToCurrentTenant();
|
||||
});
|
||||
|
||||
test('secondary models are not scoped to the current tenant when accessed directly', function () {
|
||||
secondaryModelsAreScopedToCurrentTenant();
|
||||
|
||||
// We're in acme context
|
||||
expect(tenant('id'))->toBe('acme');
|
||||
|
||||
expect(Comment::count())->toBe(2);
|
||||
});
|
||||
|
||||
test('secondary models a r e scoped to the current tenant when accessed directly and parent relationship traitis used', function () {
|
||||
$acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$acme->run(function () {
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->scoped_comments()->create(['text' => 'Comment Text']);
|
||||
|
||||
expect(Post::count())->toBe(1);
|
||||
expect(ScopedComment::count())->toBe(1);
|
||||
});
|
||||
|
||||
$foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]);
|
||||
|
||||
$foobar->run(function () {
|
||||
expect(Post::count())->toBe(0);
|
||||
expect(ScopedComment::count())->toBe(0);
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->scoped_comments()->create(['text' => 'Comment Text 2']);
|
||||
|
||||
expect(Post::count())->toBe(1);
|
||||
expect(ScopedComment::count())->toBe(1);
|
||||
});
|
||||
|
||||
// Global context
|
||||
expect(ScopedComment::count())->toBe(2);
|
||||
});
|
||||
|
||||
test('secondary models are not scoped in the central context', function () {
|
||||
secondaryModelsAreScopedToCurrentTenant();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(Comment::count())->toBe(2);
|
||||
});
|
||||
|
||||
test('global models are not scoped at all', function () {
|
||||
Schema::create('global_resources', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
});
|
||||
|
||||
GlobalResource::create(['text' => 'First']);
|
||||
GlobalResource::create(['text' => 'Second']);
|
||||
|
||||
$acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$acme->run(function () {
|
||||
expect(GlobalResource::count())->toBe(2);
|
||||
|
||||
GlobalResource::create(['text' => 'Third']);
|
||||
GlobalResource::create(['text' => 'Fourth']);
|
||||
});
|
||||
|
||||
expect(GlobalResource::count())->toBe(4);
|
||||
});
|
||||
|
||||
test('tenant id and relationship is auto added when creating primary resources in tenant context', function () {
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->relationLoaded('tenant'))->toBeTrue();
|
||||
expect($post->tenant)->toBe($acme);
|
||||
expect($post->tenant)->toBe(tenant());
|
||||
});
|
||||
|
||||
test('tenant id is not auto added when creating primary resources in central context', function () {
|
||||
$this->expectException(QueryException::class);
|
||||
|
||||
Post::create(['text' => 'Foo']);
|
||||
});
|
||||
|
||||
test('tenant id column name can be customized', function () {
|
||||
BelongsToTenant::$tenantIdColumn = 'team_id';
|
||||
|
||||
Schema::drop('comments');
|
||||
Schema::drop('posts');
|
||||
Schema::create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
|
||||
$table->string('team_id');
|
||||
|
||||
$table->foreign('team_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
expect($post->team_id)->toBe('acme');
|
||||
|
||||
// ======================================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
|
||||
expect($post->team_id)->toBe('foobar');
|
||||
|
||||
$post = Post::first();
|
||||
|
||||
expect($post->team_id)->toBe('foobar');
|
||||
|
||||
// ======================================
|
||||
// acme context again
|
||||
|
||||
tenancy()->initialize($acme);
|
||||
|
||||
$post = Post::first();
|
||||
expect($post->team_id)->toBe('acme');
|
||||
|
||||
// Assert foobar models are inaccessible in acme context
|
||||
expect(Post::count())->toBe(1);
|
||||
});
|
||||
|
||||
test('the model returned by the tenant helper has unique and exists validation rules', function () {
|
||||
Schema::table('posts', function (Blueprint $table) {
|
||||
$table->string('slug')->nullable();
|
||||
$table->unique(['tenant_id', 'slug']);
|
||||
});
|
||||
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
Post::create(['text' => 'Foo', 'slug' => 'foo']);
|
||||
$data = ['text' => 'Foo 2', 'slug' => 'foo'];
|
||||
|
||||
$uniqueFails = Validator::make($data, [
|
||||
'slug' => 'unique:posts',
|
||||
])->fails();
|
||||
$existsFails = Validator::make($data, [
|
||||
'slug' => 'exists:posts',
|
||||
])->fails();
|
||||
|
||||
// Assert that 'unique' and 'exists' aren't scoped by default
|
||||
// $this->assertFalse($uniqueFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
||||
// $this->assertTrue($existsFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
||||
|
||||
$uniqueFails = Validator::make($data, [
|
||||
'slug' => tenant()->unique('posts'),
|
||||
])->fails();
|
||||
$existsFails = Validator::make($data, [
|
||||
'slug' => tenant()->exists('posts'),
|
||||
])->fails();
|
||||
|
||||
// Assert that tenant()->unique() and tenant()->exists() are scoped
|
||||
expect($uniqueFails)->toBeTrue();
|
||||
expect($existsFails)->toBeFalse();
|
||||
});
|
||||
|
||||
// todo@tests
|
||||
function primaryModelsScopedToCurrentTenant()
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
BelongsToTenant::$tenantIdColumn = 'tenant_id';
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
Schema::create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
$table->string('tenant_id');
|
||||
$post = Post::first();
|
||||
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
Schema::create('comments', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
// ======================================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$table->unsignedInteger('post_id');
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
|
||||
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
expect($post->tenant_id)->toBe('foobar');
|
||||
expect($post->tenant->id)->toBe('foobar');
|
||||
|
||||
config(['tenancy.tenant_model' => Tenant::class]);
|
||||
}
|
||||
$post = Post::first();
|
||||
|
||||
/** @test */
|
||||
public function primary_models_are_scoped_to_the_current_tenant()
|
||||
{
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
expect($post->tenant_id)->toBe('foobar');
|
||||
expect($post->tenant->id)->toBe('foobar');
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
// ======================================
|
||||
// acme context again
|
||||
|
||||
$this->assertSame('acme', $post->tenant_id);
|
||||
$this->assertSame('acme', $post->tenant->id);
|
||||
tenancy()->initialize($acme);
|
||||
|
||||
$post = Post::first();
|
||||
$post = Post::first();
|
||||
expect($post->tenant_id)->toBe('acme');
|
||||
expect($post->tenant->id)->toBe('acme');
|
||||
|
||||
$this->assertSame('acme', $post->tenant_id);
|
||||
$this->assertSame('acme', $post->tenant->id);
|
||||
// Assert foobar models are inaccessible in acme context
|
||||
expect(Post::count())->toBe(1);
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
// todo@tests
|
||||
function secondaryModelsAreScopedToCurrentTenant()
|
||||
{
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->comments()->create(['text' => 'Comment text']);
|
||||
|
||||
$this->assertSame('foobar', $post->tenant_id);
|
||||
$this->assertSame('foobar', $post->tenant->id);
|
||||
// ================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::first();
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->comments()->create(['text' => 'Comment text 2']);
|
||||
|
||||
$this->assertSame('foobar', $post->tenant_id);
|
||||
$this->assertSame('foobar', $post->tenant->id);
|
||||
|
||||
// ======================================
|
||||
// acme context again
|
||||
|
||||
tenancy()->initialize($acme);
|
||||
|
||||
$post = Post::first();
|
||||
$this->assertSame('acme', $post->tenant_id);
|
||||
$this->assertSame('acme', $post->tenant->id);
|
||||
|
||||
// Assert foobar models are inaccessible in acme context
|
||||
$this->assertSame(1, Post::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function primary_models_are_not_scoped_in_the_central_context()
|
||||
{
|
||||
$this->primary_models_are_scoped_to_the_current_tenant();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$this->assertSame(2, Post::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model()
|
||||
{
|
||||
// acme context
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->comments()->create(['text' => 'Comment text']);
|
||||
|
||||
// ================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->comments()->create(['text' => 'Comment text 2']);
|
||||
|
||||
// ================
|
||||
// acme context again
|
||||
tenancy()->initialize($acme);
|
||||
$this->assertSame(1, Post::count());
|
||||
$this->assertSame(1, Post::first()->comments->count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function secondary_models_are_NOT_scoped_to_the_current_tenant_when_accessed_directly()
|
||||
{
|
||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
|
||||
// We're in acme context
|
||||
$this->assertSame('acme', tenant('id'));
|
||||
|
||||
$this->assertSame(2, Comment::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function secondary_models_ARE_scoped_to_the_current_tenant_when_accessed_directly_AND_PARENT_RELATIONSHIP_TRAIT_IS_USED()
|
||||
{
|
||||
$acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$acme->run(function () {
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
$post->scoped_comments()->create(['text' => 'Comment Text']);
|
||||
|
||||
$this->assertSame(1, Post::count());
|
||||
$this->assertSame(1, ScopedComment::count());
|
||||
});
|
||||
|
||||
$foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]);
|
||||
|
||||
$foobar->run(function () {
|
||||
$this->assertSame(0, Post::count());
|
||||
$this->assertSame(0, ScopedComment::count());
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
$post->scoped_comments()->create(['text' => 'Comment Text 2']);
|
||||
|
||||
$this->assertSame(1, Post::count());
|
||||
$this->assertSame(1, ScopedComment::count());
|
||||
});
|
||||
|
||||
// Global context
|
||||
$this->assertSame(2, ScopedComment::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function secondary_models_are_NOT_scoped_in_the_central_context()
|
||||
{
|
||||
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$this->assertSame(2, Comment::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function global_models_are_not_scoped_at_all()
|
||||
{
|
||||
Schema::create('global_resources', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
});
|
||||
|
||||
GlobalResource::create(['text' => 'First']);
|
||||
GlobalResource::create(['text' => 'Second']);
|
||||
|
||||
$acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$acme->run(function () {
|
||||
$this->assertSame(2, GlobalResource::count());
|
||||
|
||||
GlobalResource::create(['text' => 'Third']);
|
||||
GlobalResource::create(['text' => 'Fourth']);
|
||||
});
|
||||
|
||||
$this->assertSame(4, GlobalResource::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenant_id_and_relationship_is_auto_added_when_creating_primary_resources_in_tenant_context()
|
||||
{
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
$this->assertSame('acme', $post->tenant_id);
|
||||
$this->assertTrue($post->relationLoaded('tenant'));
|
||||
$this->assertSame($acme, $post->tenant);
|
||||
$this->assertSame(tenant(), $post->tenant);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenant_id_is_not_auto_added_when_creating_primary_resources_in_central_context()
|
||||
{
|
||||
$this->expectException(QueryException::class);
|
||||
|
||||
Post::create(['text' => 'Foo']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenant_id_column_name_can_be_customized()
|
||||
{
|
||||
BelongsToTenant::$tenantIdColumn = 'team_id';
|
||||
|
||||
Schema::drop('comments');
|
||||
Schema::drop('posts');
|
||||
Schema::create('posts', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('text');
|
||||
|
||||
$table->string('team_id');
|
||||
|
||||
$table->foreign('team_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Foo']);
|
||||
|
||||
$this->assertSame('acme', $post->team_id);
|
||||
|
||||
// ======================================
|
||||
// foobar context
|
||||
tenancy()->initialize($foobar = Tenant::create([
|
||||
'id' => 'foobar',
|
||||
]));
|
||||
|
||||
$post = Post::create(['text' => 'Bar']);
|
||||
|
||||
$this->assertSame('foobar', $post->team_id);
|
||||
|
||||
$post = Post::first();
|
||||
|
||||
$this->assertSame('foobar', $post->team_id);
|
||||
|
||||
// ======================================
|
||||
// acme context again
|
||||
|
||||
tenancy()->initialize($acme);
|
||||
|
||||
$post = Post::first();
|
||||
$this->assertSame('acme', $post->team_id);
|
||||
|
||||
// Assert foobar models are inaccessible in acme context
|
||||
$this->assertSame(1, Post::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function the_model_returned_by_the_tenant_helper_has_unique_and_exists_validation_rules()
|
||||
{
|
||||
Schema::table('posts', function (Blueprint $table) {
|
||||
$table->string('slug')->nullable();
|
||||
$table->unique(['tenant_id', 'slug']);
|
||||
});
|
||||
|
||||
tenancy()->initialize($acme = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]));
|
||||
|
||||
Post::create(['text' => 'Foo', 'slug' => 'foo']);
|
||||
$data = ['text' => 'Foo 2', 'slug' => 'foo'];
|
||||
|
||||
$uniqueFails = Validator::make($data, [
|
||||
'slug' => 'unique:posts',
|
||||
])->fails();
|
||||
$existsFails = Validator::make($data, [
|
||||
'slug' => 'exists:posts',
|
||||
])->fails();
|
||||
|
||||
// Assert that 'unique' and 'exists' aren't scoped by default
|
||||
// $this->assertFalse($uniqueFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
||||
// $this->assertTrue($existsFails); // todo get these two assertions to pass. for some reason, the validator is passing for both 'unique' and 'exists'
|
||||
|
||||
$uniqueFails = Validator::make($data, [
|
||||
'slug' => tenant()->unique('posts'),
|
||||
])->fails();
|
||||
$existsFails = Validator::make($data, [
|
||||
'slug' => tenant()->exists('posts'),
|
||||
])->fails();
|
||||
|
||||
// Assert that tenant()->unique() and tenant()->exists() are scoped
|
||||
$this->assertTrue($uniqueFails);
|
||||
$this->assertFalse($existsFails);
|
||||
}
|
||||
// ================
|
||||
// acme context again
|
||||
tenancy()->initialize($acme);
|
||||
expect(Post::count())->toBe(1);
|
||||
expect(Post::first()->comments->count())->toBe(1);
|
||||
}
|
||||
|
||||
class Tenant extends TestTenant
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue