|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NotificationChannels\WhatsApp\Test; |
| 4 | + |
| 5 | +use Illuminate\Notifications\Notifiable; |
| 6 | +use Illuminate\Notifications\Notification; |
| 7 | +use Netflie\WhatsAppCloudApi\Http\ClientHandler; |
| 8 | +use Netflie\WhatsAppCloudApi\Http\RawResponse; |
| 9 | +use Netflie\WhatsAppCloudApi\WhatsAppCloudApi; |
| 10 | +use NotificationChannels\WhatsApp\Exceptions\CouldNotSendNotification; |
| 11 | +use NotificationChannels\WhatsApp\Test\Support\DummyNotifiable; |
| 12 | +use NotificationChannels\WhatsApp\Test\Support\DummyNotification; |
| 13 | +use NotificationChannels\WhatsApp\Test\Support\DummyNotificationWithoutRecipient; |
| 14 | +use NotificationChannels\WhatsApp\WhatsAppChannel; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +final class WhatsAppChannelTest extends TestCase |
| 18 | +{ |
| 19 | + private ClientHandler $httpClient; |
| 20 | + |
| 21 | + private WhatsAppCloudApi $whatsapp; |
| 22 | + |
| 23 | + private WhatsAppChannel $channel; |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->httpClient = \Mockery::mock(ClientHandler::class); |
| 30 | + |
| 31 | + $this->whatsapp = new WhatsAppCloudApi([ |
| 32 | + 'from_phone_number_id' => '34676202545', |
| 33 | + 'access_token' => 'super-secret', |
| 34 | + 'client_handler' => $this->httpClient, |
| 35 | + ]); |
| 36 | + $this->channel = new WhatsAppChannel($this->whatsapp); |
| 37 | + } |
| 38 | + |
| 39 | + /** @test */ |
| 40 | + public function it_can_send_a_notification() |
| 41 | + { |
| 42 | + $notifiable = $this->getMockForTrait(Notifiable::class); |
| 43 | + $notification = new DummyNotification(); |
| 44 | + |
| 45 | + $body = [ |
| 46 | + 'messaging_product' => 'whatsapp', |
| 47 | + 'recipient_type' => 'individual', |
| 48 | + 'to' => $notification->toWhatsApp($notifiable)->recipient(), |
| 49 | + 'type' => 'template', |
| 50 | + 'template' => [ |
| 51 | + 'name' => $notification->toWhatsApp($notifiable)->configuredName(), |
| 52 | + 'language' => ['code' => $notification->toWhatsApp($notifiable)->configuredLanguage()], |
| 53 | + 'components' => [], |
| 54 | + ], |
| 55 | + ]; |
| 56 | + $headers = [ |
| 57 | + 'Authorization' => 'Bearer super-secret', |
| 58 | + 'Content-Type' => 'application/json', |
| 59 | + ]; |
| 60 | + $expectedResponse = new RawResponse($headers, json_encode($body), 200); |
| 61 | + |
| 62 | + $response = $this->sendMockNotification($notifiable, $notification, $expectedResponse); |
| 63 | + |
| 64 | + $this->assertEquals(json_encode($body), $response->body()); |
| 65 | + $this->assertEquals(200, $response->httpStatusCode()); |
| 66 | + $this->assertNotEmpty($notification->toWhatsApp($notifiable)->recipient()); |
| 67 | + $this->assertNotEmpty($notification->toWhatsApp($notifiable)->configuredName()); |
| 68 | + $this->assertNotEmpty($notification->toWhatsApp($notifiable)->configuredLanguage()); |
| 69 | + } |
| 70 | + |
| 71 | + /** @test */ |
| 72 | + public function it_can_send_a_notification_if_notifiable_provide_a_recipient_from_route() |
| 73 | + { |
| 74 | + $notifiable = new DummyNotifiable(); |
| 75 | + $notification = new DummyNotificationWithoutRecipient(); |
| 76 | + |
| 77 | + $body = [ |
| 78 | + 'messaging_product' => 'whatsapp', |
| 79 | + 'recipient_type' => 'individual', |
| 80 | + 'to' => $notifiable->routeNotificationForWhatsApp(), |
| 81 | + 'type' => 'template', |
| 82 | + 'template' => [ |
| 83 | + 'name' => $notification->toWhatsApp($notifiable)->configuredName(), |
| 84 | + 'language' => ['code' => $notification->toWhatsApp($notifiable)->configuredLanguage()], |
| 85 | + 'components' => [], |
| 86 | + ], |
| 87 | + ]; |
| 88 | + $headers = [ |
| 89 | + 'Authorization' => 'Bearer super-secret', |
| 90 | + 'Content-Type' => 'application/json', |
| 91 | + ]; |
| 92 | + $expectedResponse = new RawResponse($headers, json_encode($body), 200); |
| 93 | + |
| 94 | + $response = $this->sendMockNotification($notifiable, $notification, $expectedResponse); |
| 95 | + |
| 96 | + $this->assertEquals(json_encode($body), $response->body()); |
| 97 | + $this->assertEquals(200, $response->httpStatusCode()); |
| 98 | + $this->assertNotEmpty($body['to']); |
| 99 | + $this->assertNotEmpty($notification->toWhatsApp($notifiable)->configuredName()); |
| 100 | + $this->assertNotEmpty($notification->toWhatsApp($notifiable)->configuredLanguage()); |
| 101 | + } |
| 102 | + |
| 103 | + /** @test */ |
| 104 | + public function it_does_not_send_a_notification_if_the_notifiable_does_not_provide_a_recipient() |
| 105 | + { |
| 106 | + $notifiable = $this->getMockForTrait(Notifiable::class); |
| 107 | + $notification = new DummyNotificationWithoutRecipient(); |
| 108 | + |
| 109 | + $httpClient = \Mockery::mock(ClientHandler::class); |
| 110 | + $httpClient->shouldNotHaveReceived('send'); |
| 111 | + |
| 112 | + $response = $this->channel->send($notifiable, $notification); |
| 113 | + |
| 114 | + $this->assertNull($response); |
| 115 | + } |
| 116 | + |
| 117 | + /** @test */ |
| 118 | + public function send_notification_failed() |
| 119 | + { |
| 120 | + $notifiable = $this->getMockForTrait(Notifiable::class); |
| 121 | + $notification = new DummyNotification(); |
| 122 | + $expectedResponse = new RawResponse([], json_encode(['error' => true]), 500); |
| 123 | + |
| 124 | + $this->expectException(CouldNotSendNotification::class); |
| 125 | + $response = $this->sendMockNotification($notifiable, $notification, $expectedResponse); |
| 126 | + |
| 127 | + $this->assertNull($response); |
| 128 | + } |
| 129 | + |
| 130 | + private function sendMockNotification( |
| 131 | + $notifiable, |
| 132 | + Notification $notification, |
| 133 | + RawResponse $expectedResponse |
| 134 | + ) { |
| 135 | + $this->httpClient |
| 136 | + ->shouldReceive('send') |
| 137 | + ->once() |
| 138 | + ->andReturns($expectedResponse); |
| 139 | + |
| 140 | + return $this->channel->send($notifiable, $notification); |
| 141 | + } |
| 142 | +} |
0 commit comments