From 3493c6a45f29749f118001690b8b209888f89067 Mon Sep 17 00:00:00 2001 From: Derrick Obedgiu Date: Wed, 30 Oct 2024 17:26:12 +0300 Subject: [PATCH] Added tests --- tests/Integration/WhatsAppCloudApiTest.php | 161 +++++++- tests/Unit/WhatsAppCloudApiTest.php | 458 ++++++++++++++++++++- 2 files changed, 614 insertions(+), 5 deletions(-) diff --git a/tests/Integration/WhatsAppCloudApiTest.php b/tests/Integration/WhatsAppCloudApiTest.php index ea7bb1a..bc4fb4c 100644 --- a/tests/Integration/WhatsAppCloudApiTest.php +++ b/tests/Integration/WhatsAppCloudApiTest.php @@ -4,6 +4,10 @@ use Netflie\WhatsAppCloudApi\Message\ButtonReply\Button; use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\DocumentHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\ImageHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\TextHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\VideoHeader; use Netflie\WhatsAppCloudApi\Message\Contact\ContactName; use Netflie\WhatsAppCloudApi\Message\Contact\Phone; use Netflie\WhatsAppCloudApi\Message\Contact\PhoneType; @@ -347,7 +351,7 @@ public function test_send_multi_product() $this->assertEquals(false, $response->isError()); } - public function test_send_reply_buttons() + public function test_send_reply_buttons_with_text_header() { $buttonRows = [ new Button('button-1', 'Yes'), @@ -355,7 +359,7 @@ public function test_send_reply_buttons() new Button('button-3', 'Not Now'), ]; $buttonAction = new ButtonAction($buttonRows); - $header = 'RATE US'; + $header = new TextHeader('RATE US'); $footer = 'Please choose an option'; $response = $this->whatsapp_app_cloud_api->sendButton( @@ -370,6 +374,159 @@ public function test_send_reply_buttons() $this->assertEquals(false, $response->isError()); } + public function test_send_reply_buttons_with_image_id_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $response = $this->whatsapp_app_cloud_api->uploadMedia('tests/Support/netflie.png'); + $media_id = new MediaObjectID($response->decodedBody()['id']); + $header = new ImageHeader($media_id); + $footer = 'This is the footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the image in the header above was sent using its upload id', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_image_link_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $link_id = new LinkID('https://netflie.es/wp-content/uploads/2022/05/whatsapp_cloud_api_banner-1.png'); + $header = new ImageHeader($link_id); + $footer = 'This is an optional footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the image in the header above was sent using a direct image url', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_video_id_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $response = $this->whatsapp_app_cloud_api->uploadMedia('tests/Support/sample.mp4'); + $media_id = new MediaObjectID($response->decodedBody()['id']); + $header = new VideoHeader($media_id); + $footer = 'This is the footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the video in the header above was sent using its upload id', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_video_link_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $link_id = new LinkID('https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4'); + $header = new VideoHeader($link_id); + $footer = 'This is an optional footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the video in the header above was sent using a direct video url', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_document_id_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $response = $this->whatsapp_app_cloud_api->uploadMedia('tests/Support/sample.pdf'); + $media_id = new MediaObjectID($response->decodedBody()['id']); + $header = new DocumentHeader($media_id, 'whatsapp-cloud-api.pdf'); + $footer = 'This is the footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the document in the header above was sent using its upload id', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_document_link_header() + { + $buttonRows = [ + new Button('button-1', 'Button 1'), + new Button('button-2', 'Button 2'), + new Button('button-3', 'Button 3'), + ]; + $buttonAction = new ButtonAction($buttonRows); + + $link_id = new LinkID('https://netflie.es/wp-content/uploads/2022/05/image.png'); + $header = new DocumentHeader($link_id,'whatsapp-cloud-api.png'); + $footer = 'This is an optional footer'; + + $response = $this->whatsapp_app_cloud_api->sendButton( + WhatsAppCloudApiTestConfiguration::$to_phone_number_id, + 'This is a sample body, but the document in the header above was sent using its direct url', + $buttonAction, + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(false, $response->isError()); + } + public function test_send_reaction_message() { $textMessage = $this->whatsapp_app_cloud_api->sendTextMessage( diff --git a/tests/Unit/WhatsAppCloudApiTest.php b/tests/Unit/WhatsAppCloudApiTest.php index 5d777e2..ad9e68f 100644 --- a/tests/Unit/WhatsAppCloudApiTest.php +++ b/tests/Unit/WhatsAppCloudApiTest.php @@ -8,6 +8,10 @@ use Netflie\WhatsAppCloudApi\Http\RawResponse; use Netflie\WhatsAppCloudApi\Message\ButtonReply\Button; use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\DocumentHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\ImageHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\TextHeader; +use Netflie\WhatsAppCloudApi\Message\ButtonReply\VideoHeader; use Netflie\WhatsAppCloudApi\Message\Contact\ContactName; use Netflie\WhatsAppCloudApi\Message\Contact\Phone; use Netflie\WhatsAppCloudApi\Message\Contact\PhoneType; @@ -1003,7 +1007,7 @@ public function test_send_cta_url() $this->assertEquals(false, $response->isError()); } - public function test_send_reply_buttons() + public function test_send_reply_buttons_with_text_header() { $to = $this->faker->phoneNumber; $url = $this->buildMessageRequestUri(); @@ -1024,7 +1028,7 @@ public function test_send_reply_buttons() } $message = $this->faker->text(50); - $header = $this->faker->text(50); + $header = ['type' => 'text', 'text' => $this->faker->text(50)]; $footer = $this->faker->text(50); $body = [ @@ -1036,7 +1040,302 @@ public function test_send_reply_buttons() 'type' => 'button', 'body' => ['text' => $message], 'action' => $buttonAction, - 'header' => ['type' => 'text', 'text' => $header], + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $header = new TextHeader($header['text']); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_image_id_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $header = ['type' => 'image', 'image' => ['id' => $this->faker->uuid()]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $media_id = new MediaObjectID($header['image']['id']); + $header = new ImageHeader($media_id); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_image_link_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $header = ['type' => 'image', 'image' => ['link' => $this->faker->url]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $link_id = new LinkID($header['image']['link']); + $header = new ImageHeader($link_id); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_video_id_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $header = ['type' => 'video', 'video' => ['id' => $this->faker->uuid()]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $media_id = new MediaObjectID($header['video']['id']); + $header = new VideoHeader($media_id); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_video_link_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $header = ['type' => 'video', 'video' => ['link' => $this->faker->url]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, 'footer' => ['text' => $footer], ], 'context' => [ @@ -1058,6 +1357,159 @@ public function test_send_reply_buttons() $actionButtons[] = new Button($button['id'], $button['title']); } + $link_id = new LinkID($header['video']['link']); + $header = new VideoHeader($link_id); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_document_id_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $filename = $this->faker->text; + $header = ['type' => 'document', 'document' => ['id' => $this->faker->uuid(),'filename' => $filename]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $media_id = new MediaObjectID($header['document']['id']); + $header = new DocumentHeader($media_id, $header['document']['filename']); + + $response = $this->whatsapp_app_cloud_api + ->replyTo($reply_to) + ->sendButton( + $to, + $message, + new ButtonAction($actionButtons), + $header, + $footer + ); + + $this->assertEquals(200, $response->httpStatusCode()); + $this->assertEquals(json_decode($this->successfulMessageNodeResponse(), true), $response->decodedBody()); + $this->assertEquals($this->successfulMessageNodeResponse(), $response->body()); + $this->assertEquals(false, $response->isError()); + } + + public function test_send_reply_buttons_with_document_link_header() + { + $to = $this->faker->phoneNumber; + $url = $this->buildMessageRequestUri(); + $reply_to = $this->faker->uuid; + + $buttonRows = [ + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ['id' => $this->faker->uuid, 'title' => $this->faker->text(10)], + ]; + $buttonAction = ['buttons' => []]; + + foreach ($buttonRows as $button) { + $buttonAction['buttons'][] = [ + 'type' => 'reply', + 'reply' => $button, + ]; + } + + $message = $this->faker->text(50); + $filename = $this->faker->text; + $header = ['type' => 'document', 'document' => ['link' => $this->faker->url,'filename' => $filename]]; + $footer = $this->faker->text(50); + + $body = [ + 'messaging_product' => 'whatsapp', + 'recipient_type' => 'individual', + 'to' => $to, + 'type' => 'interactive', + 'interactive' => [ + 'type' => 'button', + 'body' => ['text' => $message], + 'action' => $buttonAction, + 'header' => $header, + 'footer' => ['text' => $footer], + ], + 'context' => [ + 'message_id' => $reply_to, + ], + ]; + $headers = [ + 'Authorization' => 'Bearer ' . $this->access_token, + ]; + + $this->client_handler + ->postJsonData($url, $body, $headers, Argument::type('int')) + ->shouldBeCalled() + ->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200)); + + $actionButtons = []; + + foreach ($buttonRows as $button) { + $actionButtons[] = new Button($button['id'], $button['title']); + } + + $link_id = new LinkID($header['document']['link']); + $header = new DocumentHeader($link_id, $header['document']['filename']); + $response = $this->whatsapp_app_cloud_api ->replyTo($reply_to) ->sendButton(