From d0de09aa5360b17033b81726d38b18272462f6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 1 Jun 2022 15:36:46 +0200 Subject: [PATCH 1/8] remove old versions from CI --- .github/workflows/ci.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc8ad985..81d37af5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,9 @@ env: on: push: - branches: [ 3.x, 2.x, master ] + branches: [ master ] pull_request: - branches: [ 3.x, 2.x, master ] + branches: [ master ] jobs: tests: @@ -15,11 +15,8 @@ jobs: strategy: matrix: - php: ["7.3", "8.0"] - laravel: ["^6.0", "^8.0", "^9.0"] - exclude: - - laravel: "^9.0" - php: "7.3" + php: ["8.1"] + laravel: ["^9.0"] steps: - uses: actions/checkout@v2 From 7d98ebb5d177a1ac30ec0a7d248c4cd254abce4a Mon Sep 17 00:00:00 2001 From: Victor R <39545521+viicslen@users.noreply.github.com> Date: Wed, 1 Jun 2022 10:12:59 -0400 Subject: [PATCH 2/8] [4.x] Add tenant schema dump command (#807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add tenant dump command * Register tenant schema dump command * Added tests for tenant schema dump command * remove docblocks, fix tenant() logic * trigger ci * Install mysql-client * mysql-client -> mariadb-client * add tenant-schema-test.dump to .gitignore Co-authored-by: Samuel Štancl Co-authored-by: Samuel Štancl --- .gitignore | 1 + Dockerfile | 2 +- src/Commands/TenantDump.php | 54 ++++++++++++++++++++++++++++ src/TenancyServiceProvider.php | 1 + tests/CommandsTest.php | 32 +++++++++++++++++ tests/Etc/tenant-schema.dump | 66 ++++++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/Commands/TenantDump.php create mode 100644 tests/Etc/tenant-schema.dump diff --git a/.gitignore b/.gitignore index f470ba75..95522c34 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,6 @@ psysh phpunit_var_*.xml coverage/ clover.xml +tenant-schema-test.dump tests/Etc/tmp/queuetest.json docker-compose.override.yml diff --git a/Dockerfile b/Dockerfile index 36f52d6a..a636bc45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ ENV LANG=en_GB.UTF-8 # install some OS packages we need RUN apt-get update -RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git +RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git # install php extensions RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # && if [ "${PHP_VERSION}" = "7.4" ]; then docker-php-ext-configure gd --with-freetype --with-jpeg; else docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/; fi \ diff --git a/src/Commands/TenantDump.php b/src/Commands/TenantDump.php new file mode 100644 index 00000000..557c6975 --- /dev/null +++ b/src/Commands/TenantDump.php @@ -0,0 +1,54 @@ +setName('tenants:dump'); + $this->specifyParameters(); + } + + + public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int + { + $this->tenant()->run(fn() => parent::handle($connections, $dispatcher)); + + return Command::SUCCESS; + } + + public function tenant(): Tenant + { + $tenant = $this->option('tenant') + ?? tenant() + ?? $this->ask('What tenant do you want to dump the schema for?') + ?? tenancy()->query()->first(); + + if (! $tenant instanceof Tenant) { + $tenant = tenancy()->find($tenant); + } + + throw_if(! $tenant, 'Could not identify the tenant to use for dumping the schema.'); + + return $tenant; + } + + protected function getOptions(): array + { + return array_merge([ + ['tenant', null, InputOption::VALUE_OPTIONAL, '', null], + ], parent::getOptions()); + } +} diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 4faaccf3..dd061af3 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -88,6 +88,7 @@ class TenancyServiceProvider extends ServiceProvider Commands\Migrate::class, Commands\Rollback::class, Commands\TenantList::class, + Commands\TenantDump::class, Commands\MigrateFresh::class, ]); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d7da0cab..145a93c5 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -91,6 +91,38 @@ class CommandsTest extends TestCase $this->assertTrue(Schema::hasTable('users')); } + /** @test */ + public function migrate_command_loads_schema_state() + { + $tenant = Tenant::create(); + + $this->assertFalse(Schema::hasTable('schema_users')); + $this->assertFalse(Schema::hasTable('users')); + + Artisan::call('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'); + + $this->assertFalse(Schema::hasTable('schema_users')); + $this->assertFalse(Schema::hasTable('users')); + + tenancy()->initialize($tenant); + + // Check for both tables to see if missing migrations also get executed + $this->assertTrue(Schema::hasTable('schema_users')); + $this->assertTrue(Schema::hasTable('users')); + } + + /** @test */ + public function dump_command_works() + { + $tenant = Tenant::create(); + Artisan::call('tenants:migrate'); + + tenancy()->initialize($tenant); + + Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"'); + $this->assertFileExists('tests/Etc/tenant-schema-test.dump'); + } + /** @test */ public function rollback_command_works() { diff --git a/tests/Etc/tenant-schema.dump b/tests/Etc/tenant-schema.dump new file mode 100644 index 00000000..6af9f019 --- /dev/null +++ b/tests/Etc/tenant-schema.dump @@ -0,0 +1,66 @@ +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +DROP TABLE IF EXISTS `failed_jobs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `failed_jobs` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `uuid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `connection` text COLLATE utf8mb4_unicode_ci NOT NULL, + `queue` text COLLATE utf8mb4_unicode_ci NOT NULL, + `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `migrations`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `migrations` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `batch` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `password_resets`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `password_resets` ( + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + KEY `password_resets_email_index` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `schema_users` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email_verified_at` timestamp NULL DEFAULT NULL, + `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `users_email_unique` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +INSERT INTO `migrations` VALUES (2,'2014_10_12_100000_testbench_create_password_resets_table',1); +INSERT INTO `migrations` VALUES (3,'2019_08_19_000000_testbench_create_failed_jobs_table',1); From 72c41ea9938eef4964ef9469d829c16536f69e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 13 Jun 2022 19:16:35 +0200 Subject: [PATCH 3/8] Discord link --- SUPPORT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SUPPORT.md b/SUPPORT.md index b7caaa5c..24be468b 100644 --- a/SUPPORT.md +++ b/SUPPORT.md @@ -1,5 +1,5 @@ # Get Support -If you need help with implementing the package, you can join our community [Discord server](https://discord.gg/7cpgPxv) and ask in `#help`. +If you need help with implementing the package, you can join our community [Discord server](https://archte.ch/discord) and ask in `#help`. If you're interested in paid consulting from the maintainer, see the [contact page](https://tenancyforlaravel.com/contact/) on our website. From cc6d4fe0ddcec405f645fbd2d8e90ab71afb1bec Mon Sep 17 00:00:00 2001 From: Nick Kitchen <61636526+nickakitch@users.noreply.github.com> Date: Thu, 23 Jun 2022 21:04:53 +1000 Subject: [PATCH 4/8] [4.x] Added support for Microsoft Sql Server (#715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added support for microsoft sql server database * added support for microsoft sql server database * trigger ci * revert change * trigger ci * Try installing pdo_sqlsrv * different approach for installing sqlsrv via pecl * add dependencies * add gnupg2 * Update Dockerfile * try skipping msodbcsql17 * Update Dockerfile * add dependency back * update before installing * try to add mssql * mssql host * TENANCY_TEST_MSSQL_HOST env var * add env vars for mssql * add sqlsrv vars to TestCase * rename vars to SQLSRV [skip ci] * MSSQL -> SQLSRV Co-authored-by: Samuel Štancl Co-authored-by: Samuel Štancl --- Dockerfile | 21 +++++-- assets/config.php | 1 + docker-compose.yml | 11 ++++ .../MicrosoftSQLDatabaseManager.php | 57 +++++++++++++++++++ tests/TenantDatabaseManagerTest.php | 2 + tests/TestCase.php | 4 ++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php diff --git a/Dockerfile b/Dockerfile index a636bc45..fb63afe3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ ARG PHP_VERSION=7.4 ARG PHP_TARGET=php:${PHP_VERSION}-cli -FROM ${PHP_TARGET} +FROM --platform=linux/amd64 ${PHP_TARGET} ARG COMPOSER_TARGET=2.0.3 @@ -22,10 +22,16 @@ ENV LANG=en_GB.UTF-8 # Dockerfile _and pin the versions_! Eg: # RUN pecl install memcached-2.2.0 && docker-php-ext-enable memcached -# install some OS packages we need -RUN apt-get update -RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git - # install php extensions + +RUN apt-get update \ + && apt-get install -y gnupg2 \ + && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \ + && curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \ + && apt-get update \ + && ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17 + +RUN apt-get install -y --no-install-recommends locales apt-transport-https libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git + RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # && if [ "${PHP_VERSION}" = "7.4" ]; then docker-php-ext-configure gd --with-freetype --with-jpeg; else docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/; fi \ && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pdo_sqlite pgsql zip gmp bcmath pcntl ldap sysvmsg exif \ @@ -35,7 +41,10 @@ RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # install the pcov extention && pecl install pcov \ && docker-php-ext-enable pcov \ - && echo "pcov.enabled = 1" > /usr/local/etc/php/conf.d/pcov.ini + && echo "pcov.enabled = 1" > /usr/local/etc/php/conf.d/pcov.ini \ + # install sqlsrv + && pecl install sqlsrv pdo_sqlsrv \ + && docker-php-ext-enable sqlsrv pdo_sqlsrv # clear the apt cache RUN rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/apt/lists/* \ diff --git a/assets/config.php b/assets/config.php index 85592d14..e1c82e6b 100644 --- a/assets/config.php +++ b/assets/config.php @@ -61,6 +61,7 @@ return [ 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class, 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, + 'sqlsrv' => Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class, /** * Use this database manager for MySQL to have a DB user created for each tenant database. diff --git a/docker-compose.yml b/docker-compose.yml index e8e8d418..7b635637 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,9 @@ services: TENANCY_TEST_REDIS_HOST: redis TENANCY_TEST_MYSQL_HOST: mysql TENANCY_TEST_PGSQL_HOST: postgres + TENANCY_TEST_SQLSRV_HOST: mssql + TENANCY_TEST_SQLSRV_USERNAME: sa + TENANCY_TEST_SQLSRV_PASSWORD: P@ssword stdin_open: true tty: true mysql: @@ -64,3 +67,11 @@ services: interval: 1s timeout: 3s retries: 30 + mssql: + image: mcr.microsoft.com/mssql/server:2019-latest + ports: + - 1433:1433 + environment: + - ACCEPT_EULA=Y + - SA_PASSWORD=P@ssword # todo reuse values from env above + # todo missing health check diff --git a/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php b/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php new file mode 100644 index 00000000..0bc34623 --- /dev/null +++ b/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php @@ -0,0 +1,57 @@ +connection === null) { + throw new NoConnectionSetException(static::class); + } + + return DB::connection($this->connection); + } + + public function setConnection(string $connection): void + { + $this->connection = $connection; + } + + public function createDatabase(TenantWithDatabase $tenant): bool + { + $database = $tenant->database()->getName(); + $charset = $this->database()->getConfig('charset'); + $collation = $this->database()->getConfig('collation'); + + return $this->database()->statement("CREATE DATABASE [{$database}]"); + } + + public function deleteDatabase(TenantWithDatabase $tenant): bool + { + return $this->database()->statement("DROP DATABASE [{$tenant->database()->getName()}]"); + } + + public function databaseExists(string $name): bool + { + return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'"); + } + + public function makeConnectionConfig(array $baseConfig, string $databaseName): array + { + $baseConfig['database'] = $databaseName; + + return $baseConfig; + } +} diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 3d45d96f..0e1464c0 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -19,6 +19,7 @@ use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager; @@ -103,6 +104,7 @@ class TenantDatabaseManagerTest extends TestCase ['sqlite', SQLiteDatabaseManager::class], ['pgsql', PostgreSQLDatabaseManager::class], ['pgsql', PostgreSQLSchemaManager::class], + ['sqlsrv', MicrosoftSQLDatabaseManager::class] ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index cea669a1..75fe51fd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -81,6 +81,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase ], 'database.connections.sqlite.database' => ':memory:', 'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'), + 'database.connections.sqlsrv.username' => env('TENANCY_TEST_SQLSRV_USERNAME', 'sa'), + 'database.connections.sqlsrv.password' => env('TENANCY_TEST_SQLSRV_PASSWORD', 'P@ssword'), + 'database.connections.sqlsrv.host' => env('TENANCY_TEST_SQLSRV_HOST', '127.0.0.1'), + 'database.connections.sqlsrv.database' => null, 'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'), 'tenancy.filesystem.disks' => [ 'local', From 4aec6bfda20e659c4e62d2527ead99fbf44926cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 28 Jun 2022 21:47:33 +0200 Subject: [PATCH 5/8] add phpunit dependency --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 8e932658..8123944a 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "require-dev": { "laravel/framework": "^6.0|^7.0|^8.0|^9.0", "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", + "phpunit/phpunit": "*", "league/flysystem-aws-s3-v3": "^1.0|^3.0", "doctrine/dbal": "^2.10", "spatie/valuestore": "^1.2.5" From 627233d07aa173893cc2afd6484d44f8455c7362 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 20 Jul 2022 16:02:33 +0500 Subject: [PATCH 6/8] [4.x] Don't use onDeleteCascade in the migrations (#883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * removed `cascade` on delete for domains * removed only `onDelete` cascade * keep impersonation migrations unchanged * domains set null on delete * Update 2019_09_15_000020_create_domains_table.php * Added DeleteDomain Job while deleting tenant. * Update assets/TenancyServiceProvider.stub.php Co-authored-by: Samuel Štancl * renamed class * Update DeleteDomains.php * onDelete restrict * revert nullable * removed `shouldQueue` interface * Update TenancyServiceProvider.stub.php * fetch and delete domains individually * Update DeleteDomains.php * tests for `DeleteDomains` job Co-authored-by: Samuel Štancl --- assets/TenancyServiceProvider.stub.php | 8 +++- ...2019_09_15_000020_create_domains_table.php | 2 +- src/Jobs/DeleteDomains.php | 35 ++++++++++++++++ tests/DeleteDomainsJobTest.php | 42 +++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/Jobs/DeleteDomains.php create mode 100644 tests/DeleteDomainsJobTest.php diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 1d15f418..865bb93d 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -40,7 +40,13 @@ class TenancyServiceProvider extends ServiceProvider Events\TenantSaved::class => [], Events\UpdatingTenant::class => [], Events\TenantUpdated::class => [], - Events\DeletingTenant::class => [], + Events\DeletingTenant::class => [ + JobPipeline::make([ + Jobs\DeleteDomains::class, + ])->send(function (Events\DeletingTenant $event) { + return $event->tenant; + })->shouldBeQueued(false), + ], Events\TenantDeleted::class => [ JobPipeline::make([ Jobs\DeleteDatabase::class, diff --git a/assets/migrations/2019_09_15_000020_create_domains_table.php b/assets/migrations/2019_09_15_000020_create_domains_table.php index 77c1b88a..17f706c2 100644 --- a/assets/migrations/2019_09_15_000020_create_domains_table.php +++ b/assets/migrations/2019_09_15_000020_create_domains_table.php @@ -21,7 +21,7 @@ class CreateDomainsTable extends Migration $table->string('tenant_id'); $table->timestamps(); - $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); + $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade'); }); } diff --git a/src/Jobs/DeleteDomains.php b/src/Jobs/DeleteDomains.php new file mode 100644 index 00000000..fac60e43 --- /dev/null +++ b/src/Jobs/DeleteDomains.php @@ -0,0 +1,35 @@ +tenant = $tenant; + } + + public function handle() + { + $this->tenant->domains->each->delete(); + } +} diff --git a/tests/DeleteDomainsJobTest.php b/tests/DeleteDomainsJobTest.php new file mode 100644 index 00000000..7fce9cf3 --- /dev/null +++ b/tests/DeleteDomainsJobTest.php @@ -0,0 +1,42 @@ + DatabaseAndDomainTenant::class]); + } + + /** @test */ + public function job_delete_domains_successfully() + { + $tenant = DatabaseAndDomainTenant::create(); + + $tenant->domains()->create([ + 'domain' => 'foo.localhost', + ]); + $tenant->domains()->create([ + 'domain' => 'bar.localhost', + ]); + + $this->assertSame($tenant->domains()->count(), 2); + + (new DeleteDomains($tenant))->handle(); + + $this->assertSame($tenant->refresh()->domains()->count(), 0); + } +} + +class DatabaseAndDomainTenant extends Etc\Tenant +{ + use HasDomains; +} From 97ab483173b38d9233082b349a9f73f268784449 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 20 Jul 2022 18:28:45 +0500 Subject: [PATCH 7/8] Completing PR #881 (#902) * install PHP CS Fixer * Fix styling * remove StyleCI config * use config from archtechx/template * Fix styling * added `php-cs-fixer` * Update .php-cs-fixer.php * added GitHub token * Update ci.yml * Update ci.yml * Update ci.yml * php-cs-fixer workflow same as template Co-authored-by: Erik Gaal Co-authored-by: erikgaal --- .github/workflows/ci.yml | 18 +++ .gitignore | 1 + .php-cs-fixer.php | 141 ++++++++++++++++++ .styleci.yml | 7 - .../FilesystemTenancyBootstrapper.php | 2 +- .../QueueTenancyBootstrapper.php | 9 +- src/CacheManager.php | 3 +- src/Commands/Install.php | 2 - src/Commands/Migrate.php | 2 - src/Commands/MigrateFresh.php | 2 - src/Commands/Rollback.php | 2 - src/Commands/Run.php | 2 - src/Commands/Seed.php | 2 - src/Commands/TenantDump.php | 3 +- src/Commands/TenantList.php | 2 - src/Concerns/ExtendsLaravelCommand.php | 2 + src/Concerns/HasATenantsOption.php | 2 +- src/Contracts/TenantDatabaseManager.php | 10 -- src/Database/Concerns/TenantRun.php | 3 - src/Database/Models/ImpersonationToken.php | 5 + src/Database/Models/Tenant.php | 2 + src/DatabaseConfig.php | 5 +- src/Features/UserImpersonation.php | 1 - src/Listeners/UpdateSyncedResource.php | 3 +- .../CheckTenantForMaintenanceMode.php | 2 +- src/Middleware/InitializeTenancyByDomain.php | 6 +- .../InitializeTenancyByDomainOrSubdomain.php | 2 - src/Middleware/InitializeTenancyByPath.php | 4 +- .../InitializeTenancyByRequestData.php | 2 - .../InitializeTenancyBySubdomain.php | 2 - .../Contracts/CachedTenantResolver.php | 1 - src/Tenancy.php | 5 - src/TenancyServiceProvider.php | 4 - tests/BootstrapperTest.php | 26 ++-- tests/Etc/ExampleSeeder.php | 2 +- tests/EventListenerTest.php | 1 - tests/MaintenanceModeTest.php | 4 +- tests/QueueTest.php | 36 +++-- tests/ResourceSyncingTest.php | 4 + tests/SingleDatabaseTenancyTest.php | 3 + tests/TenantDatabaseManagerTest.php | 4 +- tests/TenantModelTest.php | 1 + tests/TenantUserImpersonationTest.php | 2 + 43 files changed, 231 insertions(+), 111 deletions(-) create mode 100644 .php-cs-fixer.php delete mode 100644 .styleci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81d37af5..3cbda814 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,8 @@ name: CI env: COMPOSE_INTERACTIVE_NO_CLI: 1 + PHP_CS_FIXER_IGNORE_ENV: 1 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} on: push: @@ -26,3 +28,19 @@ jobs: run: docker-compose exec -T test composer require --no-interaction "laravel/framework:${{ matrix.laravel }}" - name: Run tests run: ./test + + php-cs-fixer: + name: Code style (php-cs-fixer) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install php-cs-fixer + run: composer global require friendsofphp/php-cs-fixer + - name: Run php-cs-fixer + run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php + - name: Commit changes from php-cs-fixer + uses: EndBug/add-and-commit@v5 + with: + author_name: "PHP CS Fixer" + author_email: "phpcsfixer@example.com" + message: Fix code style (php-cs-fixer) diff --git a/.gitignore b/.gitignore index 95522c34..64d9dc21 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ clover.xml tenant-schema-test.dump tests/Etc/tmp/queuetest.json docker-compose.override.yml +.php-cs-fixer.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 00000000..589838bc --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,141 @@ + ['syntax' => 'short'], + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ] + ], + 'blank_line_after_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'no_superfluous_phpdoc_tags' => true, + 'blank_line_before_statement' => [ + 'statements' => ['return'] + ], + 'braces' => true, + 'cast_spaces' => true, + 'class_definition' => true, + 'concat_space' => [ + 'spacing' => 'one' + ], + 'declare_equal_normalize' => true, + 'elseif' => true, + 'encoding' => true, + 'full_opening_tag' => true, + 'declare_strict_types' => true, + 'fully_qualified_strict_types' => true, // added by Shift + 'function_declaration' => true, + 'function_typehint_space' => true, + 'heredoc_to_nowdoc' => true, + 'include' => true, + 'increment_style' => ['style' => 'post'], + 'indentation_type' => true, + 'linebreak_after_opening_tag' => true, + 'line_ending' => true, + 'lowercase_cast' => true, + 'constant_case' => true, + 'lowercase_keywords' => true, + 'lowercase_static_reference' => true, // added from Symfony + 'magic_method_casing' => true, // added from Symfony + 'magic_constant_casing' => true, + 'method_argument_space' => true, + 'native_function_casing' => true, + 'no_alias_functions' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'extra', + 'throw', + 'use', + 'use_trait', + ] + ], + 'no_blank_lines_after_class_opening' => true, + 'no_blank_lines_after_phpdoc' => true, + 'no_closing_tag' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_leading_import_slash' => true, + 'no_leading_namespace_whitespace' => true, + 'no_mixed_echo_print' => [ + 'use' => 'echo' + ], + 'no_multiline_whitespace_around_double_arrow' => true, + 'multiline_whitespace_before_semicolons' => [ + 'strategy' => 'no_multi_line' + ], + 'no_short_bool_cast' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_spaces_after_function_name' => true, + 'no_spaces_around_offset' => true, + 'no_spaces_inside_parenthesis' => true, + 'no_trailing_comma_in_list_call' => true, + 'no_trailing_comma_in_singleline_array' => true, + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'no_unneeded_control_parentheses' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_return' => true, + 'no_whitespace_before_comma_in_array' => true, + 'no_whitespace_in_blank_line' => true, + 'normalize_index_brace' => true, + 'not_operator_with_successor_space' => true, + 'object_operator_without_whitespace' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'phpdoc_indent' => true, + 'general_phpdoc_tag_rename' => true, + 'phpdoc_no_access' => true, + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_scalar' => true, + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_summary' => true, + 'phpdoc_to_comment' => false, + 'phpdoc_trim' => true, + 'phpdoc_types' => true, + 'phpdoc_var_without_name' => true, + 'psr_autoloading' => true, + 'self_accessor' => true, + 'short_scalar_cast' => true, + 'simplified_null_return' => false, // disabled by Shift + 'single_blank_line_at_eof' => true, + 'single_blank_line_before_namespace' => true, + 'single_class_element_per_statement' => true, + 'single_import_per_statement' => true, + 'single_line_after_imports' => true, + 'no_unused_imports' => true, + 'single_line_comment_style' => [ + 'comment_types' => ['hash'] + ], + 'single_quote' => true, + 'space_after_semicolon' => true, + 'standardize_not_equals' => true, + 'switch_case_semicolon_to_colon' => true, + 'switch_case_space' => true, + 'ternary_operator_spaces' => true, + 'trailing_comma_in_multiline' => true, + 'trim_array_spaces' => true, + 'unary_operator_spaces' => true, + 'whitespace_after_comma_in_array' => true, +]; + +$project_path = getcwd(); +$finder = Finder::create() + ->in([ + $project_path . '/src', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new Config()) + ->setFinder($finder) + ->setRules($rules) + ->setRiskyAllowed(true) + ->setUsingCache(true); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index e6d2b2c1..00000000 --- a/.styleci.yml +++ /dev/null @@ -1,7 +0,0 @@ -risky: true -preset: laravel -enabled: -- declare_strict_types -disabled: -- concat_without_spaces -- ternary_operator_spaces diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index 2b4f8dfe..6f720e7c 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -69,7 +69,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper if (! $finalPrefix) { $finalPrefix = $originalRoot - ? rtrim($originalRoot, '/') . '/'. $suffix + ? rtrim($originalRoot, '/') . '/' . $suffix : $suffix; } diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 790e1344..2f859ecd 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -4,18 +4,17 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; -use Illuminate\Support\Str; use Illuminate\Config\Repository; -use Illuminate\Queue\QueueManager; -use Stancl\Tenancy\Contracts\Tenant; +use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Queue\Events\JobRetryRequested; +use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; -use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Contracts\TenancyBootstrapper; +use Stancl\Tenancy\Contracts\Tenant; class QueueTenancyBootstrapper implements TenancyBootstrapper { diff --git a/src/CacheManager.php b/src/CacheManager.php index 88428353..09581201 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -13,7 +13,6 @@ class CacheManager extends BaseCacheManager * * @param string $method * @param array $parameters - * @return mixed */ public function __call($method, $parameters) { @@ -21,7 +20,7 @@ class CacheManager extends BaseCacheManager if ($method === 'tags') { $count = count($parameters); - + if ($count !== 1) { throw new \Exception("Method tags() takes exactly 1 argument. $count passed."); } diff --git a/src/Commands/Install.php b/src/Commands/Install.php index dd2dd280..41492b26 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -24,8 +24,6 @@ class Install extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index c67d3598..52ecd47f 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -33,8 +33,6 @@ class Migrate extends MigrateCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index 283d70b0..63860153 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -31,8 +31,6 @@ final class MigrateFresh extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index e60d974b..1c434189 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -42,8 +42,6 @@ class Rollback extends RollbackCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Run.php b/src/Commands/Run.php index aa518d7a..2b20d9c3 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -27,8 +27,6 @@ class Run extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index dc97ae71..8c525208 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -35,8 +35,6 @@ class Seed extends SeedCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/TenantDump.php b/src/Commands/TenantDump.php index 557c6975..9c8698c6 100644 --- a/src/Commands/TenantDump.php +++ b/src/Commands/TenantDump.php @@ -21,10 +21,9 @@ class TenantDump extends DumpCommand $this->specifyParameters(); } - public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int { - $this->tenant()->run(fn() => parent::handle($connections, $dispatcher)); + $this->tenant()->run(fn () => parent::handle($connections, $dispatcher)); return Command::SUCCESS; } diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index d01afcb9..13775676 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -25,8 +25,6 @@ class TenantList extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Concerns/ExtendsLaravelCommand.php b/src/Concerns/ExtendsLaravelCommand.php index bdafc8f7..d08ad6b6 100644 --- a/src/Concerns/ExtendsLaravelCommand.php +++ b/src/Concerns/ExtendsLaravelCommand.php @@ -1,5 +1,7 @@ manager()->makeConnectionConfig( - array_merge($templateConnection, $this->tenantConfig()), $this->getName() + array_merge($templateConnection, $this->tenantConfig()), + $this->getName() ); } diff --git a/src/Features/UserImpersonation.php b/src/Features/UserImpersonation.php index 48d65bb9..f96465ff 100644 --- a/src/Features/UserImpersonation.php +++ b/src/Features/UserImpersonation.php @@ -33,7 +33,6 @@ class UserImpersonation implements Feature * * @param string|ImpersonationToken $token * @param int $ttl - * @return RedirectResponse */ public static function makeResponse($token, int $ttl = null): RedirectResponse { diff --git a/src/Listeners/UpdateSyncedResource.php b/src/Listeners/UpdateSyncedResource.php index 40d4d644..9be290f0 100644 --- a/src/Listeners/UpdateSyncedResource.php +++ b/src/Listeners/UpdateSyncedResource.php @@ -48,8 +48,7 @@ class UpdateSyncedResource extends QueueableListener protected function updateResourceInCentralDatabaseAndGetTenants($event, $syncedAttributes) { /** @var Model|SyncMaster $centralModel */ - $centralModel = $event->model->getCentralModelName() - ::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey()) + $centralModel = $event->model->getCentralModelName()::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey()) ->first(); // We disable events for this call, to avoid triggering this event & listener again. diff --git a/src/Middleware/CheckTenantForMaintenanceMode.php b/src/Middleware/CheckTenantForMaintenanceMode.php index 8e29a31e..c1c734f5 100644 --- a/src/Middleware/CheckTenantForMaintenanceMode.php +++ b/src/Middleware/CheckTenantForMaintenanceMode.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace Stancl\Tenancy\Middleware; use Closure; -use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode; use Stancl\Tenancy\Exceptions\TenancyNotInitializedException; use Symfony\Component\HttpFoundation\IpUtils; +use Symfony\Component\HttpKernel\Exception\HttpException; class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode { diff --git a/src/Middleware/InitializeTenancyByDomain.php b/src/Middleware/InitializeTenancyByDomain.php index 24a1abb7..5a07112d 100644 --- a/src/Middleware/InitializeTenancyByDomain.php +++ b/src/Middleware/InitializeTenancyByDomain.php @@ -29,13 +29,13 @@ class InitializeTenancyByDomain extends IdentificationMiddleware * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { return $this->initializeTenancy( - $request, $next, $request->getHost() + $request, + $next, + $request->getHost() ); } } diff --git a/src/Middleware/InitializeTenancyByDomainOrSubdomain.php b/src/Middleware/InitializeTenancyByDomainOrSubdomain.php index 94217bba..9b153db3 100644 --- a/src/Middleware/InitializeTenancyByDomainOrSubdomain.php +++ b/src/Middleware/InitializeTenancyByDomainOrSubdomain.php @@ -13,8 +13,6 @@ class InitializeTenancyByDomainOrSubdomain * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Middleware/InitializeTenancyByPath.php b/src/Middleware/InitializeTenancyByPath.php index 6289199b..e66400c5 100644 --- a/src/Middleware/InitializeTenancyByPath.php +++ b/src/Middleware/InitializeTenancyByPath.php @@ -38,7 +38,9 @@ class InitializeTenancyByPath extends IdentificationMiddleware // simply injected into some route controller action. if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) { return $this->initializeTenancy( - $request, $next, $route + $request, + $next, + $route ); } else { throw new RouteIsMissingTenantParameterException; diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php index de75d8c5..4e1d33ff 100644 --- a/src/Middleware/InitializeTenancyByRequestData.php +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -36,8 +36,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Middleware/InitializeTenancyBySubdomain.php b/src/Middleware/InitializeTenancyBySubdomain.php index 55d76b05..76389df7 100644 --- a/src/Middleware/InitializeTenancyBySubdomain.php +++ b/src/Middleware/InitializeTenancyBySubdomain.php @@ -28,8 +28,6 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Resolvers/Contracts/CachedTenantResolver.php b/src/Resolvers/Contracts/CachedTenantResolver.php index 968ac794..e84f1fb1 100644 --- a/src/Resolvers/Contracts/CachedTenantResolver.php +++ b/src/Resolvers/Contracts/CachedTenantResolver.php @@ -75,7 +75,6 @@ abstract class CachedTenantResolver implements TenantResolver /** * Get all the arg combinations for resolve() that can be used to find this tenant. * - * @param Tenant $tenant * @return array[] */ abstract public function getArgsForTenant(Tenant $tenant): array; diff --git a/src/Tenancy.php b/src/Tenancy.php index 30f138e3..6873f93b 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -27,7 +27,6 @@ class Tenancy /** * Initializes the tenant. * @param Tenant|int|string $tenant - * @return void */ public function initialize($tenant): void { @@ -106,9 +105,6 @@ class Tenancy /** * Run a callback in the central context. * Atomic, safely reverts to previous context. - * - * @param callable $callback - * @return mixed */ public function central(callable $callback) { @@ -132,7 +128,6 @@ class Tenancy * More performant than running $tenant->run() one by one. * * @param Tenant[]|\Traversable|string[]|null $tenants - * @param callable $callback * @return void */ public function runForMultiple($tenants, callable $callback) diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index dd061af3..e23200a6 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -15,8 +15,6 @@ class TenancyServiceProvider extends ServiceProvider { /** * Register services. - * - * @return void */ public function register(): void { @@ -76,8 +74,6 @@ class TenancyServiceProvider extends ServiceProvider /** * Bootstrap services. - * - * @return void */ public function boot(): void { diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 588fadd8..a0320282 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -5,26 +5,26 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; use Illuminate\Filesystem\FilesystemAdapter; -use ReflectionObject; -use ReflectionProperty; -use Illuminate\Support\Str; -use Illuminate\Support\Facades\DB; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; -use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Jobs\CreateDatabase; -use Stancl\Tenancy\Events\TenantCreated; -use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Illuminate\Support\Str; +use ReflectionObject; +use ReflectionProperty; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; +use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; +use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\Tests\Etc\Tenant; class BootstrapperTest extends TestCase { diff --git a/tests/Etc/ExampleSeeder.php b/tests/Etc/ExampleSeeder.php index a3e36123..2f97787e 100644 --- a/tests/Etc/ExampleSeeder.php +++ b/tests/Etc/ExampleSeeder.php @@ -19,7 +19,7 @@ class ExampleSeeder extends Seeder { DB::table('users')->insert([ 'name' => Str::random(10), - 'email' => Str::random(10).'@gmail.com', + 'email' => Str::random(10) . '@gmail.com', 'password' => bcrypt('password'), ]); } diff --git a/tests/EventListenerTest.php b/tests/EventListenerTest.php index 4a45205c..02ed8b3b 100644 --- a/tests/EventListenerTest.php +++ b/tests/EventListenerTest.php @@ -20,7 +20,6 @@ use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\MigrateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\QueueableListener; -use Stancl\Tenancy\Tenancy; use Stancl\Tenancy\Tests\Etc\Tenant; class EventListenerTest extends TestCase diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php index 4a8d8d0c..90232932 100644 --- a/tests/MaintenanceModeTest.php +++ b/tests/MaintenanceModeTest.php @@ -4,14 +4,12 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Database\Concerns\MaintenanceMode; use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Tests\Etc\Tenant; -use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; +use Symfony\Component\HttpKernel\Exception\HttpException; class MaintenanceModeTest extends TestCase { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index a3df9cd7..fe34ba92 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -4,33 +4,31 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; -use Closure; use Exception; -use Illuminate\Support\Str; use Illuminate\Bus\Queueable; -use Spatie\Valuestore\Valuestore; -use Illuminate\Support\Facades\DB; -use Stancl\Tenancy\Tests\Etc\User; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Tests\Etc\Tenant; -use Illuminate\Support\Facades\Event; -use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\Schema; -use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Jobs\CreateDatabase; -use Illuminate\Queue\InteractsWithQueue; -use Stancl\Tenancy\Events\TenantCreated; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Dispatchable; -use PDO; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Str; +use Spatie\Valuestore\Valuestore; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Tests\Etc\User; class QueueTest extends TestCase { diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 570448d1..0ff95a52 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -589,7 +589,9 @@ class CentralUser extends Model implements SyncMaster use ResourceSyncing, CentralConnection; protected $guarded = []; + public $timestamps = false; + public $table = 'users'; public function tenants(): BelongsToMany @@ -633,7 +635,9 @@ class ResourceUser extends Model implements Syncable use ResourceSyncing; protected $table = 'users'; + protected $guarded = []; + public $timestamps = false; public function getGlobalIdentifierKey() diff --git a/tests/SingleDatabaseTenancyTest.php b/tests/SingleDatabaseTenancyTest.php index b64478cc..d0425dd9 100644 --- a/tests/SingleDatabaseTenancyTest.php +++ b/tests/SingleDatabaseTenancyTest.php @@ -329,6 +329,7 @@ class Post extends Model use BelongsToTenant; protected $guarded = []; + public $timestamps = false; public function comments() @@ -345,6 +346,7 @@ class Post extends Model class Comment extends Model { protected $guarded = []; + public $timestamps = false; public function post() @@ -368,5 +370,6 @@ class ScopedComment extends Comment class GlobalResource extends Model { protected $guarded = []; + public $timestamps = false; } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 0e1464c0..12273c85 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -104,7 +104,7 @@ class TenantDatabaseManagerTest extends TestCase ['sqlite', SQLiteDatabaseManager::class], ['pgsql', PostgreSQLDatabaseManager::class], ['pgsql', PostgreSQLSchemaManager::class], - ['sqlsrv', MicrosoftSQLDatabaseManager::class] + ['sqlsrv', MicrosoftSQLDatabaseManager::class], ]; } @@ -196,7 +196,7 @@ class TenantDatabaseManagerTest extends TestCase ]); tenancy()->initialize($tenant); - $schemaConfig = version_compare(app()->version(), '9.0', '>=') ? + $schemaConfig = version_compare(app()->version(), '9.0', '>=') ? config('database.connections.' . config('database.default') . '.search_path') : config('database.connections.' . config('database.default') . '.schema'); diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 46dc6a00..2d46c233 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -172,6 +172,7 @@ class MyTenant extends Tenant class AnotherTenant extends Model implements Contracts\Tenant { protected $guarded = []; + protected $table = 'tenants'; public function getTenantKeyName(): string diff --git a/tests/TenantUserImpersonationTest.php b/tests/TenantUserImpersonationTest.php index c5e83853..b50db84b 100644 --- a/tests/TenantUserImpersonationTest.php +++ b/tests/TenantUserImpersonationTest.php @@ -274,11 +274,13 @@ class TenantUserImpersonationTest extends TestCase class ImpersonationUser extends Authenticable { protected $guarded = []; + protected $table = 'users'; } class AnotherImpersonationUser extends Authenticable { protected $guarded = []; + protected $table = 'users'; } From c0f97fa04ea2c7fb2ab6e353842d5e30dcd545e2 Mon Sep 17 00:00:00 2001 From: PHP CS Fixer Date: Wed, 20 Jul 2022 13:29:11 +0000 Subject: [PATCH 8/8] Fix code style (php-cs-fixer) --- src/Jobs/DeleteDomains.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Jobs/DeleteDomains.php b/src/Jobs/DeleteDomains.php index fac60e43..4ea92b7f 100644 --- a/src/Jobs/DeleteDomains.php +++ b/src/Jobs/DeleteDomains.php @@ -5,16 +5,10 @@ declare(strict_types=1); namespace Stancl\Tenancy\Jobs; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Stancl\Tenancy\Contracts\TenantWithDatabase; -use Stancl\Tenancy\Database\Models\Domain; -use Stancl\Tenancy\Events\DatabaseDeleted; -use Stancl\Tenancy\Events\DeletingDatabase; -use Stancl\Tenancy\Events\DeletingDomain; -use Stancl\Tenancy\Events\DomainDeleted; class DeleteDomains {