Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickobedgiu1 committed Mar 18, 2024
1 parent b4351e7 commit 51b401c
Show file tree
Hide file tree
Showing 2 changed files with 309 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/Integration/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Netflie\WhatsAppCloudApi\Message\OptionsList\Action;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Row;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Section;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action as MultiProductAction;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Row as MultiProductRow;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Section as MultiProductSection;
use Netflie\WhatsAppCloudApi\Message\Template\Component;
use Netflie\WhatsAppCloudApi\Tests\WhatsAppCloudApiTestConfiguration;
use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
Expand Down Expand Up @@ -525,6 +528,101 @@ public function test_send_call_to_action()
$this->assertEquals(false, $response->isError());
}

public function test_send_location_request()
{
$body = 'Let\'s start with your pickup. You can either manually *enter an address* or *share your current location*.';
$response = $this->whatsapp_app_cloud_api->sendLocationRequest(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$body
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(false, $response->isError());
}

public function test_send_catalog_message()
{
$this->markTestIncomplete(
'This test requires active catalog setting in whatsapp commerce, and the real sku id in catalog.'
);

$body = 'Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items you\'d like to purchase.';
$footer = 'Best grocery deals on WhatsApp!';
$sku_thumbnail = '<product-sku-id>';
$response = $this->whatsapp_app_cloud_api->sendCatalog(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$body,
$footer,
$sku_thumbnail
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(false, $response->isError());
}

public function test_send_single_product()
{
$this->markTestIncomplete(
'This test requires real catalog id and product sku id.'
);

$catalog_id = '<catalog-id>';
$sku_id = '<product-sku-id>';
$body = 'Hello! Here\'s your requested product. Thanks for shopping with us.';
$footer = 'Subject to T&C';

$response = $this->whatsapp_app_cloud_api->sendSingleProduct(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$catalog_id,
$sku_id,
$body, // body: optional
$footer // footer: optional
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(false, $response->isError());
}

public function test_send_multi_product()
{
$this->markTestIncomplete(
'This test requires real catalog data.'
);

$rows_section_1 = [
new MultiProductRow('<product-sku-id>'),
// etc
];

$rows_section_2 = [
new MultiProductRow('<product-sku-id>'),
// etc
];

$sections = [
new MultiProductSection('Section 1', $rows_section_1),
new MultiProductSection('Section 2', $rows_section_2),
];

$action = new MultiProductAction($sections);
$catalog_id = '<catalog-id>';
$header = 'Grocery Collections';
$body = 'Hello! Thanks for your interest. Here\'s what we can offer you under our grocery collection. Thank you for shopping with us.';
$footer = 'Subject to T&C';

$response = $this->whatsapp_app_cloud_api->sendMultiProduct(
WhatsAppCloudApiTestConfiguration::$to_phone_number_id,
$catalog_id,
$action,
$header,
$body,
$footer // optional
);

$this->assertEquals(200, $response->httpStatusCode());
$this->assertEquals(false, $response->isError());
}

public function test_upload_media()
{
$response = $this->whatsapp_app_cloud_api->uploadMedia('tests/Support/netflie.png');
Expand Down
211 changes: 211 additions & 0 deletions tests/Unit/WhatsAppCloudApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use Netflie\WhatsAppCloudApi\Message\OptionsList\Action;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Row;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Section;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action as MultiProductAction;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Row as MultiProductRow;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Section as MultiProductSection;
use Netflie\WhatsAppCloudApi\Message\Template\Component;
use Netflie\WhatsAppCloudApi\Response\ResponseException;
use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
Expand Down Expand Up @@ -1603,6 +1606,214 @@ public function test_send_call_to_action()
$this->assertEquals(false, $response->isError());
}

public function test_send_location_request()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$message = $this->faker->text(1024);

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'location_request_message',
'body' => ['text' => $message],
'action' => [
'name' => 'send_location'
],
],
];
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->postJsonData($url, $body, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$response = $this->whatsapp_app_cloud_api->sendLocationRequest(
$to,
$message
);

$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_catalog_message()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$message = $this->faker->text(1024);
$footer = $this->faker->text(60);
$product_retailer_id = $this->faker->text;

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'catalog_message',
'body' => ['text' => $message],
'footer' => ['text' => $footer],
'action' => [
'name' => 'catalog_message',
'parameters' => ['thumbnail_product_retailer_id' => $product_retailer_id]
],
],
];
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->postJsonData($url, $body, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$response = $this->whatsapp_app_cloud_api->sendCatalog(
$to,
$message,
$footer,
$product_retailer_id
);

$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_single_product()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$message = $this->faker->text(1024);
$footer = $this->faker->text(60);
$catalog_id = $this->faker->randomNumber();
$product_retailer_id = $this->faker->text();

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'product',
'body' => ['text' => $message],
'footer' => ['text' => $footer],
'action' => [
'catalog_id' => $catalog_id,
'product_retailer_id' => $product_retailer_id
],
],
];
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->postJsonData($url, $body, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$response = $this->whatsapp_app_cloud_api->sendSingleProduct(
$to,
$catalog_id,
$product_retailer_id,
$message,
$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_multi_product()
{
$to = $this->faker->phoneNumber;
$url = $this->buildMessageRequestUri();
$header = $this->faker->text(20);
$message = $this->faker->text(1024);
$footer = $this->faker->text(60);
$catalog_id = $this->faker->randomNumber();

$rows1 = [
['product_retailer_id' => $this->faker->uuid],
['product_retailer_id' => $this->faker->uuid],
['product_retailer_id' => $this->faker->uuid],
];

$rows2 = [
['product_retailer_id' => $this->faker->uuid],
['product_retailer_id' => $this->faker->uuid],
];


$sections = [
['title' => $this->faker->text, 'product_items' => $rows1],
['title' => $this->faker->text, 'product_items' => $rows2],
];
$actions = ['catalog_id' => $catalog_id, 'sections' => $sections];

$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $to,
'type' => 'interactive',
'interactive' => [
'type' => 'product_list',
'header' => ['type' => 'text', 'text' => $header],
'body' => ['text' => $message],
'footer' => ['text' => $footer],
'action' => $actions,
],
];

$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];

$this->client_handler
->postJsonData($url, $body, $headers, Argument::type('int'))
->shouldBeCalled()
->willReturn(new RawResponse($headers, $this->successfulMessageNodeResponse(), 200));

$actionSections = [];

foreach ($actions['sections'] as $section) {
$sectionRows = [];

foreach ($section['product_items'] as $row) {
$sectionRows[] = new MultiProductRow($row['product_retailer_id']);
}

$actionSections[] = new MultiProductSection($section['title'], $sectionRows);
}

$response = $this->whatsapp_app_cloud_api->sendMultiProduct(
$to,
$catalog_id,
new MultiProductAction($actionSections),
$header,
$message,
$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_upload_media()
{
$url = $this->buildMediaRequestUri();
Expand Down

0 comments on commit 51b401c

Please sign in to comment.