1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:34:04 +00:00
tenancy/tests/BatchTest.php
lukinovec 342c67fe02
Add skip-failing option to the Migrate command (#945)
* Add and test Migrate command's skip-failing option

* Improve naming

* Move migration event dispatching inside try block

* Change test name

* Fix skip-failing test

* Use QueryException instead of Exception

* Correct TenantDatabaseDoesNotExistException import

* Correct test

* Check for the the testing env in DB bootstrapper

* Correct the Migrate command

* Fix code style (php-cs-fixer)

* add docs todo

* Add QueryException to the Migrat command try/catch

* Return status codes in Migrate

* Fix code style (php-cs-fixer)

* Add test for not stopping tenants:migrate after the first failure

* Update Migrate command

* Fix code style (php-cs-fixer)

* Fix code style (php-cs-fixer)

* Use `getTenants()`

* Use withtenantDatabases where needed

* Add withTenantDatabases to test

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
2023-02-01 06:55:26 +01:00

46 lines
1.4 KiB
PHP

<?php
use Illuminate\Bus\BatchRepository;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
config([
'tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
BatchTenancyBootstrapper::class,
],
]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
test('batch repository is set to tenant connection and reverted', function () {
withTenantDatabases();
$tenant = Tenant::create();
$tenant2 = Tenant::create();
expect(getBatchRepositoryConnectionName())->toBe('central');
tenancy()->initialize($tenant);
expect(getBatchRepositoryConnectionName())->toBe('tenant');
tenancy()->initialize($tenant2);
expect(getBatchRepositoryConnectionName())->toBe('tenant');
tenancy()->end();
expect(getBatchRepositoryConnectionName())->toBe('central');
});
function getBatchRepositoryConnectionName()
{
return app(BatchRepository::class)->getConnection()->getName();
}