1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 23:14:03 +00:00
tenancy/tests/EventListenerTest.php
lukinovec e61a26d604
Add L10 support to 4.x (merge 3.x to master) (#1071)
* exclude master from CI

* Add space after 'up' in 'docker-compose up-d' (#900)

* Fix ArgumentCountError on the TenantAssetsController (#894)

* Fix ArgumentCount exception on the TenantAssetsController when no `$path` is provided

* CS

* CS

* Handle null case explicitly

* code style

Co-authored-by: Bram Wubs <bram@sibi.nl>
Co-authored-by: Samuel Štancl <samuel@archte.ch>

* Add support for nested tenant config override (#920)

* feat: add support for nested tenant config override

* test: ensure nested tenant values are mapped

* fix: typo mistake (#954)

* [3.x] Add Vite helper for tenancy (#956)

* Add Vite helper for tenancy

* Move Vite bundler to an Optional Feature

* Rename to foundation vite

* Add ViteBundlerTest

* Add missing end of file

* Update tests

* remove unnecessary end() call

Co-authored-by: Samuel Štancl <samuel@archte.ch>

* rewrite ViteBundlerTest to phpunit syntax

* skip vite test in Laravel < 9

* convert ViteBundler to PHP 7 syntax

* remove import of nonexistent class in older Laravel versions

* remove import of Foundation\Vite in tests

* try to exclude Vite.php from coverage report

* remove typehint

* update channel name

* Cache crash fix (#1048)

* Don't prevent accessing missing Tenant attributes. (#1045)

* [3.x] L10 compatibility (#1065)

* Bump dependencies for Laravel 10

* Update GitHub Actions for Laravel 10

* ci: do not test L10 using PHP 7.3

* drop < L9 support

* use `dispatch_sync` instead of `dispatch_now`

* migrate phpunit configuration

* Update ci.yml

* drop laravel < 9 support

* misc L10 fixes, new docker image

* specify odbc version

* wip

* properly list php versions as strings

* minor changes

* Add `getValue($queryGrammar)` to raw query

* Clean up `isVersion8` code

* rewrite hasFailed assertion

* phpunit schema update

* Upgrade `doctrine/dbal`

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
Co-authored-by: lukinovec <lukinovec@gmail.com>

* Update ci.yml

* Fix code style (php-cs-fixer)

* Update dependencies

* Change invade version

* Delete ViteBundlerTest

* Fix PHPStan error

* Delete PHPStan error ignore

* Fix CONTRIBUTING.md

* Delete ViteBundler remains

* Bring back ViteBundler

* Convert ViteBundlerTest to Pest

* Update ci.yml

---------

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
Co-authored-by: Bram Wubs <megawubs@users.noreply.github.com>
Co-authored-by: Bram Wubs <bram@sibi.nl>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
Co-authored-by: George Bishop <email.georgebishop@gmail.com>
Co-authored-by: Anbuselvan Rocky <15264938+anburocky3@users.noreply.github.com>
Co-authored-by: Wilsen Hernández <13445515+wilsenhc@users.noreply.github.com>
Co-authored-by: Joel Stein <joel@mediatrix.digital>
Co-authored-by: Guilherme Saade <saade@outlook.com.br>
Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
2023-02-20 23:47:10 +01:00

191 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Events\CallQueuedListener;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Events\BootstrappingTenancy;
use Stancl\Tenancy\Events\CreatingDatabase;
use Stancl\Tenancy\Events\CreatingTenant;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\UpdatingDomain;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Jobs\MigrateDatabase;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\QueueableListener;
use Stancl\Tenancy\Tests\Etc\Tenant;
test('listeners can be synchronous', function () {
Queue::fake();
Event::listen(TenantCreated::class, FooListener::class);
Tenant::create();
Queue::assertNothingPushed();
expect(app('foo'))->toBe('bar');
});
test('listeners can be queued by setting a static property', function () {
Queue::fake();
Event::listen(TenantCreated::class, FooListener::class);
FooListener::$shouldQueue = true;
Tenant::create();
Queue::assertPushed(CallQueuedListener::class, function (CallQueuedListener $job) {
return $job->class === FooListener::class;
});
expect(app()->bound('foo'))->toBeFalse();
});
test('ing events can be used to cancel tenant model actions', function () {
Event::listen(CreatingTenant::class, function () {
return false;
});
expect(Tenant::create()->exists)->toBe(false);
expect(Tenant::count())->toBe(0);
});
test('ing events can be used to cancel domain model actions', function () {
$tenant = Tenant::create();
Event::listen(UpdatingDomain::class, function () {
return false;
});
$domain = $tenant->domains()->create([
'domain' => 'acme',
]);
$domain->update([
'domain' => 'foo',
]);
expect($domain->refresh()->domain)->toBe('acme');
});
test('ing events can be used to cancel db creation', function () {
Event::listen(CreatingDatabase::class, function (CreatingDatabase $event) {
$event->tenant->setInternal('create_database', false);
});
$tenant = Tenant::create();
dispatch_sync(new CreateDatabase($tenant));
pest()->assertFalse($tenant->database()->manager()->databaseExists(
$tenant->database()->getName()
));
});
test('ing events can be used to cancel tenancy bootstrapping', function () {
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
RedisTenancyBootstrapper::class,
]]);
Event::listen(
TenantCreated::class,
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(BootstrappingTenancy::class, function (BootstrappingTenancy $event) {
$event->tenancy->getBootstrappersUsing = function () {
return [DatabaseTenancyBootstrapper::class];
};
});
tenancy()->initialize(Tenant::create());
expect(array_map('get_class', tenancy()->getBootstrappers()))->toBe([DatabaseTenancyBootstrapper::class]);
});
test('individual job pipelines can terminate while leaving others running', function () {
$executed = [];
Event::listen(
TenantCreated::class,
JobPipeline::make([
function () use (&$executed) {
$executed[] = 'P1J1';
},
function () use (&$executed) {
$executed[] = 'P1J2';
},
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(
TenantCreated::class,
JobPipeline::make([
function () use (&$executed) {
$executed[] = 'P2J1';
return false;
},
function () use (&$executed) {
$executed[] = 'P2J2';
},
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Tenant::create();
pest()->assertSame([
'P1J1',
'P1J2',
'P2J1', // termminated after this
// P2J2 was not reached
], $executed);
});
test('database is not migrated if creation is disabled', function () {
Event::listen(
TenantCreated::class,
JobPipeline::make([
CreateDatabase::class,
function () {
pest()->fail("The job pipeline didn't exit.");
},
MigrateDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
$tenant = Tenant::create([
'tenancy_create_database' => false,
'tenancy_db_name' => 'already_created',
]);
// assert test didn't fail
$this->assertTrue($tenant->exists());
});
class FooListener extends QueueableListener
{
public static bool $shouldQueue = false;
public function handle()
{
app()->instance('foo', 'bar');
}
}