1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 18:34:04 +00:00

Update MailTest

This commit is contained in:
lukinovec 2022-12-06 11:57:16 +01:00
parent 3ed173d40f
commit 8e5d21a85b

View file

@ -26,27 +26,47 @@ function assertMailerTransportUsesPassword(string|null $password) {
expect($mailerPassword)->toBe((string) $password);
};
test('SMTP mailer transport uses the correct tenant credentials', function() {
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'];
$tenant = Tenant::create();
tenancy()->initialize($tenant);
assertMailerTransportUsesPassword(null); // $tenant->smtp_password is null
tenancy()->end($tenant);
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();
tenancy()->end($tenant);
// Assert mailer uses the correct password after switching to a different tenant
tenancy()->initialize(Tenant::create(['smtp_password' => $newTenantPassword = 'updated']));
assertMailerTransportUsesPassword($newTenantPassword);
tenancy()->end();
$tenant->update(['smtp_password' => $newPassword = 'updated']);
tenancy()->initialize($tenant);
assertMailerTransportUsesPassword($newPassword);
// Assert mailer uses the default password after tenancy ends
assertMailerTransportUsesPassword($defaultPassword);
})->group('mailer');
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);
})->group('mailer');