From 0245df0573fd9ac627e4165979f3ce21379ac1a2 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Mon, 13 Apr 2020 04:46:33 -0400 Subject: [PATCH] Mail Extender (#2012) This allows registering new drivers, or overwriting existing ones. --- framework/core/src/Extend/Mail.php | 38 ++++++ .../tests/integration/extenders/MailTest.php | 127 ++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 framework/core/src/Extend/Mail.php create mode 100644 framework/core/tests/integration/extenders/MailTest.php diff --git a/framework/core/src/Extend/Mail.php b/framework/core/src/Extend/Mail.php new file mode 100644 index 000000000..37058546a --- /dev/null +++ b/framework/core/src/Extend/Mail.php @@ -0,0 +1,38 @@ +drivers[$identifier] = $driver; + + return $this; + } + + public function extend(Container $container, Extension $extension = null) + { + $container->extend('mail.supported_drivers', function ($existingDrivers) { + return array_merge($existingDrivers, $this->drivers); + }); + } +} diff --git a/framework/core/tests/integration/extenders/MailTest.php b/framework/core/tests/integration/extenders/MailTest.php new file mode 100644 index 000000000..c18cce91a --- /dev/null +++ b/framework/core/tests/integration/extenders/MailTest.php @@ -0,0 +1,127 @@ +prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser(), + ], + ]); + } + + /** + * @test + */ + public function custom_driver_doesnt_exist_by_default() + { + $this->prepDb(); + + $response = $this->send( + $this->requestAsUser( + $this->request('GET', '/api/mail-settings'), + 1 + ) + ); + + $drivers = json_decode($response->getBody(), true)['data']['attributes']['fields']; + + $this->assertArrayNotHasKey('custom', $drivers); + } + + /** + * @test + */ + public function added_driver_appears_in_mail_settings() + { + $this->extend( + (new Extend\Mail()) + ->driver('custom', CustomDriver::class) + ); + + $this->prepDb(); + + $response = $this->send( + $this->requestAsUser( + $this->request('GET', '/api/mail-settings'), + 1 + ) + ); + + $drivers = json_decode($response->getBody(), true)['data']['attributes']['fields']; + + $this->assertArrayHasKey('custom', $drivers); + } + + /** + * @test + */ + public function adding_driver_with_duplicate_name_overrides_fields() + { + $this->extend( + (new Extend\Mail()) + ->driver('smtp', CustomDriver::class) + ); + + $this->prepDb(); + + $response = $this->send( + $this->requestAsUser( + $this->request('GET', '/api/mail-settings'), + 1 + ) + ); + + $requiredFields = json_decode($response->getBody(), true)['data']['attributes']['fields']['smtp']; + + $this->assertEquals($requiredFields, ['customSetting1' => '']); + } +} + +class CustomDriver implements DriverInterface +{ + public function availableSettings(): array + { + return ['customSetting1' => '']; + } + + public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag + { + return new MessageBag; + } + + public function canSend(): bool + { + return false; + } + + public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport + { + return new Swift_NullTransport(); + } +}