From 0432942893c2015b36410e78d49be849d0f06d11 Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Fri, 20 Sep 2024 09:24:34 +0200 Subject: [PATCH 1/2] feat: Add laravel 11 support and replace phpcs with pint --- .github/workflows/styling.yml | 15 ++++--- .github/workflows/tests.yml | 17 ++++--- .github/workflows/update-changelog.yml | 4 +- .php-cs-fixer.dist.php | 39 ---------------- README.md | 7 +-- composer.json | 24 ++++++---- config/mailjet.php | 52 +++++++++++++++++++++- pint.json | 10 +++++ src/{ => Channels}/MailjetEmailChannel.php | 18 ++------ src/{ => Channels}/MailjetSmsChannel.php | 18 ++------ src/{ => Exceptions}/MailjetException.php | 2 +- src/MailjetNotifierServiceProvider.php | 4 +- src/MailjetService.php | 43 ++++++------------ src/{ => Messages}/MailjetEmailMessage.php | 4 +- src/{ => Messages}/MailjetSmsMessage.php | 2 +- tests/MailjetEmailChannelTest.php | 17 ++++--- tests/MailjetServiceTest.php | 31 ++++++------- tests/MailjetSmsChannelTest.php | 18 +++++--- tests/{ => Models}/User.php | 7 ++- tests/Pest.php | 2 + tests/TestCase.php | 10 ++--- 21 files changed, 176 insertions(+), 168 deletions(-) delete mode 100644 .php-cs-fixer.dist.php create mode 100644 pint.json rename src/{ => Channels}/MailjetEmailChannel.php (58%) rename src/{ => Channels}/MailjetSmsChannel.php (58%) rename src/{ => Exceptions}/MailjetException.php (96%) rename src/{ => Messages}/MailjetEmailMessage.php (97%) rename src/{ => Messages}/MailjetSmsMessage.php (92%) rename tests/{ => Models}/User.php (50%) diff --git a/.github/workflows/styling.yml b/.github/workflows/styling.yml index 000e77c..1ee0ed4 100644 --- a/.github/workflows/styling.yml +++ b/.github/workflows/styling.yml @@ -1,6 +1,6 @@ name: Check & fix styling -on: [ push ] +on: [push] jobs: styling: @@ -8,14 +8,17 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} - - - name: Run PHP CS Fixer - uses: docker://oskarstark/php-cs-fixer-ga + - name: Set up PHP + uses: shivammathur/setup-php@v2 with: - args: --allow-risky=yes + php-version: 8.1 + - name: Install Composer + run: composer install --no-interaction + - name: Run Laravel Pint + run: composer format - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 58beed2..f724186 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,12 +11,19 @@ jobs: strategy: fail-fast: false matrix: - php: [ "8.1", "8.0", "8.2" ] - laravel: [ "^9.0", '^10.0' ] - dependency-version: [ prefer-lowest, prefer-stable ] + php: [8.1, 8.2] + laravel: [9.*, 10.*, 11.*] + dependency-version: [prefer-stable] + include: + - laravel: 11.* + testbench: 9.* + - laravel: 10.* + testbench: 8.* + - laravel: 9.* + testbench: 7.* exclude: - - php: "8.0" - laravel: "^10.0" + - laravel: 11.* + php: 8.1 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml index 0293715..94df2c3 100644 --- a/.github/workflows/update-changelog.yml +++ b/.github/workflows/update-changelog.yml @@ -2,7 +2,7 @@ name: "Update Changelog" on: release: - types: [ released ] + types: [released] jobs: update: @@ -10,7 +10,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: ref: main diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index b5b1f34..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,39 +0,0 @@ -notPath('vendor') - ->in([ - __DIR__ . '/src', - ]) - ->name('*.php') - ->notName('*.blade.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true); - -$config = new PhpCsFixer\Config(); - -return $config->setRules([ - '@PSR12' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'unary_operator_spaces' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_var_without_name' => true, - 'class_attributes_separation' => [ - 'elements' => [ - 'method' => 'one', - ], - ], - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], -])->setFinder($finder); diff --git a/README.md b/README.md index b6f09d1..433e40d 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,7 @@ php artisan vendor:publish --provider="YieldStudio\LaravelMailjetNotifier\Mailje namespace App\Notifications; -use Illuminate\Notifications\Notification; -use YieldStudio\LaravelMailjetNotifier\MailjetEmailChannel; +use Illuminate\Notifications\Notification;use YieldStudio\LaravelMailjetNotifier\Channels\MailjetEmailChannel; class OrderConfirmation extends Notification { @@ -77,9 +76,7 @@ class OrderConfirmation extends Notification namespace App\Notifications; -use Illuminate\Notifications\Notification -;use YieldStudio\LaravelMailjetNotifier\MailjetSmsChannel; -use YieldStudio\LaravelMailjetNotifier\MailjetSmsMessage; +use Illuminate\Notifications\Notification;use YieldStudio\LaravelMailjetNotifier\Channels\MailjetSmsChannel;use YieldStudio\LaravelMailjetNotifier\Messages\MailjetSmsMessage; class ResetPassword extends Notification { diff --git a/composer.json b/composer.json index 53063ff..719ab83 100644 --- a/composer.json +++ b/composer.json @@ -19,20 +19,26 @@ "email": "james@yieldstudio.fr", "homepage": "https://yieldstudio.fr", "role": "Developer" + }, + { + "name": "Arthur Monney", + "email": "arthur.monney@yieldstudio.fr", + "homepage": "https://yieldstudio.fr", + "role": "Developer" } ], "require": { - "php": "^8.0", - "illuminate/support": "^9|^10", - "illuminate/database": "^9|^10", - "mailjet/mailjet-apiv3-php": "^1.5" + "php": "^8.1|^8.2", + "illuminate/database": "^9|^10|^11", + "illuminate/support": "^9|^10|^11", + "mailjet/mailjet-apiv3-php": "^1.6" }, "require-dev": { "ciareis/bypass": "^1.0", - "friendsofphp/php-cs-fixer": "^3.8", - "orchestra/testbench": "7.*|8.*", - "pestphp/pest": "^1.21", - "phpunit/phpunit": "^9.1" + "laravel/pint": "^1.16", + "orchestra/testbench": "^7.44|^8.25|^9.0", + "pestphp/pest": "^2.34", + "phpunit/phpunit": "^9.5|^10.5" }, "autoload": { "psr-4": { @@ -45,7 +51,7 @@ } }, "scripts": { - "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", + "format": "vendor/bin/pint", "test": "vendor/bin/pest", "post-autoload-dump": [ "@php ./vendor/bin/testbench package:discover --ansi" diff --git a/config/mailjet.php b/config/mailjet.php index fb24fce..6a24144 100644 --- a/config/mailjet.php +++ b/config/mailjet.php @@ -1,16 +1,66 @@ env('MAILJET_APIKEY', ''), 'secret' => env('MAILJET_APISECRET', ''), - 'smsToken' => env('MAILJET_SMSTOKEN'), + + /* + |-------------------------------------------------------------------------- + | Email Sender + |-------------------------------------------------------------------------- + | + | Email and Name used by Mailjet when sending email. + | This configuration is used when sending mail. + | + */ + 'emailFrom' => [ 'Email' => env('MAIL_FROM_ADDRESS'), 'Name' => env('MAIL_FROM_NAME'), ], + + /* + |-------------------------------------------------------------------------- + | SMS Sender + |-------------------------------------------------------------------------- + | + | Defines the name that will be used as the "from" for all outgoing text messages + | + */ + 'smsFrom' => env('MAILJET_SMS_SENDER'), + 'smsToken' => env('MAILJET_SMSTOKEN'), + + /* + |-------------------------------------------------------------------------- + | Dry + |-------------------------------------------------------------------------- + | + */ + 'dry' => (bool) env('MAILJET_DRY', false), + + /* + |-------------------------------------------------------------------------- + | Options + |-------------------------------------------------------------------------- + | + */ + 'options' => [ 'version' => 'v3.1', ], + ]; diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..d8d26bd --- /dev/null +++ b/pint.json @@ -0,0 +1,10 @@ +{ + "preset": "laravel", + "rules": { + "concat_space": { + "spacing": "one" + }, + "blank_line_before_statement": true, + "declare_strict_types": true + } +} \ No newline at end of file diff --git a/src/MailjetEmailChannel.php b/src/Channels/MailjetEmailChannel.php similarity index 58% rename from src/MailjetEmailChannel.php rename to src/Channels/MailjetEmailChannel.php index cf45a40..809383f 100644 --- a/src/MailjetEmailChannel.php +++ b/src/Channels/MailjetEmailChannel.php @@ -2,27 +2,17 @@ declare(strict_types=1); -namespace YieldStudio\LaravelMailjetNotifier; +namespace YieldStudio\LaravelMailjetNotifier\Channels; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Notification; +use YieldStudio\LaravelMailjetNotifier\Exceptions\MailjetException; +use YieldStudio\LaravelMailjetNotifier\MailjetService; class MailjetEmailChannel { - protected MailjetService $mailjetService; - - /** - * Create a new mail channel instance. - * - * @param MailjetService $mailjetService - * - * @return void - */ - public function __construct(MailjetService $mailjetService) - { - $this->mailjetService = $mailjetService; - } + public function __construct(protected MailjetService $mailjetService) {} /** * @throws MailjetException diff --git a/src/MailjetSmsChannel.php b/src/Channels/MailjetSmsChannel.php similarity index 58% rename from src/MailjetSmsChannel.php rename to src/Channels/MailjetSmsChannel.php index 5dd473b..a7087a7 100644 --- a/src/MailjetSmsChannel.php +++ b/src/Channels/MailjetSmsChannel.php @@ -2,27 +2,17 @@ declare(strict_types=1); -namespace YieldStudio\LaravelMailjetNotifier; +namespace YieldStudio\LaravelMailjetNotifier\Channels; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Notification; +use YieldStudio\LaravelMailjetNotifier\Exceptions\MailjetException; +use YieldStudio\LaravelMailjetNotifier\MailjetService; final class MailjetSmsChannel { - protected MailjetService $mailjetService; - - /** - * Create a new mail channel instance. - * - * @param MailjetService $mailjetService - * - * @return void - */ - public function __construct(MailjetService $mailjetService) - { - $this->mailjetService = $mailjetService; - } + public function __construct(protected MailjetService $mailjetService) {} /** * @throws MailjetException diff --git a/src/MailjetException.php b/src/Exceptions/MailjetException.php similarity index 96% rename from src/MailjetException.php rename to src/Exceptions/MailjetException.php index bf3012a..c9c40a7 100644 --- a/src/MailjetException.php +++ b/src/Exceptions/MailjetException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace YieldStudio\LaravelMailjetNotifier; +namespace YieldStudio\LaravelMailjetNotifier\Exceptions; use Exception; use Mailjet\Response; diff --git a/src/MailjetNotifierServiceProvider.php b/src/MailjetNotifierServiceProvider.php index 026e385..3baa7e7 100644 --- a/src/MailjetNotifierServiceProvider.php +++ b/src/MailjetNotifierServiceProvider.php @@ -12,7 +12,7 @@ public function boot(): void { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__.'/../config' => config_path(), + __DIR__ . '/../config' => config_path(), ], 'config'); } } @@ -21,7 +21,7 @@ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/mailjet.php', 'mailjet'); - $this->app->bind(MailjetService::class, function () { + $this->app->bind(MailjetService::class, function (): MailjetService { $key = config('mailjet.key', ''); $secret = config('mailjet.secret', ''); $smsToken = config('mailjet.smsToken'); diff --git a/src/MailjetService.php b/src/MailjetService.php index 8802430..bb49c8a 100644 --- a/src/MailjetService.php +++ b/src/MailjetService.php @@ -7,6 +7,9 @@ use Mailjet\Client; use Mailjet\Resources; use Mailjet\Response; +use YieldStudio\LaravelMailjetNotifier\Exceptions\MailjetException; +use YieldStudio\LaravelMailjetNotifier\Messages\MailjetEmailMessage; +use YieldStudio\LaravelMailjetNotifier\Messages\MailjetSmsMessage; class MailjetService { @@ -42,11 +45,7 @@ public function __construct(string $key, string $secret, bool $dry = false, arra /** * Send mailjet email. * - * @param MailjetEmailMessage $message - * @param array $args Request arguments - * @param array $options - * - * @return Response|null + * @param array $args Request arguments * * @throws MailjetException */ @@ -68,11 +67,7 @@ public function sendEmail(MailjetEmailMessage $message, array $args = [], array /** * Send mailjet SMS. * - * @param MailjetSmsMessage $message - * @param array $args Request arguments - * @param array $options - * - * @return Response + * @param array $args Request arguments * * @throws MailjetException */ @@ -99,11 +94,8 @@ public function sendSms(MailjetSmsMessage $message, array $args = [], array $opt /** * Trigger a POST request. * - * @param array $resource Mailjet Resource/Action pair - * @param array $args Request arguments - * @param array $options - * - * @return Response + * @param array $resource Mailjet Resource/Action pair + * @param array $args Request arguments * * @throws MailjetException */ @@ -121,11 +113,8 @@ public function post(array $resource, array $args = [], array $options = []): Re /** * Trigger a GET request. * - * @param array $resource Mailjet Resource/Action pair - * @param array $args Request arguments - * @param array $options - * - * @return Response + * @param array $resource Mailjet Resource/Action pair + * @param array $args Request arguments * * @throws MailjetException */ @@ -143,11 +132,8 @@ public function get(array $resource, array $args = [], array $options = []): Res /** * Trigger a PUT request. * - * @param array $resource Mailjet Resource/Action pair - * @param array $args Request arguments - * @param array $options - * - * @return Response + * @param array $resource Mailjet Resource/Action pair + * @param array $args Request arguments * * @throws MailjetException */ @@ -165,11 +151,8 @@ public function put(array $resource, array $args = [], array $options = []): Res /** * Trigger a DELETE request. * - * @param array $resource Mailjet Resource/Action pair - * @param array $args Request arguments - * @param array $options - * - * @return Response + * @param array $resource Mailjet Resource/Action pair + * @param array $args Request arguments * * @throws MailjetException */ diff --git a/src/MailjetEmailMessage.php b/src/Messages/MailjetEmailMessage.php similarity index 97% rename from src/MailjetEmailMessage.php rename to src/Messages/MailjetEmailMessage.php index ab78ebe..6373bbd 100644 --- a/src/MailjetEmailMessage.php +++ b/src/Messages/MailjetEmailMessage.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace YieldStudio\LaravelMailjetNotifier; +namespace YieldStudio\LaravelMailjetNotifier\Messages; use Illuminate\Support\Traits\Conditionable; -class MailjetEmailMessage +final class MailjetEmailMessage { use Conditionable; diff --git a/src/MailjetSmsMessage.php b/src/Messages/MailjetSmsMessage.php similarity index 92% rename from src/MailjetSmsMessage.php rename to src/Messages/MailjetSmsMessage.php index b46d91c..7118890 100644 --- a/src/MailjetSmsMessage.php +++ b/src/Messages/MailjetSmsMessage.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace YieldStudio\LaravelMailjetNotifier; +namespace YieldStudio\LaravelMailjetNotifier\Messages; use Illuminate\Support\Traits\Conditionable; diff --git a/tests/MailjetEmailChannelTest.php b/tests/MailjetEmailChannelTest.php index 756ef43..13c90ec 100644 --- a/tests/MailjetEmailChannelTest.php +++ b/tests/MailjetEmailChannelTest.php @@ -1,24 +1,27 @@ mock(MailjetService::class)->shouldReceive('sendEmail')->once(); $channel = new MailjetEmailChannel($mock->getMock()); - $channel->send(new User(), new class extends Notification { - public function via() + $channel->send(new User, new class extends Notification + { + public function via(): array { return [MailjetEmailChannel::class]; } public function toMailjetEmail(User $notifiable): MailjetEmailMessage { - return new MailjetEmailMessage(); + return new MailjetEmailMessage; } }); }); diff --git a/tests/MailjetServiceTest.php b/tests/MailjetServiceTest.php index fd54438..35e4005 100644 --- a/tests/MailjetServiceTest.php +++ b/tests/MailjetServiceTest.php @@ -1,15 +1,16 @@ true]), ); @@ -23,14 +24,14 @@ 'url' => str_replace('http://', '', $bypass->getBaseUrl()), ]); - $message = (new MailjetEmailMessage())->templateId(1); + $message = (new MailjetEmailMessage)->templateId(1); $result = $mailjetService->sendEmail($message); $bypass->assertRoutes(); - assertInstanceOf(Response::class, $result); + expect($result)->toBeInstanceOf(Response::class); }); -it('returns a MailjetException when sending email fails', function () { +it('returns a MailjetException when sending email fails', function (): void { $bypass = Bypass::serve( Route::post('/v3/send', ['success' => false], 400), ); @@ -44,12 +45,12 @@ 'url' => str_replace('http://', '', $bypass->getBaseUrl()), ]); - $message = (new MailjetEmailMessage())->templateId(1); + $message = (new MailjetEmailMessage)->templateId(1); $mailjetService->sendEmail($message); })->throws(MailjetException::class); -it('returns a Response instance when the sms is sent correctly', function () { +it('returns a Response instance when the sms is sent correctly', function (): void { $bypass = Bypass::serve( Route::post('/v4/sms-send', ['success' => true]), ); @@ -62,14 +63,14 @@ $mailjetService->setSmsToken('testing'); - $message = (new MailjetSmsMessage())->to('0601020304')->text('Hello'); + $message = (new MailjetSmsMessage)->to('0601020304')->text('Hello'); $result = $mailjetService->sendSms($message); $bypass->assertRoutes(); - assertInstanceOf(Response::class, $result); + expect($result)->toBeInstanceOf(Response::class); }); -it('returns a MailjetException when sending sms fails', function () { +it('returns a MailjetException when sending sms fails', function (): void { $bypass = Bypass::serve( Route::post('/v4/sms-send', ['success' => false], 400), ); @@ -82,6 +83,6 @@ $mailjetService->setSmsToken('testing'); - $message = (new MailjetSmsMessage())->to('0601020304')->text('Hello'); + $message = (new MailjetSmsMessage)->to('0601020304')->text('Hello'); $mailjetService->sendSms($message); })->throws(MailjetException::class); diff --git a/tests/MailjetSmsChannelTest.php b/tests/MailjetSmsChannelTest.php index ee3d0be..828729b 100644 --- a/tests/MailjetSmsChannelTest.php +++ b/tests/MailjetSmsChannelTest.php @@ -1,24 +1,28 @@ mock(MailjetService::class)->shouldReceive('sendSms')->once(); $channel = new MailjetSmsChannel($mock->getMock()); - $channel->send(new User(), new class extends Notification { - public function via() + + $channel->send(new User, new class extends Notification + { + public function via(): array { return [MailjetSmsChannel::class]; } public function toMailjetSms(Model $notifiable): MailjetSmsMessage { - return new MailjetSmsMessage(); + return new MailjetSmsMessage; } }); }); diff --git a/tests/User.php b/tests/Models/User.php similarity index 50% rename from tests/User.php rename to tests/Models/User.php index 73746b6..32cacd1 100644 --- a/tests/User.php +++ b/tests/Models/User.php @@ -1,10 +1,13 @@ in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5df56ba..4f3509a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,7 @@ Date: Fri, 20 Sep 2024 09:47:02 +0200 Subject: [PATCH 2/2] fix: Add testbench on test run action --- .github/workflows/tests.yml | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f724186..00d5d48 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -46,7 +46,7 @@ jobs: - name: Install dependencies run: | - composer require "laravel/framework:${{ matrix.laravel }}" --dev --no-interaction --no-update + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - name: Execute tests run: vendor/bin/pest diff --git a/composer.json b/composer.json index 719ab83..1737f2f 100644 --- a/composer.json +++ b/composer.json @@ -36,9 +36,9 @@ "require-dev": { "ciareis/bypass": "^1.0", "laravel/pint": "^1.16", - "orchestra/testbench": "^7.44|^8.25|^9.0", + "orchestra/testbench": "^7|^8|^9", "pestphp/pest": "^2.34", - "phpunit/phpunit": "^9.5|^10.5" + "phpunit/phpunit": "^9|^10" }, "autoload": { "psr-4": {