From 0507ee59ab0887d5af7c427081ecfdc9a815bd29 Mon Sep 17 00:00:00 2001 From: Keller Martin <43446570+kellerjmrtn@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:01:44 -0400 Subject: [PATCH] Reintroduce NullDriver From v4 (#323) * Reintroduce NullDriver From v4 * Update composer.json --------- Co-authored-by: Keller Martin Co-authored-by: Freek Van der Herten --- composer.json | 3 ++- src/Drivers/NullDriver.php | 25 +++++++++++++++++++++++++ src/NewsletterServiceProvider.php | 9 +++++++++ tests/Drivers/NullDriverTest.php | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Drivers/NullDriver.php create mode 100644 tests/Drivers/NullDriverTest.php diff --git a/composer.json b/composer.json index 0472033..9a76834 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ ], "require": { "php": "^8.1", - "spatie/laravel-package-tools": "^1.13" + "spatie/laravel-package-tools": "^1.13", + "illuminate/log": "^9|^10|^11" }, "suggest": { "spatie/mailcoach-sdk-php": "For working with Mailcoach", diff --git a/src/Drivers/NullDriver.php b/src/Drivers/NullDriver.php new file mode 100644 index 0000000..6fb5ac6 --- /dev/null +++ b/src/Drivers/NullDriver.php @@ -0,0 +1,25 @@ +logCalls = $logCalls; + } + + public function __call($name, $arguments) + { + if ($this->logCalls) { + Log::debug('Called Spatie Newsletter facade method: '.$name.' with:', $arguments); + } + } +} diff --git a/src/NewsletterServiceProvider.php b/src/NewsletterServiceProvider.php index 6b1c1e2..6e95cf8 100644 --- a/src/NewsletterServiceProvider.php +++ b/src/NewsletterServiceProvider.php @@ -5,6 +5,7 @@ use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; use Spatie\Newsletter\Drivers\Driver; +use Spatie\Newsletter\Drivers\NullDriver; use Spatie\Newsletter\Support\Lists; class NewsletterServiceProvider extends PackageServiceProvider @@ -22,6 +23,14 @@ public function bootingPackage() /** @var class-string $driverClass */ $driverClass = config('newsletter.driver'); + if ( + is_null($driverClass) + || $driverClass === 'log' + || $driverClass === NullDriver::class + ) { + return new NullDriver($driverClass === 'log'); + } + $arguments = config('newsletter.driver_arguments'); $lists = Lists::createFromConfig(config('newsletter')); diff --git a/tests/Drivers/NullDriverTest.php b/tests/Drivers/NullDriverTest.php new file mode 100644 index 0000000..c55c214 --- /dev/null +++ b/tests/Drivers/NullDriverTest.php @@ -0,0 +1,27 @@ +set('newsletter.driver', null); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('uses the NullDriver with log config value', function () { + config()->set('newsletter.driver', 'log'); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('uses the NullDriver with NullDriver::class config', function () { + config()->set('newsletter.driver', NullDriver::class); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('accepts calls to the NullDriver', function () { + config()->set('newsletter.driver', null); + + expect(app('newsletter')->subscribe('example@example.org'))->toBeNull(); +});