Skip to content

Commit

Permalink
Merge pull request #321 from spatie/l9
Browse files Browse the repository at this point in the history
Add support for Laravel 9
  • Loading branch information
freekmurze authored Jan 19, 2022
2 parents a3f4352 + 29707fd commit 7937d70
Show file tree
Hide file tree
Showing 27 changed files with 104 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.1, 8.0]
laravel: [8.*]
laravel: [9.*, 8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.0",
"illuminate/support": "^8.0",
"illuminate/support": "^9.0|^8.0",
"spatie/laravel-package-tools": "^1.9"
},
"require-dev": {
Expand All @@ -30,7 +30,9 @@
},
"autoload": {
"psr-4": {
"Spatie\\Multitenancy\\": "src"
"Spatie\\Multitenancy\\": "src",
"Spatie\\Multitenancy\\Database\\Factories\\": "database/factories"

}
},
"autoload-dev": {
Expand Down
22 changes: 22 additions & 0 deletions database/factories/TenantFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Spatie\Multitenancy\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tests\TestClasses\User;

class TenantFactory extends Factory
{
protected $model = Tenant::class;

public function definition(): array
{
return [
'name' => $this->faker->name,
'domain' => $this->faker->unique()->domainName,
'database' => $this->faker->userName,
];
}
}
22 changes: 22 additions & 0 deletions database/factories/TenantNotifiableFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Spatie\Multitenancy\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Tests\Feature\Models\TenantNotifiable;
use Spatie\Multitenancy\Tests\TestClasses\User;

class TenantNotifiableFactory extends Factory
{
protected $model = TenantNotifiable::class;

public function definition(): array
{
return [
'name' => $this->faker->name,
'domain' => $this->faker->unique()->domainName,
'database' => $this->faker->userName,
];
}
}
23 changes: 23 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\Multitenancy\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Tests\TestClasses\User;

class UserFactory extends Factory
{
protected $model = User::class;

public function definition(): array
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}
1 change: 0 additions & 1 deletion src/Actions/MakeQueueTenantAwareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected function listenForJobsBeingQueued(): self

protected function listenForJobsBeingProcessed(): self
{
app('events')->listen('*', fn ($event) => dump($event));
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
if (! array_key_exists('tenantId', $event->job->payload())) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Multitenancy\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Multitenancy\Actions\ForgetCurrentTenantAction;
use Spatie\Multitenancy\Actions\MakeTenantCurrentAction;
Expand All @@ -11,6 +12,7 @@
class Tenant extends Model
{
use UsesLandlordConnection;
use HasFactory;

public function makeCurrent(): self
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Commands/TenantAwareCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function setUp(): void

config()->set('multitenancy.switch_tenant_tasks', [SwitchTenantDatabaseTask::class]);

$this->tenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant->makeCurrent();

$this->anotherTenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_2']);
$this->anotherTenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_2']);
$this->anotherTenant->makeCurrent();

Tenant::forgetCurrent();
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Commands/TenantsArtisanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function setUp(): void

config()->set('multitenancy.switch_tenant_tasks', [SwitchTenantDatabaseTask::class]);

$this->tenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant->makeCurrent();
Schema::connection('tenant')->dropIfExists('migrations');

$this->anotherTenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_2']);
$this->anotherTenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_2']);
$this->anotherTenant->makeCurrent();
Schema::connection('tenant')->dropIfExists('migrations');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function setUp(): void
Route::get('test-middleware', fn () => 'ok')->middleware(['web', EnsureValidTenantSession::class]);

/** @var \Spatie\Multitenancy\Models\Tenant $tenant */
$this->tenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_1']);

$this->tenant->makeCurrent();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Http/Middleware/NeedsTenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void

Route::get('middleware-test', fn () => 'ok')->middleware(NeedsTenant::class);

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/LandlordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setUp(): void
{
parent::setUp();

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();
}

/** @test */
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Models/TenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function setUp(): void
{
parent::setUp();

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();
}

/** @test */
Expand Down Expand Up @@ -78,10 +78,10 @@ public function it_can_check_if_a_current_tenant_has_been_set()
public function it_can_check_if_the_a_particular_tenant_is_the_current_one()
{
/** @var \Spatie\Multitenancy\Models\Tenant $tenant */
$tenant = factory(Tenant::class)->create();
$tenant = Tenant::factory()->create();

/** @var \Spatie\Multitenancy\Models\Tenant $anotherTenant */
$anotherTenant = factory(Tenant::class)->create();
$anotherTenant = Tenant::factory()->create();

$this->assertFalse($tenant->isCurrent());
$this->assertFalse($anotherTenant->isCurrent());
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Tasks/PrefixCacheTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function it_will_separate_the_cache_for_each_tenant()
cache()->put('key', 'original-value');

/** @var \Spatie\Multitenancy\Models\Tenant $tenant */
$tenant = factory(Tenant::class)->create();
$tenant = Tenant::factory()->create();
$tenant->makeCurrent();
$this->assertFalse(cache()->has('key'));
cache()->put('key', 'tenant-value');

/** @var \Spatie\Multitenancy\Models\Tenant $anotherTenant */
$anotherTenant = factory(Tenant::class)->create();
$anotherTenant = Tenant::factory()->create();
$anotherTenant->makeCurrent();
$this->assertFalse(cache()->has('key'));
cache()->put('key', 'another-tenant-value');
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Tasks/SwitchRouteCacheTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public function setUp(): void
public function it_will_use_a_different_routes_cache_environment_variable_for_each_tenant()
{
/** @var \Spatie\Multitenancy\Models\Tenant $tenant */
$tenant = factory(Tenant::class)->create();
$tenant = Tenant::factory()->create();
$tenant->makeCurrent();
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$tenant->id}.php", env('APP_ROUTES_CACHE'));

/** @var \Spatie\Multitenancy\Models\Tenant $anotherTenant */
$anotherTenant = factory(Tenant::class)->create();
$anotherTenant = Tenant::factory()->create();
$anotherTenant->makeCurrent();
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$anotherTenant->id}.php", env('APP_ROUTES_CACHE'));

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Tasks/SwitchTenantDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function setUp(): void
{
parent::setUp();

$this->tenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_1']);
$this->tenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_1']);

$this->anotherTenant = factory(Tenant::class)->create(['database' => 'laravel_mt_tenant_2']);
$this->anotherTenant = Tenant::factory()->create(['database' => 'laravel_mt_tenant_2']);

config()->set('multitenancy.switch_tenant_tasks', [SwitchTenantDatabaseTask::class]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function setUp(): void
{
parent::setUp();

Event::fake();
Event::fake(JobFailed::class);

config()->set('multitenancy.queues_are_tenant_aware_by_default', true);

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();

$this->valuestore = Valuestore::make($this->tempFile('tenantAware.json'))->flush();

Expand All @@ -53,7 +53,7 @@ public function it_will_inject_the_current_tenant_id_in_a_job()
public function it_will_inject_the_right_tenant_even_when_the_current_tenant_switches()
{
/** @var \Spatie\Multitenancy\Models\Tenant $anotherTenant */
$anotherTenant = factory(Tenant::class)->create();
$anotherTenant = Tenant::factory()->create();

$this->tenant->makeCurrent();
$job = new TestJob($this->valuestore);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/TenantAwareJobs/QueuedMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void
config()->set('queue.default', 'sync');
config()->set('mail.default', 'log');

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();
}

/** @test */
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/TenantAwareJobs/QueuedNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp(): void
config()->set('queue.default', 'sync');
config()->set('mail.default', 'log');

$this->tenant = factory(TenantNotifiable::class)->create();
$this->tenant = TenantNotifiable::factory()->create();
}

/** @test */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
config()->set('multitenancy.queues_are_tenant_aware_by_default', true);
config()->set('queue.default', 'sync');

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();

$this->valuestore = Valuestore::make($this->tempFile('tenantAware.json'))->flush();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/TenantAwareJobs/TenantAwareJobsByConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp(): void
config()->set('queue.default', 'sync');
config()->set('mail.default', 'log');

$this->tenant = factory(Tenant::class)->create();
$this->tenant = Tenant::factory()->create();
$this->valuestore = Valuestore::make($this->tempFile('tenantAware.json'))->flush();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/TenantCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setUp(): void
{
parent::setUp();

factory(Tenant::class, 3)->create();
Tenant::factory()->count(3)->create();

$this->tenants = Tenant::get();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/TenantFinder/DomainTenantFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp(): void
/** @test */
public function it_can_find_a_tenant_for_the_current_domain()
{
$tenant = factory(Tenant::class)->create(['domain' => 'my-domain.com']);
$tenant = Tenant::factory()->create(['domain' => 'my-domain.com']);

$request = Request::create('https://my-domain.com');

Expand All @@ -39,7 +39,7 @@ public function it_will_return_null_if_there_are_no_tenants()
/** @test */
public function it_will_return_null_if_no_tenant_can_be_found_for_the_current_domain()
{
$tenant = factory(Tenant::class)->create(['domain' => 'my-domain.com']);
$tenant = Tenant::factory()->create(['domain' => 'my-domain.com']);

$request = Request::create('https://another-domain.com');

Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Multitenancy\Tests;

use Illuminate\Console\Application as Artisan;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
Expand All @@ -19,7 +20,9 @@ public function setUp(): void
{
parent::setUp();

$this->withFactories(__DIR__ . '/database/factories');
Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Spatie\\Multitenancy\\Database\\Factories\\'.class_basename($modelName).'Factory'
);

$this->migrateDb();

Expand Down
12 changes: 0 additions & 12 deletions tests/database/factories/TenantFactory.php

This file was deleted.

12 changes: 0 additions & 12 deletions tests/database/factories/TenantNotifiableFactory.php

This file was deleted.

13 changes: 0 additions & 13 deletions tests/database/factories/UserFactory.php

This file was deleted.

0 comments on commit 7937d70

Please sign in to comment.