diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 984973c..29900ba 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,12 +7,13 @@ on: jobs: tests: runs-on: ${{ matrix.os }} + strategy: fail-fast: true matrix: os: [ubuntu-latest] - php: ['8.0', 8.1, 8.2, 8.3] - laravel: [9.*, 10.*, 11.*] + php: [8.2, 8.3, 8.4] + laravel: ['10.*', '11.*', '12.*'] dependency-version: [prefer-lowest, prefer-stable] exclude: - laravel: 9.* @@ -27,6 +28,10 @@ jobs: php: '8.0' - laravel: 11.* php: 8.1 + - laravel: 12.* + php: '8.0' + - laravel: 12.* + php: 8.1 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} @@ -46,5 +51,6 @@ jobs: run: | composer require "illuminate/console:${{ matrix.laravel }}" "illuminate/mail:${{ matrix.laravel }}" "illuminate/view:${{ matrix.laravel }}" --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress + - name: Execute tests run: vendor/bin/pest diff --git a/composer.json b/composer.json index 15c2d68..5d34c15 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,14 @@ ], "homepage": "https://github.com/beyondcode/helo-laravel", "require": { - "php": "^8.0 || ^8.1 || ^8.2", - "illuminate/console": "^8.0 || ^9.0 || ^10.0 || ^11.0", - "illuminate/mail": "^8.0 || ^9.0 || ^10.0 || ^11.0", - "illuminate/view": "^8.0 || ^9.0 || ^10.0 || ^11.0" + "php": "^8.2", + "illuminate/console": "^10.0 || ^11.0 || ^12.0", + "illuminate/mail": "^10.0 || ^11.0 || ^12.0", + "illuminate/view": "^10.0 || ^11.0 || ^12.0" }, "require-dev": { - "orchestra/testbench": "^7.0 || ^8.0 || ^9.0", - "pestphp/pest": "1.x-dev || 2.x-dev" + "orchestra/testbench": "^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0", + "pestphp/pest": "1.x-dev || 2.x-dev || ^3.7" }, "minimum-stability": "dev", "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 667a775..268eabc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,37 +1,23 @@ - - - - tests - - - - - - src - - - - - - - - - - - - - - + + + + tests + + + + + src + + + + + + + + + + + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..667a775 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,37 @@ + + + + + tests + + + + + + src + + + + + + + + + + + + + + + diff --git a/src/CreatesMailers.php b/src/CreatesMailers.php index 3fb0aa1..1b40afb 100644 --- a/src/CreatesMailers.php +++ b/src/CreatesMailers.php @@ -4,116 +4,21 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; -use Swift_Mailer; trait CreatesMailers { - protected function createLaravel6Mailer($app) - { - $config = $this->getConfig(); - - // Once we have create the mailer instance, we will set a container instance - // on the mailer. This allows us to resolve mailer classes via containers - // for maximum testability on said classes instead of passing Closures. - $mailer = new Mailer( - $app['view'], - $app['swift.mailer'], - $app['events'] - ); - - if ($app->bound('queue')) { - $mailer->setQueue($app['queue']); - } - - // Next we will set all of the global addresses on this mailer, which allows - // for easy unification of all "from" addresses as well as easy debugging - // of sent messages since they get be sent into a single email address. - foreach (['from', 'reply_to', 'to'] as $type) { - $this->setGlobalAddress($mailer, $config, $type); - } - - return $mailer; - } - - protected function createLaravel7Mailer($app) - { - $defaultDriver = $app['mail.manager']->getDefaultDriver(); - $config = $this->getConfig($defaultDriver); - - // Laravel 7 no longer bindes the swift.mailer: - $swiftMailer = new Swift_Mailer($app['mail.manager']->createTransport($config)); - - // Once we have create the mailer instance, we will set a container instance - // on the mailer. This allows us to resolve mailer classes via containers - // for maximum testability on said classes instead of passing Closures. - $mailer = new Laravel7Mailer( - 'smtp', - $app['view'], - $swiftMailer, - $app['events'] - ); - - if ($app->bound('queue')) { - $mailer->setQueue($app['queue']); - } - - // Next we will set all of the global addresses on this mailer, which allows - // for easy unification of all "from" addresses as well as easy debugging - // of sent messages since they get be sent into a single email address. - foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { - $this->setGlobalAddress($mailer, $config, $type); - } - - return $mailer; - } - - protected function createLaravel8Mailer($app) + protected function createGeneralLaravelMailer($app) { $defaultDriver = $app['mail.manager']->getDefaultDriver(); $config = $this->getConfig($defaultDriver); - $swiftMailer = new Swift_Mailer($app['mail.manager']->createTransport($config)); + // We get Symfony Transport from Laravel 10+ mailer + $symfonyTransport = $app['mailer']->getSymfonyTransport(); // Once we have create the mailer instance, we will set a container instance // on the mailer. This allows us to resolve mailer classes via containers // for maximum testability on said classes instead of passing Closures. - $mailer = new Laravel8Mailer( - 'smtp', - $app['view'], - $swiftMailer, - $app['events'] - ); - - if ($app->bound('queue')) { - $mailer->setQueue($app['queue']); - } - - // Next we will set all of the global addresses on this mailer, which allows - // for easy unification of all "from" addresses as well as easy debugging - // of sent messages since they get be sent into a single email address. - foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { - $this->setGlobalAddress($mailer, $config, $type); - } - - return $mailer; - } - - protected function createLaravel9Mailer($app) - { - $defaultDriver = $app['mail.manager']->getDefaultDriver(); - $config = $this->getConfig($defaultDriver); - - // We get Symfony Transport from Laravel 9 mailer - if (version_compare(app()->version(), '10.0.0', '<')) { - $symfonyTransport = $app['mail.manager']->getSymfonyTransport(); - } else { - $symfonyTransport = $app['mailer']->getSymfonyTransport(); - } - - // Once we have create the mailer instance, we will set a container instance - // on the mailer. This allows us to resolve mailer classes via containers - // for maximum testability on said classes instead of passing Closures. - $mailer = new Laravel9Mailer( + $mailer = new GeneralLaravelMailer( 'smtp', $app['view'], $symfonyTransport, @@ -152,11 +57,7 @@ protected function getConfig($name = 'smtp') */ protected function setGlobalAddress($mailer, array $config, $type) { - if (version_compare(app()->version(), '7.0.0', '<')) { - $address = Arr::get($config, $type); - } else { - $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]); - } + $address = Arr::get($config, $type, $this->app['config']['mail.'.$type]); if (is_array($address) && isset($address['address'])) { $mailer->{'always'.Str::studly($type)}($address['address'], $address['name']); diff --git a/src/Laravel8Mailer.php b/src/GeneralLaravelMailer.php similarity index 66% rename from src/Laravel8Mailer.php rename to src/GeneralLaravelMailer.php index c1bdc29..f7076b0 100644 --- a/src/Laravel8Mailer.php +++ b/src/GeneralLaravelMailer.php @@ -5,8 +5,19 @@ use Illuminate\Contracts\Mail\Factory as MailFactory; use Illuminate\Contracts\Mail\Mailer as MailerContract; -class Laravel8Mailer extends Laravel7Mailer implements MailerContract, MailFactory +class GeneralLaravelMailer extends Mailer implements MailerContract, MailFactory { + public $currentMailer = null; + protected $app; + protected $mailers = []; + + public function mailer($name = null) + { + $this->currentMailer = $name; + + return $this; + } + /** * Set laravel application. * diff --git a/src/HeloLaravelServiceProvider.php b/src/HeloLaravelServiceProvider.php index 52565a4..c6c7a74 100644 --- a/src/HeloLaravelServiceProvider.php +++ b/src/HeloLaravelServiceProvider.php @@ -45,30 +45,16 @@ public function register() $this->mergeConfigFrom(__DIR__.'/../config/helo.php', 'helo'); $this->app->singleton(Mailer::class, function ($app) { - $version = $this->version($app); - - if ($version < 7) { - return $this->createLaravel6Mailer($app); - } - - if ($version < 8) { - return $this->createLaravel7Mailer($app); - } - - if ($version < 9) { - return $this->createLaravel8Mailer($app); - } - - return $this->createLaravel9Mailer($app); + return $this->createGeneralLaravelMailer($app); }); } protected function bootMailable() { + $managerInstance = app()->make(MailManager::class, ['app' => app()]); $instance = app()->make(Mailer::class); - Mail::swap($instance); - + Mail::swap($managerInstance); $this->app->instance(MailerContract::class, $instance); } diff --git a/src/Laravel7Mailer.php b/src/Laravel7Mailer.php deleted file mode 100644 index 2e80397..0000000 --- a/src/Laravel7Mailer.php +++ /dev/null @@ -1,18 +0,0 @@ -currentMailer = $name; - - return $this; - } -} diff --git a/src/Laravel9Mailer.php b/src/Laravel9Mailer.php deleted file mode 100644 index a36093c..0000000 --- a/src/Laravel9Mailer.php +++ /dev/null @@ -1,10 +0,0 @@ -createLaravel9Mailer($this->app); + return $this->createGeneralLaravelMailer($this->app); } return $this->mailers[$name] = $this->get($name); diff --git a/tests/HeloTest.php b/tests/HeloTest.php index 2099cac..db9d2fd 100644 --- a/tests/HeloTest.php +++ b/tests/HeloTest.php @@ -2,7 +2,6 @@ namespace BeyondCode\HeloLaravel\Tests; -use BeyondCode\HeloLaravel\Laravel7Mailer; use BeyondCode\HeloLaravel\Mailer; use BeyondCode\HeloLaravel\TestMail; use BeyondCode\HeloLaravel\TestMailCommand; @@ -29,9 +28,5 @@ test('the correct mailer is binded', function () { $mailer = app(Mailer::class); - if (version_compare(app()->version(), '7.0.0', '<')) { - $this->assertTrue($mailer instanceof Mailer); - } else { - $this->assertTrue($mailer instanceof Laravel7Mailer); - } + $this->assertTrue($mailer instanceof Mailer); });