Skip to content

Commit

Permalink
Add Support for Laravel 9 / Drop Support for Laravel 8 (#8)
Browse files Browse the repository at this point in the history
* Update run-tests.yml Workflow

* Update Composer Constraints to Support Laravel 9

* Fix StoreMailables

* Turn empty names to null
  • Loading branch information
stefanzweifel committed Jan 18, 2022
1 parent dcccbb2 commit df3bdbb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 55 deletions.
57 changes: 24 additions & 33 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: run-tests

on:
push:
branches: [main]
pull_request:
branches: [main]
on: [push, pull_request]

jobs:
test:
Expand All @@ -13,35 +9,30 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0, 8.1]
laravel: [8.*]
stability: [prefer-lowest, prefer-stable]
php: [8.1, 8.0]
laravel: [9.*]
dependency-version: [prefer-stable]
include:
- laravel: 8.*
testbench: ^6.6
- laravel: 9.*
testbench: 7.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: pcov

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/pest --coverage
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: pcov

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/pest --coverage
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"require": {
"php": "^8.0",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^8.73"
"illuminate/contracts": "^9.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.3",
"nunomaduro/collision": "^5.10",
"friendsofphp/php-cs-fixer": "^3.4",
"nunomaduro/collision": "^5.10 |^6.0",
"nunomaduro/larastan": "^1.0",
"orchestra/testbench": "^6.22",
"orchestra/testbench": "^7",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpstan/extension-installer": "^1.1",
Expand Down
31 changes: 22 additions & 9 deletions src/Listeners/StoreOutgoingMailListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Collection;
use ReflectionClass;
use Symfony\Component\Mime\Address;
use Wnx\Sends\Contracts\HasSends;
use Wnx\Sends\Models\Send;

Expand All @@ -30,11 +31,11 @@ protected function createSendModel(MessageSent $event): Send
'mail_class' => $this->getMailClassHeaderValue($event),
'subject' => $event->message->getSubject(),
'content' => $this->getContent($event),
'from' => $event->message->getFrom(),
'reply_to' => $event->message->getReplyTo(),
'to' => $event->message->getTo(),
'cc' => $event->message->getCc(),
'bcc' => $event->message->getBcc(),
'from' => $this->getAddressesValue($event->message->getFrom()),
'reply_to' => $this->getAddressesValue($event->message->getReplyTo()),
'to' => $this->getAddressesValue($event->message->getTo()),
'cc' => $this->getAddressesValue($event->message->getCc()),
'bcc' => $this->getAddressesValue($event->message->getBcc()),
'sent_at' => now(),
]);
}
Expand All @@ -51,7 +52,7 @@ protected function getSendUuid(MessageSent $event): ?string
return null;
}

return $headerValue->getFieldBody();
return $headerValue->getBodyAsString();
}

protected function getMailClassHeaderValue(MessageSent $event): ?string
Expand All @@ -66,7 +67,7 @@ protected function getMailClassHeaderValue(MessageSent $event): ?string
return null;
}

return decrypt($headerValue->getFieldBody());
return decrypt($headerValue->getBodyAsString());
}

/**
Expand All @@ -93,7 +94,7 @@ protected function getModels(MessageSent $event): Collection
return collect([]);
}

$models = decrypt($headerValue->getFieldBody());
$models = decrypt($headerValue->getBodyAsString());

return collect(json_decode($models, true, 512, JSON_THROW_ON_ERROR))
->map(function (array $tuple): Model {
Expand All @@ -111,6 +112,18 @@ protected function getContent(MessageSent $event): ?string
return null;
}

return $event->message->getBody();
return $event->message->getHtmlBody();
}

/**
* @param array<Address> $address
* @return Collection|null
*/
private function getAddressesValue(array $address): ?Collection
{
$addresses = collect($address)
->flatMap(fn (Address $address) => [$address->getAddress() => $address->getName() === '' ? null : $address->getName()]);

return $addresses->count() > 0 ? $addresses : null;
}
}
6 changes: 3 additions & 3 deletions src/Support/StoreMailables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionProperty;
use Swift_Message;
use Symfony\Component\Mime\Email;
use Wnx\Sends\Contracts\HasSends;

trait StoreMailables
{
protected function storeClassName(): self
{
$this->withSwiftMessage(function (Swift_Message $message) {
$this->withSymfonyMessage(function (Email $message) {
$message->getHeaders()->addTextHeader(config('sends.headers.mail_class'), encrypt(self::class));
});

Expand Down Expand Up @@ -46,7 +46,7 @@ protected function associateWith(array|HasSends $models = []): static
])
->toJson();

$this->withSwiftMessage(function (Swift_Message $message) use ($models) {
$this->withSymfonyMessage(function (Email $message) use ($models) {
$message->getHeaders()->addTextHeader(config('sends.headers.models'), encrypt($models));
});

Expand Down
3 changes: 2 additions & 1 deletion tests/Listeners/AttachSendUuidListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Illuminate\Mail\Events\MessageSending;
use function PHPUnit\Framework\assertNotNull;
use function PHPUnit\Framework\assertTrue;
use Symfony\Component\Mime\Email;
use Wnx\Sends\Listeners\AttachSendUuidListener;

it('attaches send uuid header', function () {
$event = new MessageSending(new Swift_Message());
$event = new MessageSending(new Email());

(new AttachSendUuidListener())->handle($event);

Expand Down
8 changes: 3 additions & 5 deletions tests/Listeners/StoreOutgoingMailListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
use Wnx\Sends\Tests\TestSupport\Models\TestModelWithoutHasSendsContract;
use Wnx\Sends\Tests\TestSupport\Notifications\TestNotification;

it('stores_outgoing_mails_in_database_table', function () {
it('stores outgoing mails in database table', function () {
Mail::to([
[
'email' => '[email protected]',
'name' => 'To 1 Name',
], [
'email' => '[email protected]',
'name' => 'To 2 Name',
],
'[email protected]',
])
->send(new TestMail());

Expand All @@ -37,7 +35,7 @@
'subject' => '::subject::',
'to' => json_encode([
'[email protected]' => 'To 1 Name',
'[email protected]' => 'To 2 Name',
'[email protected]' => null,
]),
'cc' => null,
'bcc' => null,
Expand Down

0 comments on commit df3bdbb

Please sign in to comment.