generated from spiral-packages/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae7b244
commit 3306cb7
Showing
11 changed files
with
517 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Notifications\Tests\App\Notifications; | ||
|
||
use Spiral\Queue\QueueableInterface; | ||
use Symfony\Component\Notifier\Message\EmailMessage; | ||
use Symfony\Component\Notifier\Notification\Notification; | ||
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; | ||
use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
|
||
class UserNotification extends Notification implements QueueableInterface | ||
{ | ||
public function getChannels(RecipientInterface $recipient): array | ||
{ | ||
return ['email']; | ||
} | ||
|
||
public function asEmailMessage(EmailRecipientInterface $recipient, string $transport = null): ?EmailMessage | ||
{ | ||
return EmailMessage::fromNotification($this, $recipient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Notifications\Tests; | ||
|
||
use Mockery as m; | ||
use Spiral\Core\FactoryInterface; | ||
use Spiral\Notifications\ChannelManager; | ||
use Spiral\Notifications\Config\NotificationsConfig; | ||
use Spiral\SendIt\Config\MailerConfig; | ||
use Symfony\Component\Mailer\Transport\RoundRobinTransport as MailerRoundRobinTransport; | ||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; | ||
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransport; | ||
use Symfony\Component\Notifier\Channel\ChannelInterface; | ||
use Symfony\Component\Notifier\Channel\EmailChannel; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\RoundRobinTransport; | ||
|
||
final class ChannelManagerTest extends TestCase | ||
{ | ||
private ChannelManager $manager; | ||
private m\LegacyMockInterface|m\MockInterface|FactoryInterface $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$config = new NotificationsConfig([ | ||
'channels' => [ | ||
'email_single' => [ | ||
'type' => EmailChannel::class, | ||
'transport' => 'gmail', | ||
], | ||
'email_multiple' => [ | ||
'type' => EmailChannel::class, | ||
'transport' => ['gmail', 'yahoo'], | ||
], | ||
'firebase' => [ | ||
'type' => 'firebase', | ||
'transport' => 'firebase', | ||
], | ||
'firebase_multiple' => [ | ||
'type' => 'firebase_multiple', | ||
'transport' => ['firebase1', 'firebase'], | ||
], | ||
'unknown' => [ | ||
'type' => 'unknown', | ||
'transport' => 'unknown', | ||
], | ||
'unsupported' => [ | ||
'type' => 'unsupported', | ||
'transport' => 'unsupported', | ||
], | ||
], | ||
'transports' => [ | ||
'gmail' => 'smtp://gmail:[email protected]:25', | ||
'yahoo' => 'smtp://yahoo:[email protected]:25', | ||
'firebase' => 'firebase://USERNAME:PASSWORD@default', | ||
'firebase1' => 'firebase://USERNAME1:PASSWORD@default', | ||
'unknown' => 'foo://USERNAME:PASSWORD@default', | ||
'unsupported' => 'discord://TOKEN@default?webhook_id=ID', | ||
], | ||
]); | ||
|
||
$this->manager = new ChannelManager( | ||
$this->factory = m::mock(FactoryInterface::class), | ||
$config, | ||
new MailerConfig([ | ||
'dsn' => 'smtp://user:[email protected]:25', | ||
'from' => '[email protected]', | ||
'queue' => null, | ||
'pipeline' => null, | ||
'queueConnection' => null, | ||
]) | ||
); | ||
} | ||
|
||
public function testGetsEmailChannelWithSingleTransport(): void | ||
{ | ||
$this->factory->shouldReceive('make')->once() | ||
->withArgs(static function (string $type, array $args): bool { | ||
return $type === EmailChannel::class | ||
&& $args['transport'] instanceof EsmtpTransport | ||
&& (string)$args['transport'] === 'smtp://smtp.gmail.com' | ||
&& $args['from'] === '[email protected]'; | ||
}) | ||
->andReturn($channel = m::mock(ChannelInterface::class)); | ||
|
||
$this->assertSame( | ||
$channel, | ||
$this->manager->getChannel('email_single') | ||
); | ||
} | ||
|
||
public function testGetsEmailChannelWithMultipleTransport(): void | ||
{ | ||
$this->factory->shouldReceive('make')->once() | ||
->withArgs(static function (string $type, array $args): bool { | ||
return $type === EmailChannel::class | ||
&& $args['transport'] instanceof MailerRoundRobinTransport | ||
&& (string)$args['transport'] === 'roundrobin(smtp://smtp.gmail.com smtp://smtp.yahoo.com)' | ||
&& $args['from'] === '[email protected]'; | ||
}) | ||
->andReturn($channel = m::mock(ChannelInterface::class)); | ||
|
||
$this->assertSame( | ||
$channel, | ||
$this->manager->getChannel('email_multiple') | ||
); | ||
} | ||
|
||
public function testGetsNotificationChannelWithSingleTransport(): void | ||
{ | ||
$this->factory->shouldReceive('make')->once() | ||
->withArgs(static function (string $type, array $args): bool { | ||
return $type === 'firebase' | ||
&& $args['transport'] instanceof FirebaseTransport | ||
&& (string)$args['transport'] === 'firebase://fcm.googleapis.com/fcm/send'; | ||
}) | ||
->andReturn($channel = m::mock(ChannelInterface::class)); | ||
|
||
$this->assertSame( | ||
$channel, | ||
$this->manager->getChannel('firebase') | ||
); | ||
} | ||
|
||
public function testGetsNotificationChannelWithMultipleTransport(): void | ||
{ | ||
$this->factory->shouldReceive('make')->once() | ||
->withArgs(static function (string $type, array $args): bool { | ||
return $type === 'firebase_multiple' | ||
&& $args['transport'] instanceof RoundRobinTransport; | ||
}) | ||
->andReturn($channel = m::mock(ChannelInterface::class)); | ||
|
||
$this->assertSame( | ||
$channel, | ||
$this->manager->getChannel('firebase_multiple') | ||
); | ||
} | ||
|
||
public function testUnknownTransportTypeShouldThrowAnException(): void | ||
{ | ||
$this->expectException(UnsupportedSchemeException::class); | ||
$this->expectErrorMessage('The "foo" scheme is not supported.'); | ||
|
||
$this->manager->getChannel('unknown'); | ||
} | ||
|
||
public function testUnsupportedTransportTypeShouldThrowAnException(): void | ||
{ | ||
$this->expectException(UnsupportedSchemeException::class); | ||
$this->expectErrorMessage('Unable to send notification via "discord" as the bridge is not installed; try running "composer require symfony/discord-notifier".'); | ||
|
||
$this->manager->getChannel('unsupported'); | ||
} | ||
} |
Oops, something went wrong.