Notification example

'icon_emoji' => $emoji,
                'blocks' => [
                    [
                        'type' => 'section',
                        'text' => [
                            'type' => 'mrkdwn',
                            'text' => $subject,
                        ],
                    ],
                ],
            ],
            (new Notification($subject))->emoji($emoji),
        ];

        yield 'with content + without exception' => [
            [
                'icon_emoji' => $emoji,
                'blocks' => [
                    [
                        'type' => 'section',
                        'text' => [
                            'type' => 'mrkdwn',
                            'text' => $subject,
                        ],

        };

        $container['uniqueid.persister'] = function D$c) {
            return new UniqueIdPersister(
                $c['uniqueid.generator'],
                $c['db']
            );
        };

        $container['shopware.notify'] = function D$c) {
            return new Notification(
                $c['config']['api.endpoint'],
                $c['uniqueid.generator']->getUniqueId(),
                $c['http-client']
            );
        };

        $container['anti.xss'] = function D$c) {
            return new AntiXSS();
        };
    }
}
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;

final class NotificationController
{
    public function indexAction(NotifierInterface $notifier)
    {
        $firstNotification = new Notification('Hello World!', ['chat/slack']);
        $firstNotification->content('Symfony is awesome!');

        $notifier->send($firstNotification);

        $secondNotification = (new Notification('New urgent notification'))
            ->importance(Notification::IMPORTANCE_URGENT)
        ;
        $notifier->send($secondNotification);

        return new Response();
    }
}
public function testSetTransport()
    {
        $message = new PushMessage('Hello', 'World');
        $message->transport('next_one');

        $this->assertSame('next_one', $message->getTransport());
    }

    public function testCreateFromNotification()
    {
        $notification = new Notification('Hello');
        $notification->content('World');

        $message = PushMessage::fromNotification($notification);

        $this->assertSame('Hello', $message->getSubject());
        $this->assertSame('World', $message->getContent());
        $this->assertSame($notification$message->getNotification());
    }
}
/** * @author Jan Schädlich <jan.schaedlich@sensiolabs.de> */
class EmailMessageTest extends TestCase
{
    public function testEnsureNonEmptyEmailOnCreationFromNotification()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('"Symfony\Component\Notifier\Message\EmailMessage" needs an email, it cannot be empty.');

        EmailMessage::fromNotification(new Notification()new Recipient('', '+3312345678'));
    }
}
$response = $this->createMock(ResponseInterface::class);

        $response->expects($this->exactly(2))
            ->method('getStatusCode')
            ->willReturn(200);

        $response->expects($this->once())
            ->method('getContent')
            ->willReturn(json_encode(['status' => 1, 'request' => 'uuid']));

        $notification = (new Notification($messageSubject))->content($messageContent);
        $pushMessage = PushMessage::fromNotification($notification);

        $expectedBody = http_build_query([
            'message' => 'testMessageContent',
            'title' => 'testMessageSubject',
            'token' => 'appToken',
            'user' => 'userKey',
        ]);

        $client = new MockHttpClient(function Dstring $method, string $url, array $options = []) use (
            $response,
            
$response = $this->createMock(ResponseInterface::class);

        $response->expects($this->exactly(2))
            ->method('getStatusCode')
            ->willReturn(201);

        $response->expects($this->once())
            ->method('getContent')
            ->willReturn(json_encode(['id' => '42']));

        $notification = new Notification($message);
        $chatMessage = ChatMessage::fromNotification($notification);

        $expectedBody = json_encode([
            'specificContent' => [
                'com.linkedin.ugc.ShareContent' => [
                    'shareCommentary' => [
                        'attributes' => [],
                        'text' => 'testMessage',
                    ],
                    'shareMediaCategory' => 'NONE',
                ],
            ],

        $response = $this->createMock(ResponseInterface::class);

        $response->expects($this->exactly(2))
            ->method('getStatusCode')
            ->willReturn(200);

        $response->expects($this->once())
            ->method('getContent')
            ->willReturn('{"name":"spaces/My-Space/messages/abcdefg.hijklmno","thread":{"name":"spaces/My-Space/threads/abcdefg.hijklmno"}}');

        $notification = new Notification('testMessage');
        $chatMessage = ChatMessage::fromNotification($notification);

        $expectedBody = json_encode([
            'text' => ' *testMessage* ',
        ]);

        $client = new MockHttpClient(function Dstring $method, string $url, array $options = []) use ($response$expectedBody): ResponseInterface {
            $this->assertSame($expectedBody$options['body']);

            return $response;
        });

        
$response = $this->createMock(ResponseInterface::class);

        $response->expects($this->exactly(2))
            ->method('getStatusCode')
            ->willReturn(200);

        $response->expects($this->once())
            ->method('getContent')
            ->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247', 'channel' => 'C123456']));

        $notification = new Notification($message);
        $chatMessage = ChatMessage::fromNotification($notification);
        $options = SlackOptions::fromNotification($notification);

        $expectedBody = json_encode([
            'blocks' => $options->toArray()['blocks'],
            'channel' => $channel,
            'text' => $message,
        ]);

        $client = new MockHttpClient(function Dstring $method, string $url, array $options = []) use ($response$expectedBody): ResponseInterface {
            $this->assertJsonStringEqualsJsonString($expectedBody$options['body']);

            
return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]);
        });

        $transport = self::createTransport($client);

        $transport->send(new ChatMessage($message$options));
    }

    public function testSendFromNotification()
    {
        $notification = new Notification($message = 'testMessage');
        $chatMessage = ChatMessage::fromNotification($notification);

        $expectedBody = json_encode([
            'text' => $message,
        ]);

        $client = new MockHttpClient(function Dstring $method, string $url, array $options = []) use ($expectedBody): ResponseInterface {
            $this->assertJsonStringEqualsJsonString($expectedBody$options['body']);

            return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]);
        });

        
if ($records = array_filter($records$this->isHandling(...))) {
            $this->notify($records);
        }
    }

    private function notify(array $records): void
    {
        $record = $this->getHighestRecord($records);
        if (($record['context']['exception'] ?? null) instanceof \Throwable) {
            $notification = Notification::fromThrowable($record['context']['exception']);
        } else {
            $notification = new Notification($record['message']);
        }

        $notification->importanceFromLogLevelName(Logger::getLevelName($record['level']));

        $this->notifier->send($notification, ...$this->notifier->getAdminRecipients());
    }

    private function getHighestRecord(array $records): array|LogRecord
    {
        $highestRecord = null;
        foreach ($records as $record) {
            
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Notification\Notification;

final class MobytOptionsTest extends TestCase
{
    /** * @dataProvider fromNotificationDataProvider */
    public function testFromNotification(string $importance, string $expectedMessageType)
    {
        $notification = (new Notification('Foo'))->importance($importance);

        $options = MobytOptions::fromNotification($notification)->toArray();

        $this->assertSame($expectedMessageType$options['message_type']);
    }

    /** * @return \Generator<array{0: string, 1: string}> */
    public static function fromNotificationDataProvider(): \Generator
    {
        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\Action\OpenUriAction;
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsOptions;
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\Section\Section;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Notification\Notification;

final class MicrosoftTeamsOptionsTest extends TestCase
{
    public function testFromNotification()
    {
        $notification = (new Notification($subject = 'Subject'))
            ->content($content = 'Content');

        $this->assertSame(
            [
                'title' => $subject,
                'text' => $content,
                '@type' => 'MessageCard',
                '@context' => 'https://schema.org/extensions',
            ],
            MicrosoftTeamsOptions::fromNotification($notification)->toArray()
        );
    }
/** * @author Smaïne Milianni <smaine.milianni@gmail.com> */
final class NotifierTest extends TestCase
{
    public function testItThrowAnExplicitErrorIfAnSmsChannelDoesNotHaveRecipient()
    {
        $this->expectException(LogicException::class);
        $this->expectExceptionMessage('The "sms" channel needs a Recipient.');

        $notifier = new Notifier(['sms' => new SmsChannel(new NullTransport())]);
        $notifier->send(new Notification('Hello World!', ['sms/twilio']));
    }
}
/** * @dataProvider defaultFlashMessageImportanceDataProvider */
    public function testImportanceLevelIsReflectedInFlashMessageType(
        FlashMessageImportanceMapperInterface $mapper,
        string $importance,
        string $expectedFlashMessageType
    ) {
        $session = $this->createMock(Session::class);
        $session->method('getFlashBag')->willReturn(new FlashBag());
        $browserChannel = $this->buildBrowserChannel($session$mapper);
        $notification = new Notification();
        $notification->importance($importance);
        $recipient = new Recipient('hello@example.com');

        $browserChannel->notify($notification$recipient);

        $this->assertEquals($expectedFlashMessageTypearray_key_first($session->getFlashBag()->all()));
    }

    public function testUnknownImportanceMappingIsReported()
    {
        $session = $this->createMock(Session::class);
        
Home | Imprint | This part of the site doesn't use cookies.