From f999229a1a08be759f1f47154fc9fe8d7e564f61 Mon Sep 17 00:00:00 2001 From: Nguyen Yustar Date: Fri, 21 Apr 2017 18:41:59 +0700 Subject: [PATCH] Minor update - Reformat code to PSR2 - Refactor code using 'return first' technique - Add tests --- composer.json | 3 + src/juno_okyo/Chatfuel.php | 462 +++++++++++++++++++------------------ tests/ChatfuelTest.php | 263 +++++++++++++++++++++ 3 files changed, 506 insertions(+), 222 deletions(-) create mode 100644 tests/ChatfuelTest.php diff --git a/composer.json b/composer.json index 0cb61ec..2e1705c 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ "psr-4": { "juno_okyo\\": "src/juno_okyo/" } + }, + "require-dev": { + "phpunit/phpunit": "^5.7" } } diff --git a/src/juno_okyo/Chatfuel.php b/src/juno_okyo/Chatfuel.php index 384c864..9b34790 100644 --- a/src/juno_okyo/Chatfuel.php +++ b/src/juno_okyo/Chatfuel.php @@ -1,233 +1,251 @@ * * Website: https://junookyo.blogspot.com/ */ + namespace juno_okyo; class Chatfuel { - const VERSION = '1.0.0'; - - protected $response = array(); - - public function __construct($debug = FALSE) - { - if (( ! $debug) && ( ! isset($_SERVER['HTTP_USER_AGENT']) OR strpos($_SERVER['HTTP_USER_AGENT'], 'Apache-HttpAsyncClient') === FALSE)) { - exit; - } - } - - public function __destruct() - { - if (count($this->response) > 0) { - try { - header('Content-Type: application/json'); - echo json_encode(array('messages' => $this->response)); - exit; - } catch (Exception $e) { - // noop - } - } - } - - public function sendText($messages = null) - { - if (is_null($messages)) { - throw new Exception('Invalid input', 1); - } - - $type = gettype($messages); - if ($type === 'string') { - $this->response[] = array('text' => $messages); - } elseif ($type === 'array' || is_array($messages)) { - foreach ($messages as $message) { - $this->response[] = array('text' => $message); - } - } else { - $this->response[] = array('text' => 'Error!'); - } - } - - public function sendImage($url) - { - if ($this->isURL($url)) { - $this->sendAttachment('image', array('url' => $url)); - } else { - $this->sendText('Error: Invalid URL!'); - } - } - - public function sendVideo($url) - { - if ($this->isURL($url)) { - $this->sendAttachment('video', array('url' => $url)); - } else { - $this->sendText('Error: Invalid URL!'); - } - } - - public function sendAudio($url) - { - if ($this->isURL($url)) { - $this->sendAttachment('audio', array('url' => $url)); - } else { - $this->sendText('Error: Invalid URL!'); - } - } - - public function sendTextCard($text, $buttons) - { - if (is_array($buttons)) { - $this->sendAttachment('template', array( - 'template_type' => 'button', - 'text' => $text, - 'buttons' => $buttons - )); - - return TRUE; - } - - return FALSE; - } - - public function sendGallery($elements) - { - if (is_array($elements)) { - $this->sendAttachment('template', array( - 'template_type' => 'generic', - 'elements' => $elements - )); - - return TRUE; - } - - return FALSE; - } - - public function createElement($title, $image, $subTitle, $buttons) - { - if ($this->isURL($image) && is_array($buttons)) { - return array( - 'title' => $title, - 'image_url' => $image, - 'subtitle' => $subTitle, - 'buttons' => $buttons - ); - } - - return FALSE; - } - - public function createButtonToBlock($title, $block, $setAttributes = NULL) - { - $button = array(); - $button['type'] = 'show_block'; - $button['title'] = $title; - - if (is_array($block)) { - $button['block_names'] = $block; - } else { - $button['block_name'] = $block; - } - - if ( ! is_null($setAttributes) && is_array($setAttributes)) { - $button['set_attributes'] = $setAttributes; - } - - return $button; - } - - public function createButtonToURL($title, $url, $setAttributes = NULL) - { - if ($this->isURL($url)) { - $button = array(); - $button['type'] = 'web_url'; - $button['url'] = $url; - $button['title'] = $title; - - if ( ! is_null($setAttributes) && is_array($setAttributes)) { - $button['set_attributes'] = $setAttributes; - } - - return $button; - } - - return FALSE; - } - - public function createPostBackButton($title, $url) - { - if ($this->isURL($url)) { - return array( - 'url' => $url, - 'type' => 'json_plugin_url', - 'title' => $title - ); - } - - return FALSE; - } - - public function createCallButton($phoneNumber, $title = 'Call') - { - return array( - 'type' => 'phone_number', - 'phone_number' => $phoneNumber, - 'title' => $title - ); - } - - public function createShareButton() - { - return array('type' => 'element_share'); - } - - public function createQuickReply($text, $quickReplies) - { - if (is_array($quickReplies)) { - $this->response['text'] = $text; - $this->response['quick_replies'] = $quickReplies; - return TRUE; - } - - return FALSE; - } - - public function createQuickReplyButton($title, $block) - { - $button = array(); - $button['title'] = $title; - - if (is_array($block)) { - $button['block_names'] = $block; - } else { - $button['block_name'] = $block; - } - - return $button; - } - - private function sendAttachment($type, $payload) - { - $type = strtolower($type); - $validTypes = array('image', 'video', 'audio', 'template'); - - if (in_array($type, $validTypes)) { - $this->response[] = array( - 'attachment' => array( - 'type' => $type, - 'payload' => $payload - ) - ); - } else { - $this->response[] = array('text' => 'Error: Invalid type!'); - } - } - - private function isURL($url) - { - return filter_var($url, FILTER_VALIDATE_URL); - } + const VERSION = '1.0.1'; + + protected $response = array(); + + public function __construct($debug = false) + { + if ((! $debug) && (! isset($_SERVER['HTTP_USER_AGENT']) || strpos($_SERVER['HTTP_USER_AGENT'], 'Apache-HttpAsyncClient') === false)) { + exit; + } + } + + public function __destruct() + { + // never run when execute PHPUnit + if (count($this->response) > 0 && ! $this->isCLI()) { + try { + header('Content-Type: application/json'); + echo json_encode(array('messages' => $this->response)); + + exit; + } catch (\Exception $e) { + // nop + } + } + } + + public function sendText($messages = null) + { + if (is_null($messages)) { + throw new \Exception('Invalid input', 1); + } + + if (! is_string($messages) && ! is_array($messages)) { + return $this->response[] = array('text' => 'Error!'); + } + + if (is_array($messages)) { + $arr = array(); + + foreach ($messages as $message) { + $arr[] = array('text' => $message); + } + + $this->response = array_merge($this->response, $arr); + + return $arr; + } + + return $this->response[] = array('text' => $messages); + } + + public function sendImage($url) + { + if (! $this->isURL($url)) { + return $this->sendText('Error: Invalid URL!'); + } + + return $this->sendAttachment('image', array('url' => $url)); + } + + public function sendVideo($url) + { + if (! $this->isURL($url)) { + return $this->sendText('Error: Invalid URL!'); + } + + return $this->sendAttachment('video', array('url' => $url)); + } + + public function sendAudio($url) + { + if (! $this->isURL($url)) { + return $this->sendText('Error: Invalid URL!'); + } + + return $this->sendAttachment('audio', array('url' => $url)); + } + + public function sendTextCard($text, $buttons) + { + if (! is_array($buttons)) { + return; + } + + return $this->sendAttachment('template', array( + 'template_type' => 'button', + 'text' => $text, + 'buttons' => $buttons + )); + } + + public function sendGallery($elements) + { + if (! is_array($elements)) { + return; + } + + return $this->sendAttachment('template', array( + 'template_type' => 'generic', + 'elements' => $elements + )); + } + + public function createElement($title, $image, $subTitle, $buttons) + { + if (! $this->isURL($image) || ! is_array($buttons)) { + return; + } + + return array( + 'title' => $title, + 'image_url' => $image, + 'subtitle' => $subTitle, + 'buttons' => $buttons + ); + } + + public function createButtonToBlock($title, $block, $setAttributes = null) + { + $button = array(); + $button['type'] = 'show_block'; + $button['title'] = $title; + + if (is_array($block)) { + $button['block_names'] = $block; + } + + if (is_string($block)) { + $button['block_name'] = $block; + } + + if (! is_null($setAttributes) && is_array($setAttributes)) { + $button['set_attributes'] = $setAttributes; + } + + return $button; + } + + public function createButtonToURL($title, $url, $setAttributes = null) + { + if (! $this->isURL($url)) { + return; + } + + $button = array(); + $button['type'] = 'web_url'; + $button['url'] = $url; + $button['title'] = $title; + + if (! is_null($setAttributes) && is_array($setAttributes)) { + $button['set_attributes'] = $setAttributes; + } + + return $button; + } + + public function createPostBackButton($title, $url) + { + if (! $this->isURL($url)) { + return; + } + + return array( + 'url' => $url, + 'type' => 'json_plugin_url', + 'title' => $title + ); + } + + public function createCallButton($phoneNumber, $title = 'Call') + { + return array( + 'type' => 'phone_number', + 'phone_number' => $phoneNumber, + 'title' => $title + ); + } + + public function createShareButton() + { + return array('type' => 'element_share'); + } + + public function createQuickReply($text, $quickReplies) + { + if (! is_array($quickReplies)) { + return; + } + + $arr = array('text' => $text, 'quick_replies' => $quickReplies); + + $this->response = array_merge($this->response, $arr); + + return $arr; + } + + public function createQuickReplyButton($title, $block) + { + $button = array(); + $button['title'] = $title; + + if (is_array($block)) { + $button['block_names'] = $block; + } + + if (is_string($block)) { + $button['block_name'] = $block; + } + + return $button; + } + + private function sendAttachment($type, $payload) + { + $type = strtolower($type); + $validTypes = array('image', 'video', 'audio', 'template'); + + if (! in_array($type, $validTypes)) { + return $this->response[] = array('text' => 'Error: Invalid type!'); + } + + return $this->response[] = array( + 'attachment' => array( + 'type' => $type, + 'payload' => $payload + ) + ); + } + + private function isCLI() + { + return php_sapi_name() === 'cli'; + } + + private function isURL($url) + { + return filter_var($url, FILTER_VALIDATE_URL); + } } diff --git a/tests/ChatfuelTest.php b/tests/ChatfuelTest.php new file mode 100644 index 0000000..9bb8e29 --- /dev/null +++ b/tests/ChatfuelTest.php @@ -0,0 +1,263 @@ +chatfuel = new Chatfuel(true); + } + + public function testSendTextException() + { + $this->expectException(\Exception::class); + + $this->chatfuel->sendText(); + } + + public function testSendTextError() + { + $this->assertSame( + ['text' => 'Error!'], + $this->chatfuel->sendText(1) + ); + } + + public function testSendText() + { + $this->assertSame( + ['text' => 'test'], + $this->chatfuel->sendText('test') + ); + $this->assertSame( + [['text' => 'test 1'], ['text' => 'test 2']], + $this->chatfuel->sendText(['test 1', 'test 2']) + ); + } + + public function testSendAttachmentError() + { + $url = 'this-is-an-invalid-url.test.org/data.ext'; + + $this->assertSame( + ['text' => 'Error: Invalid URL!'], + $this->chatfuel->sendImage($url) + ); + $this->assertSame( + ['text' => 'Error: Invalid URL!'], + $this->chatfuel->sendAudio($url) + ); + $this->assertSame( + ['text' => 'Error: Invalid URL!'], + $this->chatfuel->sendVideo($url) + ); + $this->assertNull( + $this->chatfuel->sendTextCard('test', null) + ); + $this->assertNull( + $this->chatfuel->sendGallery(null) + ); + } + + public function testSendAttachment() + { + $url = 'http://this-is-a-valid-url.test.org/data.ext'; + + $this->assertSame( + ['attachment' => ['type' => 'image', 'payload' => ['url' => $url]]], + $this->chatfuel->sendImage($url) + ); + $this->assertSame( + ['attachment' => ['type' => 'audio', 'payload' => ['url' => $url]]], + $this->chatfuel->sendAudio($url) + ); + $this->assertSame( + ['attachment' => ['type' => 'video', 'payload' => ['url' => $url]]], + $this->chatfuel->sendVideo($url) + ); + $this->assertSame( + ['attachment' => ['type' => 'template', 'payload' => [ + 'template_type' => 'button', + 'text' => 'test', + 'buttons' => [] + ]]], + $this->chatfuel->sendTextCard('test', []) + ); + $this->assertSame( + ['attachment' => ['type' => 'template', 'payload' => [ + 'template_type' => 'generic', + 'elements' => [] + ]]], + $this->chatfuel->sendGallery([]) + ); + } + + public function testCreateElementError() + { + $invalidUrl = 'this-is-an-invalid-url.test.org/data.ext'; + $validUrl = 'http://this-is-a-valid-url.test.org/data.ext'; + + $this->assertNull( + $this->chatfuel->createElement('test title', $invalidUrl, 'test subtitle', []) + ); + $this->assertNull( + $this->chatfuel->createElement('test title', $validUrl, 'test subtitle', null) + ); + $this->assertNull( + $this->chatfuel->createElement('test title', $invalidUrl, 'test subtitle', null) + ); + } + + public function testCreateElement() + { + $url = 'http://this-is-a-valid-url.test.org/data.ext'; + + $this->assertSame( + [ + 'title' => 'test', + 'image_url' => $url, + 'subtitle' => 'test subtitle', + 'buttons' => [] + ], + $this->chatfuel->createElement('test', $url, 'test subtitle', []) + ); + } + + public function testCreateElementButtonError() + { + $url = 'this-is-an-invalid-url.test.org/data.ext'; + + $this->assertNull( + $this->chatfuel->createButtonToURL('test', $url) + ); + + $this->assertNull( + $this->chatfuel->createPostBackButton('test', $url) + ); + } + + public function testCreateElementButton() + { + $title = 'test'; + $url = 'http://this-is-a-valid-url.test.org/data.ext'; + + $this->assertSame( + [ + 'type' => 'show_block', + 'title' => $title, + 'block_names' => [] + ], + $this->chatfuel->createButtonToBlock($title, []) + ); + $this->assertSame( + [ + 'type' => 'show_block', + 'title' => $title, + 'block_name' => 'block' + ], + $this->chatfuel->createButtonToBlock($title, 'block') + ); + $this->assertSame( + [ + 'type' => 'show_block', + 'title' => $title, + 'block_names' => [], + 'set_attributes' => [] + ], + $this->chatfuel->createButtonToBlock($title, [], []) + ); + + $this->assertSame( + [ + 'type' => 'web_url', + 'url' => $url, + 'title' => $title, + ], + $this->chatfuel->createButtonToURL($title, $url) + ); + $this->assertSame( + [ + 'type' => 'web_url', + 'url' => $url, + 'title' => $title, + 'set_attributes' => [] + ], + $this->chatfuel->createButtonToURL($title, $url, []) + ); + + $this->assertSame( + [ + 'url' => $url, + 'type' => 'json_plugin_url', + 'title' => $title + ], + $this->chatfuel->createPostBackButton($title, $url) + ); + + $this->assertSame( + [ + 'type' => 'phone_number', + 'phone_number' => 01234567890, + 'title' => 'Call' + ], + $this->chatfuel->createCallButton(01234567890) + ); + $this->assertSame( + [ + 'type' => 'phone_number', + 'phone_number' => 01234567890, + 'title' => $title + ], + $this->chatfuel->createCallButton(01234567890, $title) + ); + + $this->assertSame( + ['type' => 'element_share'], + $this->chatfuel->createShareButton() + ); + } + + public function testCreateQuickReplyError() + { + $this->assertNull( + $this->chatfuel->createQuickReply('test', null) + ); + } + + public function testCreateQuickReply() + { + $this->assertSame( + [ + 'text' => 'test', + 'quick_replies' => [] + ], + $this->chatfuel->createQuickReply('test', []) + ); + } + + public function testCreateQuickReplyButton() + { + $title = 'test'; + + $this->assertSame( + [ + 'title' => $title, + 'block_names' => [] + ], + $this->chatfuel->createQuickReplyButton($title, []) + ); + $this->assertSame( + [ + 'title' => $title, + 'block_name' => 'block' + ], + $this->chatfuel->createQuickReplyButton($title, 'block') + ); + } +}