Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests with newer Laravel 11 versions #33

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
parameters:
ignoreErrors:
-
message: "#^Method InnoGE\\\\LaravelMsGraphMail\\\\MicrosoftGraphTransport\\:\\:transformEmailAddress\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/MicrosoftGraphTransport.php

-
message: "#^Method InnoGE\\\\LaravelMsGraphMail\\\\MicrosoftGraphTransport\\:\\:transformEmailAddresses\\(\\) should return array\\<array\\<string, array\\<string, string\\>\\>\\> but returns array\\.$#"
count: 1
path: src/MicrosoftGraphTransport.php

-
message: "#^Parameter \\#1 \\$message of static method Symfony\\\\Component\\\\Mime\\\\MessageConverter\\:\\:toEmail\\(\\) expects Symfony\\\\Component\\\\Mime\\\\Message, Symfony\\\\Component\\\\Mime\\\\RawMessage given\\.$#"
count: 1
Expand All @@ -9,3 +19,13 @@ parameters:
message: "#^Parameter \\#2 \\$html of method InnoGE\\\\LaravelMsGraphMail\\\\MicrosoftGraphTransport\\:\\:prepareAttachments\\(\\) expects string\\|null, resource\\|string\\|null given\\.$#"
count: 1
path: src/MicrosoftGraphTransport.php

-
message: "#^Method InnoGE\\\\LaravelMsGraphMail\\\\Services\\\\MicrosoftGraphApiService\\:\\:getAccessToken\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/Services/MicrosoftGraphApiService.php

-
message: "#^Method InnoGE\\\\LaravelMsGraphMail\\\\Services\\\\MicrosoftGraphApiService\\:\\:sendMail\\(\\) has parameter \\$payload with no value type specified in iterable type array\\.$#"
count: 1
path: src/Services/MicrosoftGraphApiService.php
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

class InvalidResponse extends Exception
{
}
class InvalidResponse extends Exception {}
4 changes: 4 additions & 0 deletions src/MicrosoftGraphTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ protected function doSend(SentMessage $message): void
$this->microsoftGraphApiService->sendMail($envelope->getSender()->getAddress(), $payload);
}

/**
* @return array<int, array<int<0, max>, array<string, bool|string|null>>|string|null>
*/
protected function prepareAttachments(Email $email, ?string $html): array
{
$attachments = [];
Expand All @@ -83,6 +86,7 @@ protected function prepareAttachments(Email $email, ?string $html): array

/**
* @param Collection<array-key, Address> $recipients
* @return array<array-key, array<string, array<string, string>>>
*/
protected function transformEmailAddresses(Collection $recipients): array
{
Expand Down
12 changes: 2 additions & 10 deletions src/Services/MicrosoftGraphApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace InnoGE\LaravelMsGraphMail\Services;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
Expand All @@ -16,12 +15,8 @@ public function __construct(
protected readonly string $clientId,
protected readonly string $clientSecret,
protected readonly int $accessTokenTtl
) {
}
) {}

/**
* @throws RequestException
*/
public function sendMail(string $from, array $payload): Response
{
return $this->getBaseRequest()
Expand Down Expand Up @@ -50,10 +45,7 @@ protected function getAccessToken(): string
$response->throw();

$accessToken = $response->json('access_token');
if (! is_string($accessToken)) {
$notString = var_export($accessToken, true);
throw new InvalidResponse("Expected response to contain key access_token of type string, got: {$notString}.");
}
throw_unless(is_string($accessToken), new InvalidResponse('Expected response to contain key access_token of type string, got: '.var_export($accessToken, true).'.'));

return $accessToken;
});
Expand Down
14 changes: 3 additions & 11 deletions tests/MicrosoftGraphTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Mail::to('[email protected]')
->bcc('[email protected]')
->cc('[email protected]')
->send(new TestMail());
->send(new TestMail);

Http::assertSent(function (Request $value) {
expect($value)
Expand Down Expand Up @@ -371,11 +371,11 @@
Mail::to('[email protected]')
->bcc('[email protected]')
->cc('[email protected]')
->send(new TestMailWithInlineImage());
->send(new TestMailWithInlineImage);

Http::assertSent(function (Request $value) {
// ContentId gets random generated, so get this value first and check for equality later
$inlineImageContentId = json_decode($value->body())->message->attachments[1]->contentId;
$inlineImageContentId = json_decode($value->body())->message->attachments[0]->contentId;

expect($value)
->url()->toBe('https://graph.microsoft.com/v1.0/users/[email protected]/sendMail')
Expand Down Expand Up @@ -415,14 +415,6 @@
],
],
'attachments' => [
[
'@odata.type' => '#microsoft.graph.fileAttachment',
'name' => 'test-file-1.txt',
'contentType' => 'text',
'contentBytes' => 'Zm9vCg==',
'contentId' => 'test-file-1.txt',
'isInline' => false,
],
[
'@odata.type' => '#microsoft.graph.fileAttachment',
'name' => $inlineImageContentId,
Expand Down
4 changes: 1 addition & 3 deletions tests/Stubs/TestMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class TestMail extends Mailable
*
* @return void
*/
public function __construct(private readonly bool $isHtml = true)
{
}
public function __construct(private readonly bool $isHtml = true) {}

/**
* Get the message envelope.
Expand Down
15 changes: 1 addition & 14 deletions tests/Stubs/TestMailWithInlineImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace InnoGE\LaravelMsGraphMail\Tests\Stubs;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
Expand All @@ -18,9 +17,7 @@ class TestMailWithInlineImage extends Mailable
*
* @return void
*/
public function __construct(private readonly bool $isHtml = true)
{
}
public function __construct(private readonly bool $isHtml = true) {}

/**
* Get the message envelope.
Expand All @@ -47,14 +44,4 @@ public function content()

return new Content(html: 'html-mail-with-inline-image');
}

/**
* Get the attachments for the message.
*/
public function attachments(): array
{
return [
Attachment::fromPath('tests/Resources/files/test-file-1.txt'),
];
}
}