From 68de3600bd6418eae3ea23d460c527e82d45608a Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 14 Dec 2022 19:08:00 +0500 Subject: [PATCH 01/11] Improve commands CLI output (#1030) * use component info/error methods * Update src/Commands/ClearPendingTenants.php Co-authored-by: lukinovec Co-authored-by: lukinovec --- src/Commands/ClearPendingTenants.php | 7 +++---- src/Commands/CreatePendingTenants.php | 6 +++--- src/Commands/Link.php | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Commands/ClearPendingTenants.php b/src/Commands/ClearPendingTenants.php index 82b01cd0..0e27a209 100644 --- a/src/Commands/ClearPendingTenants.php +++ b/src/Commands/ClearPendingTenants.php @@ -17,7 +17,7 @@ class ClearPendingTenants extends Command public function handle(): int { - $this->info('Removing pending tenants.'); + $this->components->info('Removing pending tenants.'); $expirationDate = now(); // We compare the original expiration date to the new one to check if the new one is different later @@ -27,8 +27,7 @@ class ClearPendingTenants extends Command $olderThanHours = (int) $this->option('older-than-hours'); if ($olderThanDays && $olderThanHours) { - $this->line(" Cannot use '--older-than-days' and '--older-than-hours' together \n"); // todo@cli refactor all of these styled command outputs to use $this->components - $this->line('Please, choose only one of these options.'); + $this->components->error("Cannot use '--older-than-days' and '--older-than-hours' together. Please, choose only one of these options."); return 1; // Exit code for failure } @@ -51,7 +50,7 @@ class ClearPendingTenants extends Command ->delete() ->count(); - $this->info($deletedTenantCount . ' pending ' . str('tenant')->plural($deletedTenantCount) . ' deleted.'); + $this->components->info($deletedTenantCount . ' pending ' . str('tenant')->plural($deletedTenantCount) . ' deleted.'); return 0; } diff --git a/src/Commands/CreatePendingTenants.php b/src/Commands/CreatePendingTenants.php index 5c255664..c37b8bd7 100644 --- a/src/Commands/CreatePendingTenants.php +++ b/src/Commands/CreatePendingTenants.php @@ -14,7 +14,7 @@ class CreatePendingTenants extends Command public function handle(): int { - $this->info('Creating pending tenants.'); + $this->components->info('Creating pending tenants.'); $maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count')); $pendingTenantCount = $this->getPendingTenantCount(); @@ -30,8 +30,8 @@ class CreatePendingTenants extends Command $createdCount++; } - $this->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.'); - $this->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.'); + $this->components->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.'); + $this->components->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.'); return 0; } diff --git a/src/Commands/Link.php b/src/Commands/Link.php index a6dd6c5f..d49cc7f2 100644 --- a/src/Commands/Link.php +++ b/src/Commands/Link.php @@ -34,7 +34,7 @@ class Link extends Command $this->createLinks($tenants); } } catch (Exception $exception) { - $this->error($exception->getMessage()); + $this->components->error($exception->getMessage()); return 1; } From 21dc69e35830f66571a5f195d283098f3500a882 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 15 Dec 2022 15:03:03 +0100 Subject: [PATCH 02/11] Use invade in BootstrapperTest (#1033) --- tests/BootstrapperTest.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index ba4ea41a..da51cbde 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -331,15 +331,7 @@ function getDiskPrefix(string $disk): string /** @var FilesystemAdapter $disk */ $disk = Storage::disk($disk); $adapter = $disk->getAdapter(); + $prefix = invade(invade($adapter)->prefixer)->prefix; - $prefixer = (new ReflectionObject($adapter))->getProperty('prefixer'); - $prefixer->setAccessible(true); - - // reflection -> instance - $prefixer = $prefixer->getValue($adapter); - - $prefix = (new ReflectionProperty($prefixer, 'prefix')); - $prefix->setAccessible(true); - - return $prefix->getValue($prefixer); + return $prefix; } From f42f08cb874001b8626d193022cfb994069d013e Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Sat, 17 Dec 2022 06:08:03 +0500 Subject: [PATCH 03/11] Add session state when impersonating tenant (#1029) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * Fix code style (php-cs-fixer) * Update TenantUserImpersonationTest.php * renamed method * update method name in test * rename session key * fix test * Update src/Features/UserImpersonation.php Co-authored-by: Samuel Štancl * Update UserImpersonation.php Co-authored-by: PHP CS Fixer Co-authored-by: Samuel Štancl --- src/Features/UserImpersonation.php | 17 +++++++++++++++++ tests/TenantUserImpersonationTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Features/UserImpersonation.php b/src/Features/UserImpersonation.php index 4c9bb104..608bed07 100644 --- a/src/Features/UserImpersonation.php +++ b/src/Features/UserImpersonation.php @@ -48,6 +48,23 @@ class UserImpersonation implements Feature $token->delete(); + session()->put('tenancy_impersonating', true); + return redirect($token->redirect_url); } + + public static function isImpersonating(): bool + { + return session()->has('tenancy_impersonating'); + } + + /** + * Logout from the current domain and forget impersonation session. + */ + public static function leave(): void // todo possibly rename + { + auth()->logout(); + + session()->forget('tenancy_impersonating'); + } } diff --git a/tests/TenantUserImpersonationTest.php b/tests/TenantUserImpersonationTest.php index 0fcb9022..1e72c604 100644 --- a/tests/TenantUserImpersonationTest.php +++ b/tests/TenantUserImpersonationTest.php @@ -83,6 +83,19 @@ test('tenant user can be impersonated on a tenant domain', function () { pest()->get('http://foo.localhost/dashboard') ->assertSuccessful() ->assertSee('You are logged in as Joe'); + + expect(UserImpersonation::isImpersonating())->toBeTrue(); + expect(session('tenancy_impersonating'))->toBeTrue(); + + // Leave impersonation + UserImpersonation::leave(); + + expect(UserImpersonation::isImpersonating())->toBeFalse(); + expect(session('tenancy_impersonating'))->toBeNull(); + + // Assert can't access the tenant dashboard + pest()->get('http://foo.localhost/dashboard') + ->assertRedirect('http://foo.localhost/login'); }); test('tenant user can be impersonated on a tenant path', function () { @@ -116,6 +129,19 @@ test('tenant user can be impersonated on a tenant path', function () { pest()->get('/acme/dashboard') ->assertSuccessful() ->assertSee('You are logged in as Joe'); + + expect(UserImpersonation::isImpersonating())->toBeTrue(); + expect(session('tenancy_impersonating'))->toBeTrue(); + + // Leave impersonation + UserImpersonation::leave(); + + expect(UserImpersonation::isImpersonating())->toBeFalse(); + expect(session('tenancy_impersonating'))->toBeNull(); + + // Assert can't access the tenant dashboard + pest()->get('/acme/dashboard') + ->assertRedirect('/login'); }); test('tokens have a limited ttl', function () { From 0f892f1585cd79dc48f9e4e50c14c5131c1f5995 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 4 Jan 2023 02:12:25 +0100 Subject: [PATCH 04/11] Make tenants able to have custom mail credentials (#989) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Replace MailManager singleton with an instance of a custom mail manager which always resolves the mailers instead of getting the cached ones * Fix code style (php-cs-fixer) * Add MailTenancyBootstrapper * Add MailTenancyBootstrapper to tenancy.bootstrappers config (commented out) * Fix code style (php-cs-fixer) * Make credentials map a public static property * Always resolve only the mailers specified in the mailersToNotCache public static property * Fix typo in comment * Update TenancyServiceProvider comment * add todo * Add comments to TenancyMailManager, rename property * Remove the configKey array check * Simplify bootstrap method * Change $credentialsMap so that config keys are the keys, and the tenant property names are the values * Rename $mailersToAlwaysResolve to $tenantMailers * Update comment * Update comment * Rename variable in TenancyServiceProvider comment * Scaffold tests * Update comments after review * Uncomment MailTenancyBootstrapper in config * Use array_key_exists instead of null check * Split config logic into methods * Update mapping credentials * Add tests for the added logic * Fix code style (php-cs-fixer) * Delete default 'smtp' mailer in $tenantMailers * Add separate method to pick the appropriate mail credentials map preset * Specify test name * Move mail bootstrapper tests to BootstrapperTest * Depend less on the default mailer by adding a static `$mailer` property * Use static property for map presets * Comment out MailTenancyBootstrapper from config * Add return types to MailTenancyBootstrapper methods * Update test name * Move MailManager extension to MailTenancyBootstrapper * Fix code style (php-cs-fixer) * Update config reverting test * Use `invade()` instead of ReflectionClass * Fix constructor parameter formatting * Delete TenancyMailManager, update tests * Add return type * Update comment * Update MailTest * Delete `group('mailer')` * Delete bindNewMailManagerInstance() * Delete remaining `group('mailer')` * Fix comment * Fix comment Co-authored-by: PHP CS Fixer Co-authored-by: Samuel Štancl --- assets/config.php | 1 + src/Bootstrappers/MailTenancyBootstrapper.php | 79 +++++++++++++++++++ tests/BootstrapperTest.php | 45 +++++++++++ tests/MailTest.php | 72 +++++++++++++++++ tests/TestCase.php | 3 + 5 files changed, 200 insertions(+) create mode 100644 src/Bootstrappers/MailTenancyBootstrapper.php create mode 100644 tests/MailTest.php diff --git a/assets/config.php b/assets/config.php index 3778e107..fab224db 100644 --- a/assets/config.php +++ b/assets/config.php @@ -102,6 +102,7 @@ return [ Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, + // Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed ], diff --git a/src/Bootstrappers/MailTenancyBootstrapper.php b/src/Bootstrappers/MailTenancyBootstrapper.php new file mode 100644 index 00000000..7f15f547 --- /dev/null +++ b/src/Bootstrappers/MailTenancyBootstrapper.php @@ -0,0 +1,79 @@ + 'tenant_property', + * ] + */ + public static array $credentialsMap = []; + + public static string|null $mailer = null; + + protected array $originalConfig = []; + + public static array $mapPresets = [ + 'smtp' => [ + 'mail.mailers.smtp.host' => 'smtp_host', + 'mail.mailers.smtp.port' => 'smtp_port', + 'mail.mailers.smtp.username' => 'smtp_username', + 'mail.mailers.smtp.password' => 'smtp_password', + ], + ]; + + public function __construct( + protected Repository $config, + protected Application $app + ) { + static::$mailer ??= $config->get('mail.default'); + static::$credentialsMap = array_merge(static::$credentialsMap, static::$mapPresets[static::$mailer] ?? []); + } + + public function bootstrap(Tenant $tenant): void + { + // Forget the mail manager instance to clear the cached mailers + $this->app->forgetInstance('mail.manager'); + + $this->setConfig($tenant); + } + + public function revert(): void + { + $this->unsetConfig(); + + $this->app->forgetInstance('mail.manager'); + } + + protected function setConfig(Tenant $tenant): void + { + foreach (static::$credentialsMap as $configKey => $storageKey) { + $override = $tenant->$storageKey; + + if (array_key_exists($storageKey, $tenant->getAttributes())) { + $this->originalConfig[$configKey] ??= $this->config->get($configKey); + + $this->config->set($configKey, $override); + } + } + } + + protected function unsetConfig(): void + { + foreach ($this->originalConfig as $key => $value) { + $this->config->set($key, $value); + } + } +} diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index da51cbde..3cc50b58 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Str; +use Illuminate\Mail\MailManager; use Illuminate\Support\Facades\DB; use Stancl\JobPipeline\JobPipeline; use Illuminate\Support\Facades\File; @@ -23,6 +24,7 @@ use Stancl\Tenancy\Jobs\RemoveStorageSymlinks; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\DeleteTenantStorage; use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; @@ -326,6 +328,49 @@ test('local storage public urls are generated correctly', function() { expect(File::isDirectory($tenantStoragePath))->toBeFalse(); }); +test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() { + MailTenancyBootstrapper::$credentialsMap = [ + 'mail.mailers.smtp.username' => 'smtp_username', + 'mail.mailers.smtp.password' => 'smtp_password' + ]; + + config([ + 'mail.default' => 'smtp', + 'mail.mailers.smtp.username' => $defaultUsername = 'default username', + 'mail.mailers.smtp.password' => 'no password' + ]); + + $tenant = Tenant::create(['smtp_password' => $password = 'testing password']); + + tenancy()->initialize($tenant); + + expect(array_key_exists('smtp_password', tenant()->getAttributes()))->toBeTrue(); + expect(array_key_exists('smtp_host', tenant()->getAttributes()))->toBeFalse(); + expect(config('mail.mailers.smtp.username'))->toBe($defaultUsername); + expect(config('mail.mailers.smtp.password'))->toBe(tenant()->smtp_password); + + // Assert that the current mailer uses tenant's smtp_password + assertMailerTransportUsesPassword($password); +}); + +test('MailTenancyBootstrapper reverts the config and mailer credentials to default when tenancy ends', function() { + MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password']; + config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'no password']); + + tenancy()->initialize(Tenant::create(['smtp_password' => $tenantPassword = 'testing password'])); + + expect(config('mail.mailers.smtp.password'))->toBe($tenantPassword); + + assertMailerTransportUsesPassword($tenantPassword); + + tenancy()->end(); + + expect(config('mail.mailers.smtp.password'))->toBe($defaultPassword); + + // Assert that the current mailer uses the default SMTP password + assertMailerTransportUsesPassword($defaultPassword); +}); + function getDiskPrefix(string $disk): string { /** @var FilesystemAdapter $disk */ diff --git a/tests/MailTest.php b/tests/MailTest.php new file mode 100644 index 00000000..544fda1b --- /dev/null +++ b/tests/MailTest.php @@ -0,0 +1,72 @@ + 'smtp']); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); +}); + +// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password +function assertMailerTransportUsesPassword(string|null $password) { + $manager = app(MailManager::class); + $mailer = invade($manager)->get('smtp'); + $mailerPassword = invade($mailer->getSymfonyTransport())->password; + + expect($mailerPassword)->toBe((string) $password); +}; + +test('mailer transport uses the correct credentials', function() { + config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']); + MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password']; + + tenancy()->initialize($tenant = Tenant::create()); + assertMailerTransportUsesPassword($defaultPassword); // $tenant->smtp_password is not set, so the default password should be used + tenancy()->end(); + + // Assert mailer uses the updated password + $tenant->update(['smtp_password' => $newPassword = 'changed']); + + tenancy()->initialize($tenant); + assertMailerTransportUsesPassword($newPassword); + tenancy()->end(); + + // Assert mailer uses the correct password after switching to a different tenant + tenancy()->initialize(Tenant::create(['smtp_password' => $newTenantPassword = 'updated'])); + assertMailerTransportUsesPassword($newTenantPassword); + tenancy()->end(); + + // Assert mailer uses the default password after tenancy ends + assertMailerTransportUsesPassword($defaultPassword); +}); + + +test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() { + $mailers = fn() => invade(app(MailManager::class))->mailers; + + app(MailManager::class)->mailer('smtp'); + + expect($mailers())->toHaveCount(1); + + tenancy()->initialize(Tenant::create()); + + expect($mailers())->toHaveCount(0); + + app(MailManager::class)->mailer('smtp'); + + expect($mailers())->toHaveCount(1); + + tenancy()->end(); + + expect($mailers())->toHaveCount(0); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 7b9deea0..07af199f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -14,6 +14,7 @@ use Stancl\Tenancy\Facades\GlobalCache; use Stancl\Tenancy\Facades\Tenancy; use Stancl\Tenancy\TenancyServiceProvider; use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; abstract class TestCase extends \Orchestra\Testbench\TestCase { @@ -104,6 +105,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase '--force' => true, ], 'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that + 'tenancy.bootstrappers.mail' => MailTenancyBootstrapper::class, 'queue.connections.central' => [ 'driver' => 'sync', 'central' => true, @@ -113,6 +115,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase ]); $app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration + $app->singleton(MailTenancyBootstrapper::class); } protected function getPackageProviders($app) From 03ac1ef12768c03d119aa6296413709a80062dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 02:23:48 +0100 Subject: [PATCH 05/11] fix phpstan errors (seems like it started ignoring @property annotations on interfaces and abstract classes) --- phpstan.neon | 4 +++- src/Contracts/Domain.php | 2 +- src/Resolvers/DomainTenantResolver.php | 6 +++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index a6bce96d..7ae06b44 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -28,7 +28,7 @@ parameters: paths: - src/Features/TelescopeTags.php - - message: '#Parameter \#1 \$key of method Illuminate\\Contracts\\Cache\\Repository::put\(\) expects string#' + message: '#Parameter \#1 \$key of method Illuminate\\Cache\\Repository::put\(\) expects#' paths: - src/helpers.php - @@ -48,6 +48,8 @@ parameters: paths: - src/Database/DatabaseConfig.php - '#Method Stancl\\Tenancy\\Tenancy::cachedResolvers\(\) should return array#' + - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$tenancy#' + - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$resolver#' checkMissingIterableValueType: false treatPhpDocTypesAsCertain: false diff --git a/src/Contracts/Domain.php b/src/Contracts/Domain.php index a9a19a50..cfe89f43 100644 --- a/src/Contracts/Domain.php +++ b/src/Contracts/Domain.php @@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * * @see \Stancl\Tenancy\Database\Models\Domain * - * @method __call(string $method, array $parameters) IDE support. This will be a model. + * @method __call(string $method, array $parameters) IDE support. This will be a model. // todo check if we can remove these now * @method static __callStatic(string $method, array $parameters) IDE support. This will be a model. * @mixin \Illuminate\Database\Eloquent\Model */ diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 2163febe..ba4e66a0 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Resolvers; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; @@ -39,14 +40,17 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver protected function setCurrentDomain(Tenant $tenant, string $domain): void { + /** @var Tenant&Model $tenant */ static::$currentDomain = $tenant->domains->where('domain', $domain)->first(); } public function getArgsForTenant(Tenant $tenant): array { + /** @var Tenant&Model $tenant */ + $tenant->unsetRelation('domains'); - return $tenant->domains->map(function (Domain $domain) { + return $tenant->domains->map(function (Domain&Model $domain) { return [$domain->domain]; })->toArray(); } From d4c6c34e7c3e320fafd7eb6630e0b71fac4de83b Mon Sep 17 00:00:00 2001 From: PHP CS Fixer Date: Wed, 4 Jan 2023 01:24:21 +0000 Subject: [PATCH 06/11] Fix code style (php-cs-fixer) --- src/Resolvers/DomainTenantResolver.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index ba4e66a0..ceecd0b6 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -47,7 +47,6 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver public function getArgsForTenant(Tenant $tenant): array { /** @var Tenant&Model $tenant */ - $tenant->unsetRelation('domains'); return $tenant->domains->map(function (Domain&Model $domain) { From d0dd87ab07868f7cccc4a6412eb466319156a7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 02:43:10 +0100 Subject: [PATCH 07/11] bump PHP to 8.2, minor ci fixes --- Dockerfile | 2 +- composer.json | 12 ++++++------ docker-compose.yml | 2 +- tests/CommandsTest.php | 2 +- tests/Etc/Console/ExampleCommand.php | 12 ++++++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0ced8009..5dfe442c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # add amd64 platform to support Mac M1 FROM --platform=linux/amd64 shivammathur/node:latest-amd64 -ARG PHP_VERSION=8.1 +ARG PHP_VERSION=8.2 WORKDIR /var/www/html diff --git a/composer.json b/composer.json index 68f16f25..0cfe3984 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-json": "*", "illuminate/support": "^9.0", "spatie/ignition": "^1.4", @@ -58,16 +58,16 @@ } }, "scripts": { - "docker-up": "PHP_VERSION=8.1 docker-compose up -d", - "docker-down": "PHP_VERSION=8.1 docker-compose down", - "docker-rebuild": "PHP_VERSION=8.1 docker-compose up -d --no-deps --build", + "docker-up": "PHP_VERSION=8.2 docker-compose up -d", + "docker-down": "PHP_VERSION=8.2 docker-compose down", + "docker-rebuild": "PHP_VERSION=8.2 docker-compose up -d --no-deps --build", "docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml", "coverage": "open coverage/phpunit/html/index.html", "phpstan": "vendor/bin/phpstan", "phpstan-pro": "vendor/bin/phpstan --pro", "cs": "php-cs-fixer fix --config=.php-cs-fixer.php", - "test": "PHP_VERSION=8.1 ./test --no-coverage", - "test-full": "PHP_VERSION=8.1 ./test" + "test": "PHP_VERSION=8.2 ./test --no-coverage", + "test-full": "PHP_VERSION=8.2 ./test" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/docker-compose.yml b/docker-compose.yml index 116b48f1..465b36cd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: build: context: . args: - PHP_VERSION: ${PHP_VERSION:-8.1} + PHP_VERSION: ${PHP_VERSION:-8.2} depends_on: mysql: condition: service_healthy diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d8484253..7d6f0884 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -372,7 +372,7 @@ function runCommandWorks(): void Artisan::call('tenants:migrate', ['--tenants' => [$id]]); pest()->artisan("tenants:run --tenants=$id 'foo foo --b=bar --c=xyz' ") - ->expectsOutput("User's name is Test command") + ->expectsOutput("User's name is Test user") ->expectsOutput('foo') ->expectsOutput('xyz'); } diff --git a/tests/Etc/Console/ExampleCommand.php b/tests/Etc/Console/ExampleCommand.php index 72263b37..cdd7b551 100644 --- a/tests/Etc/Console/ExampleCommand.php +++ b/tests/Etc/Console/ExampleCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests\Etc\Console; +use Illuminate\Support\Str; use Illuminate\Console\Command; class ExampleCommand extends Command @@ -22,14 +23,13 @@ class ExampleCommand extends Command */ public function handle() { - User::create([ - 'id' => 999, - 'name' => 'Test command', - 'email' => 'test@command.com', + $id = User::create([ + 'name' => 'Test user', + 'email' => Str::random(8) . '@example.com', 'password' => bcrypt('password'), - ]); + ])->id; - $this->line("User's name is " . User::find(999)->name); + $this->line("User's name is " . User::find($id)->name); $this->line($this->argument('a')); $this->line($this->option('c')); } From 9078280a44c3c5d5d4e37ad3c7fc356461603e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 03:04:00 +0100 Subject: [PATCH 08/11] revert to 8.1 in CI for now --- Dockerfile | 3 ++- INTERNAL.md | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 INTERNAL.md diff --git a/Dockerfile b/Dockerfile index 5dfe442c..73a052d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ # add amd64 platform to support Mac M1 FROM --platform=linux/amd64 shivammathur/node:latest-amd64 -ARG PHP_VERSION=8.2 +# todo update this to 8.2 once shivammathur/node supports that +ARG PHP_VERSION=8.1 WORKDIR /var/www/html diff --git a/INTERNAL.md b/INTERNAL.md new file mode 100644 index 00000000..4b3297dd --- /dev/null +++ b/INTERNAL.md @@ -0,0 +1,8 @@ +# Internal development notes + +## Updating the docker image used by the GH action + +1. Login in to Docker Hub: `docker login -u archtechx -p` +2. Build the image (probably shut down docker-compose containers first): `docker-compose build --no-cache` +3. Tag a new image: `docker tag tenancy_test archtechx/tenancy:latest` +4. Push the image: `docker push archtechx/tenancy:latest` From db1dc334a63058eb3ad976bdc1bc90606d958175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 03:04:56 +0100 Subject: [PATCH 09/11] add todo --- src/Tenancy.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tenancy.php b/src/Tenancy.php index e8187dd8..991f9234 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -118,6 +118,7 @@ class Tenancy */ public static function find(int|string $id): Tenant|null { + // todo update all syntax like this once we're fully on PHP 8.2 /** @var (Tenant&Model)|null */ $tenant = static::model()->where(static::model()->getTenantKeyName(), $id)->first(); From 32a128d65750b178e3ed32c70d42ab57dca860c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 03:08:56 +0100 Subject: [PATCH 10/11] lower required php version back to 8.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0cfe3984..098b1cc4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": "^8.2", + "php": "^8.1", "ext-json": "*", "illuminate/support": "^9.0", "spatie/ignition": "^1.4", From 24d71230e8231d6086f5eaf316ae34129a1e02d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 4 Jan 2023 03:18:52 +0100 Subject: [PATCH 11/11] comment out php 8.2 phpstan ignores --- phpstan.neon | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 7ae06b44..6a864833 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -48,8 +48,10 @@ parameters: paths: - src/Database/DatabaseConfig.php - '#Method Stancl\\Tenancy\\Tenancy::cachedResolvers\(\) should return array#' - - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$tenancy#' - - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$resolver#' + + # php 8.2 + # - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$tenancy#' + # - '#Access to an undefined property Stancl\\Tenancy\\Middleware\\IdentificationMiddleware\:\:\$resolver#' checkMissingIterableValueType: false treatPhpDocTypesAsCertain: false