mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 15:34:04 +00:00
Delete TenancyMailManager, update tests
This commit is contained in:
parent
cf5bffea25
commit
fcd71f0b63
4 changed files with 51 additions and 78 deletions
|
|
@ -9,7 +9,6 @@ use Illuminate\Foundation\Application;
|
|||
use Illuminate\Mail\MailManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\TenancyMailManager;
|
||||
|
||||
class MailTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
|
|
@ -48,9 +47,7 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
|
|||
{
|
||||
// Use custom mail manager that resolves the mailers specified in its $tenantMailers static property
|
||||
// Instead of getting the cached mailers from the $mailers property
|
||||
$this->app->extend(MailManager::class, function (MailManager $mailManager) {
|
||||
return new TenancyMailManager($this->app);
|
||||
});
|
||||
$this->bindNewMailManagerInstance();
|
||||
|
||||
$this->setConfig($tenant);
|
||||
}
|
||||
|
|
@ -58,6 +55,15 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
|
|||
public function revert(): void
|
||||
{
|
||||
$this->unsetConfig();
|
||||
|
||||
$this->bindNewMailManagerInstance();
|
||||
}
|
||||
|
||||
protected function bindNewMailManagerInstance()
|
||||
{
|
||||
$this->app->extend(MailManager::class, function (MailManager $mailManager) {
|
||||
return new MailManager($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
protected function setConfig(Tenant $tenant): void
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy; // todo new Overrides namespace?
|
||||
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
|
||||
|
||||
/**
|
||||
* This class (paired with the MailTenancyBootstrapper)
|
||||
* allows tenants to have their custom mailer credentials.
|
||||
*
|
||||
* Tenancy swaps Laravel's MailManager singleton for an instance of this class,
|
||||
* which overrides the manager's get method to always resolve
|
||||
* the mailers specified in the static $tenantMailers property
|
||||
* instead of getting them from the $mailers property where they're cached.
|
||||
*
|
||||
* @see MailTenancyBootstrapper
|
||||
*/
|
||||
class TenancyMailManager extends MailManager
|
||||
{
|
||||
/**
|
||||
* Mailers to always resolve from the container (even when they're
|
||||
* cached and available in the $mailers property).
|
||||
*/
|
||||
public static array $tenantMailers = [];
|
||||
|
||||
/**
|
||||
* Override the get method so that the mailers in $tenantMailers always get resolved,
|
||||
* even when they're cached and available in the $mailers property.
|
||||
*/
|
||||
protected function get($name)
|
||||
{
|
||||
if (in_array($name, static::$tenantMailers)) {
|
||||
return $this->resolve($name);
|
||||
}
|
||||
|
||||
return parent::get($name);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -327,37 +328,48 @@ 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', function() {
|
||||
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::create(['smtp_password' => 'testing password'])->run(function() use ($defaultUsername) {
|
||||
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);
|
||||
});
|
||||
});
|
||||
$tenant = Tenant::create(['smtp_password' => $password = 'testing password']);
|
||||
|
||||
test('MailTenancyBootstrapper reverts the config to default when tenancy ends', function() {
|
||||
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);
|
||||
})->group('mailer');
|
||||
|
||||
test('MailTenancyBootstrapper reverts the config and mailer credentials to default when tenancy ends', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
config(['mail.mailers.smtp.password' => $defaultPassword = 'no 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);
|
||||
})->group('mailer');
|
||||
|
||||
function getDiskPrefix(string $disk): string
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
use Illuminate\Mail\MailManager;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\TenancyMailManager;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
|
|
@ -18,39 +17,36 @@ beforeEach(function() {
|
|||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
});
|
||||
|
||||
test('tenancy swaps the MailManager singleton for an instance of TenancyMailManager', function() {
|
||||
tenancy()->initialize(Tenant::create());
|
||||
// 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(app(MailManager::class))->toBeInstanceOf(TenancyMailManager::class);
|
||||
});
|
||||
expect($mailerPassword)->toBe((string) $password);
|
||||
};
|
||||
|
||||
test('SMTP mailer transport uses the correct tenant credentials', function() {
|
||||
TenancyMailManager::$tenantMailers = ['smtp'];
|
||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password
|
||||
$assertTransportUsesPassword = function(string|null $password) use ($tenant) {
|
||||
tenancy()->initialize($tenant);
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$manager = app(MailManager::class);
|
||||
assertMailerTransportUsesPassword(null); // $tenant->smtp_password is null
|
||||
|
||||
$mailer = invade($manager)->get('smtp');
|
||||
$mailerPassword = invade($mailer->getSymfonyTransport())->password;
|
||||
|
||||
expect($mailerPassword)->toBe((string) $password);
|
||||
|
||||
tenancy()->end();
|
||||
};
|
||||
|
||||
$assertTransportUsesPassword(null); // $tenant->smtp_password is null
|
||||
tenancy()->end($tenant);
|
||||
|
||||
$tenant->update(['smtp_password' => $newPassword = 'changed']);
|
||||
|
||||
$assertTransportUsesPassword($newPassword);
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
assertMailerTransportUsesPassword($newPassword);
|
||||
|
||||
tenancy()->end($tenant);
|
||||
|
||||
$tenant->update(['smtp_password' => $newPassword = 'updated']);
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$assertTransportUsesPassword($newPassword);
|
||||
});
|
||||
assertMailerTransportUsesPassword($newPassword);
|
||||
})->group('mailer');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue