fix: move local packages inside repo so remote server can install them
This commit is contained in:
1 parent
50e5f3e381
commit
30b2fb3958
40 files changed
+1671
-2
No files matched your search
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests;
|
||||
|
||||
use PromoSeven\AzureMailer\Exceptions\AuthenticationException;
|
||||
use PromoSeven\AzureMailer\Exceptions\GraphApiException;
|
||||
|
||||
class ExceptionsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test_authentication_exception_formats_message(): void
|
||||
{
|
||||
$e = AuthenticationException::fromResponse('invalid_client', 'The client secret is incorrect.');
|
||||
|
||||
$this->assertInstanceOf(\RuntimeException::class, $e);
|
||||
$this->assertStringContainsString('invalid_client', $e->getMessage());
|
||||
$this->assertStringContainsString('The client secret is incorrect.', $e->getMessage());
|
||||
}
|
||||
|
||||
public function test_graph_api_exception_formats_message(): void
|
||||
{
|
||||
$e = GraphApiException::fromResponse('ErrorItemNotFound', 'The specified object was not found.');
|
||||
|
||||
$this->assertInstanceOf(\RuntimeException::class, $e);
|
||||
$this->assertStringContainsString('ErrorItemNotFound', $e->getMessage());
|
||||
$this->assertStringContainsString('The specified object was not found.', $e->getMessage());
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests\Graph;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PromoSeven\AzureMailer\Exceptions\GraphApiException;
|
||||
use PromoSeven\AzureMailer\Graph\GraphClient;
|
||||
use PromoSeven\AzureMailer\Graph\TokenManager;
|
||||
use PromoSeven\AzureMailer\Tests\TestCase;
|
||||
|
||||
class GraphClientTest extends TestCase
|
||||
{
|
||||
private array $config = [
|
||||
'tenant_id' => 'test-tenant',
|
||||
'client_id' => 'test-client-id',
|
||||
'client_secret' => 'test-secret',
|
||||
'from_address' => '[email protected]',
|
||||
'timeout' => 30,
|
||||
'graph_api_version' => 'v1.0',
|
||||
];
|
||||
|
||||
private array $payload = [
|
||||
'message' => [
|
||||
'subject' => 'Test',
|
||||
'body' => ['contentType' => 'HTML', 'content' => '<p>Hello</p>'],
|
||||
'toRecipients' => [['emailAddress' => ['address' => '[email protected]', 'name' => '']]],
|
||||
'ccRecipients' => [],
|
||||
'bccRecipients' => [],
|
||||
'replyTo' => [],
|
||||
'attachments' => [],
|
||||
],
|
||||
'saveToSentItems' => false,
|
||||
];
|
||||
|
||||
public function test_sends_payload_with_bearer_token(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response(['access_token' => 'tok', 'expires_in' => 3600], 200),
|
||||
'graph.microsoft.com/*' => Http::response('', 202),
|
||||
]);
|
||||
|
||||
$client = new GraphClient(new TokenManager($this->config), $this->config);
|
||||
$client->send($this->payload);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'graph.microsoft.com/v1.0/users/[email protected]/sendMail')
|
||||
&& $request->hasHeader('Authorization', 'Bearer tok');
|
||||
});
|
||||
}
|
||||
|
||||
public function test_retries_with_fresh_token_on_401(): void
|
||||
{
|
||||
Log::shouldReceive('warning')
|
||||
->once()
|
||||
->with('azure-mailer: 401 received, retrying with fresh token');
|
||||
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::sequence()
|
||||
->push(['access_token' => 'stale-token', 'expires_in' => 3600], 200)
|
||||
->push(['access_token' => 'fresh-token', 'expires_in' => 3600], 200),
|
||||
'graph.microsoft.com/*' => Http::sequence()
|
||||
->push('', 401)
|
||||
->push('', 202),
|
||||
]);
|
||||
|
||||
$client = new GraphClient(new TokenManager($this->config), $this->config);
|
||||
$client->send($this->payload);
|
||||
|
||||
Http::assertSentCount(4); // 2 token fetches + 2 graph calls
|
||||
}
|
||||
|
||||
public function test_throws_graph_api_exception_on_second_401(): void
|
||||
{
|
||||
Log::shouldReceive('warning')->once();
|
||||
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response(['access_token' => 'tok', 'expires_in' => 3600], 200),
|
||||
'graph.microsoft.com/*' => Http::response([
|
||||
'error' => ['code' => 'InvalidAuthenticationToken', 'message' => 'Token is expired.'],
|
||||
], 401),
|
||||
]);
|
||||
|
||||
$this->expectException(GraphApiException::class);
|
||||
$this->expectExceptionMessage('InvalidAuthenticationToken');
|
||||
|
||||
$client = new GraphClient(new TokenManager($this->config), $this->config);
|
||||
$client->send($this->payload);
|
||||
}
|
||||
|
||||
public function test_throws_graph_api_exception_on_other_error(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response(['access_token' => 'tok', 'expires_in' => 3600], 200),
|
||||
'graph.microsoft.com/*' => Http::response([
|
||||
'error' => ['code' => 'ErrorInvalidRecipients', 'message' => 'Recipient address is invalid.'],
|
||||
], 400),
|
||||
]);
|
||||
|
||||
$this->expectException(GraphApiException::class);
|
||||
$this->expectExceptionMessage('ErrorInvalidRecipients');
|
||||
|
||||
$client = new GraphClient(new TokenManager($this->config), $this->config);
|
||||
$client->send($this->payload);
|
||||
}
|
||||
|
||||
public function test_uses_graph_api_version_from_config(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response(['access_token' => 'tok', 'expires_in' => 3600], 200),
|
||||
'graph.microsoft.com/*' => Http::response('', 202),
|
||||
]);
|
||||
|
||||
$config = array_merge($this->config, ['graph_api_version' => 'beta']);
|
||||
$client = new GraphClient(new TokenManager($config), $config);
|
||||
$client->send($this->payload);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'graph.microsoft.com/beta/');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests\Graph;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PromoSeven\AzureMailer\Exceptions\AuthenticationException;
|
||||
use PromoSeven\AzureMailer\Graph\TokenManager;
|
||||
use PromoSeven\AzureMailer\Tests\TestCase;
|
||||
|
||||
class TokenManagerTest extends TestCase
|
||||
{
|
||||
private array $config = [
|
||||
'tenant_id' => 'test-tenant',
|
||||
'client_id' => 'test-client-id',
|
||||
'client_secret' => 'test-secret',
|
||||
];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Cache::flush();
|
||||
}
|
||||
|
||||
public function test_fetches_token_from_azure_on_cache_miss(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response([
|
||||
'access_token' => 'my-access-token',
|
||||
'expires_in' => 3600,
|
||||
'token_type' => 'Bearer',
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$manager = new TokenManager($this->config);
|
||||
$token = $manager->getToken();
|
||||
|
||||
$this->assertSame('my-access-token', $token);
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'test-tenant/oauth2/v2.0/token')
|
||||
&& $request['grant_type'] === 'client_credentials'
|
||||
&& $request['client_id'] === 'test-client-id'
|
||||
&& $request['client_secret'] === 'test-secret'
|
||||
&& $request['scope'] === 'https://graph.microsoft.com/.default';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_returns_cached_token_without_hitting_azure(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response([
|
||||
'access_token' => 'first-token',
|
||||
'expires_in' => 3600,
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$manager = new TokenManager($this->config);
|
||||
$manager->getToken(); // first call — hits Azure
|
||||
$manager->getToken(); // second call — should use cache
|
||||
|
||||
Http::assertSentCount(1);
|
||||
}
|
||||
|
||||
public function test_invalidate_clears_cached_token(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::sequence()
|
||||
->push(['access_token' => 'token-one', 'expires_in' => 3600], 200)
|
||||
->push(['access_token' => 'token-two', 'expires_in' => 3600], 200),
|
||||
]);
|
||||
|
||||
$manager = new TokenManager($this->config);
|
||||
$manager->getToken(); // fetches token-one
|
||||
$manager->invalidate(); // clears cache
|
||||
$second = $manager->getToken(); // fetches token-two
|
||||
|
||||
$this->assertSame('token-two', $second);
|
||||
Http::assertSentCount(2);
|
||||
}
|
||||
|
||||
public function test_token_is_cached_with_ttl_of_expires_in_minus_60(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response([
|
||||
'access_token' => 'ttl-token',
|
||||
'expires_in' => 120, // 120 - 60 = 60 second TTL
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$manager = new TokenManager($this->config);
|
||||
$manager->getToken();
|
||||
|
||||
// Token should be in cache immediately after fetch
|
||||
$this->assertTrue(Cache::has('azure_mailer_token_test-client-id'));
|
||||
|
||||
// Advance time past the TTL (61 seconds)
|
||||
$this->travel(61)->seconds();
|
||||
|
||||
// Token should now be expired from cache
|
||||
$this->assertFalse(Cache::has('azure_mailer_token_test-client-id'));
|
||||
}
|
||||
|
||||
public function test_throws_authentication_exception_on_azure_error(): void
|
||||
{
|
||||
Http::fake([
|
||||
'login.microsoftonline.com/*' => Http::response([
|
||||
'error' => 'invalid_client',
|
||||
'error_description' => 'The client secret supplied is incorrect.',
|
||||
], 401),
|
||||
]);
|
||||
|
||||
$this->expectException(AuthenticationException::class);
|
||||
$this->expectExceptionMessage('invalid_client');
|
||||
|
||||
(new TokenManager($this->config))->getToken();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests;
|
||||
|
||||
use Illuminate\Mail\MailManager;
|
||||
use PromoSeven\AzureMailer\Transport\AzureTransport;
|
||||
|
||||
class ServiceProviderTest extends TestCase
|
||||
{
|
||||
protected function defineEnvironment($app): void
|
||||
{
|
||||
parent::defineEnvironment($app);
|
||||
|
||||
$app['config']->set('mail.mailers.azure', [
|
||||
'transport' => 'azure',
|
||||
'tenant_id' => 'test-tenant',
|
||||
'client_id' => 'test-client-id',
|
||||
'client_secret' => 'test-secret',
|
||||
'from_address' => '[email protected]',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_azure_transport_is_registered_with_mail_manager(): void
|
||||
{
|
||||
$manager = $this->app->make(MailManager::class);
|
||||
$transport = $manager->mailer('azure')->getSymfonyTransport();
|
||||
|
||||
$this->assertInstanceOf(AzureTransport::class, $transport);
|
||||
$this->assertSame('azure', (string) $transport);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests;
|
||||
|
||||
use Orchestra\Testbench\TestCase as OrchestraTestCase;
|
||||
use PromoSeven\AzureMailer\AzureMailerServiceProvider;
|
||||
|
||||
abstract class TestCase extends OrchestraTestCase
|
||||
{
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
return [AzureMailerServiceProvider::class];
|
||||
}
|
||||
|
||||
protected function defineEnvironment($app): void
|
||||
{
|
||||
$app['config']->set('cache.default', 'array');
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PromoSeven\AzureMailer\Tests\Transport;
|
||||
|
||||
use PromoSeven\AzureMailer\Graph\GraphClient;
|
||||
use PromoSeven\AzureMailer\Tests\TestCase;
|
||||
use PromoSeven\AzureMailer\Transport\AzureTransport;
|
||||
use Symfony\Component\Mailer\Envelope;
|
||||
use Symfony\Component\Mime\Email;
|
||||
|
||||
class AzureTransportTest extends TestCase
|
||||
{
|
||||
public function test_sends_html_body(): void
|
||||
{
|
||||
$captured = [];
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$client->expects($this->once())
|
||||
->method('send')
|
||||
->willReturnCallback(function (array $payload) use (&$captured) {
|
||||
$captured = $payload;
|
||||
});
|
||||
|
||||
$email = (new Email())
|
||||
->from('[email protected]')
|
||||
->to('[email protected]')
|
||||
->subject('Hello')
|
||||
->html('<p>World</p>');
|
||||
|
||||
$transport = new AzureTransport($client, ['save_to_sent_items' => false]);
|
||||
$transport->send($email, Envelope::create($email));
|
||||
|
||||
$this->assertSame('Hello', $captured['message']['subject']);
|
||||
$this->assertSame('HTML', $captured['message']['body']['contentType']);
|
||||
$this->assertSame('<p>World</p>', $captured['message']['body']['content']);
|
||||
$this->assertSame('[email protected]', $captured['message']['toRecipients'][0]['emailAddress']['address']);
|
||||
$this->assertFalse($captured['saveToSentItems']);
|
||||
}
|
||||
|
||||
public function test_falls_back_to_text_body_when_no_html(): void
|
||||
{
|
||||
$captured = [];
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$client->method('send')->willReturnCallback(function ($p) use (&$captured) { $captured = $p; });
|
||||
|
||||
$email = (new Email())
|
||||
->from('[email protected]')
|
||||
->to('[email protected]')
|
||||
->subject('Text only')
|
||||
->text('Plain text content');
|
||||
|
||||
$transport = new AzureTransport($client, ['save_to_sent_items' => false]);
|
||||
$transport->send($email, Envelope::create($email));
|
||||
|
||||
$this->assertSame('Text', $captured['message']['body']['contentType']);
|
||||
$this->assertSame('Plain text content', $captured['message']['body']['content']);
|
||||
}
|
||||
|
||||
public function test_maps_cc_bcc_and_reply_to(): void
|
||||
{
|
||||
$captured = [];
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$client->method('send')->willReturnCallback(function ($p) use (&$captured) { $captured = $p; });
|
||||
|
||||
$email = (new Email())
|
||||
->from('[email protected]')
|
||||
->to('[email protected]')
|
||||
->cc('[email protected]')
|
||||
->bcc('[email protected]')
|
||||
->replyTo('[email protected]')
|
||||
->subject('Recipients test')
|
||||
->html('<p>hi</p>');
|
||||
|
||||
$transport = new AzureTransport($client, ['save_to_sent_items' => false]);
|
||||
$transport->send($email, Envelope::create($email));
|
||||
|
||||
$this->assertSame('[email protected]', $captured['message']['ccRecipients'][0]['emailAddress']['address']);
|
||||
$this->assertSame('[email protected]', $captured['message']['bccRecipients'][0]['emailAddress']['address']);
|
||||
$this->assertSame('[email protected]', $captured['message']['replyTo'][0]['emailAddress']['address']);
|
||||
}
|
||||
|
||||
public function test_encodes_attachments_as_base64(): void
|
||||
{
|
||||
$captured = [];
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$client->method('send')->willReturnCallback(function ($p) use (&$captured) { $captured = $p; });
|
||||
|
||||
$email = (new Email())
|
||||
->from('[email protected]')
|
||||
->to('[email protected]')
|
||||
->subject('With attachment')
|
||||
->html('<p>see attachment</p>')
|
||||
->attach('PDF content here', 'report.pdf', 'application/pdf');
|
||||
|
||||
$transport = new AzureTransport($client, ['save_to_sent_items' => false]);
|
||||
$transport->send($email, Envelope::create($email));
|
||||
|
||||
$attachment = $captured['message']['attachments'][0];
|
||||
$this->assertSame('#microsoft.graph.fileAttachment', $attachment['@odata.type']);
|
||||
$this->assertSame('report.pdf', $attachment['name']);
|
||||
$this->assertSame(base64_encode('PDF content here'), $attachment['contentBytes']);
|
||||
$this->assertSame('application/pdf', $attachment['contentType']);
|
||||
}
|
||||
|
||||
public function test_save_to_sent_items_is_configurable(): void
|
||||
{
|
||||
$captured = [];
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$client->method('send')->willReturnCallback(function ($p) use (&$captured) { $captured = $p; });
|
||||
|
||||
$email = (new Email())
|
||||
->from('[email protected]')
|
||||
->to('[email protected]')
|
||||
->subject('Save it')
|
||||
->html('<p>keep</p>');
|
||||
|
||||
$transport = new AzureTransport($client, ['save_to_sent_items' => true]);
|
||||
$transport->send($email, Envelope::create($email));
|
||||
|
||||
$this->assertTrue($captured['saveToSentItems']);
|
||||
}
|
||||
|
||||
public function test_to_string_returns_azure(): void
|
||||
{
|
||||
$client = $this->createMock(GraphClient::class);
|
||||
$transport = new AzureTransport($client, []);
|
||||
|
||||
$this->assertSame('azure', (string) $transport);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user