From 093a393a1b42d234a6c837243d1da090ecfd6fe5 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Mon, 9 Oct 2017 21:13:01 +0300 Subject: [PATCH 01/14] Changed method names --- src/Testing/BotManTester.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index cf20b28..7af5896 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -96,7 +96,7 @@ public function setUser($user_info) * @param IncomingMessage $message * @return $this */ - public function receivesRaw($message) + public function receivesIncomingMessage($message) { $this->driver->messages = [$message]; @@ -114,7 +114,7 @@ public function receivesRaw($message) */ public function receives($message) { - return $this->receivesRaw(new IncomingMessage($message, $this->user_id, $this->channel)); + return $this->receivesIncomingMessage(new IncomingMessage($message, $this->user_id, $this->channel)); } /** @@ -138,7 +138,7 @@ public function receivesLocation($latitude = 24, $longitude = 57) $message = new IncomingMessage(Location::PATTERN, $this->user_id, $this->channel); $message->setLocation(new Location($latitude, $longitude, null)); - return $this->receivesRaw($message); + return $this->receivesIncomingMessage($message); } /** @@ -157,7 +157,7 @@ public function receivesImages(array $urls = null) $message = new IncomingMessage(Image::PATTERN, $this->user_id, $this->channel); $message->setImages($images); - return $this->receivesRaw($message); + return $this->receivesIncomingMessage($message); } /** @@ -177,7 +177,7 @@ public function receivesAudio(array $urls = null) $message = new IncomingMessage(Audio::PATTERN, $this->user_id, $this->channel); $message->setAudio($audio); - return $this->receivesRaw($message); + return $this->receivesIncomingMessage($message); } /** @@ -196,7 +196,7 @@ public function receivesVideos(array $urls = null) $message = new IncomingMessage(Video::PATTERN, $this->user_id, $this->channel); $message->setVideos($videos); - return $this->receivesRaw($message); + return $this->receivesIncomingMessage($message); } /** @@ -215,7 +215,7 @@ public function receivesFiles(array $urls = null) $message = new IncomingMessage(File::PATTERN, $this->user_id, $this->channel); $message->setFiles($files); - return $this->receivesRaw($message); + return $this->receivesIncomingMessage($message); } /** @@ -228,7 +228,7 @@ public function receivesEvent($name, $payload = null) $this->driver->setEventName($name); $this->driver->setEventPayload($payload); - return $this->receivesRaw(new IncomingMessage('', $this->user_id, $this->channel)); + return $this->receivesIncomingMessage(new IncomingMessage('', $this->user_id, $this->channel)); } /** @@ -350,7 +350,7 @@ public function assertTemplate($template, $strict = false) * @param OutgoingMessage $message * @return $this */ - public function assertRaw($message) + public function assertOutgoingMessage($message) { PHPUnit::assertSame($message, $this->getReply()); @@ -361,7 +361,7 @@ public function assertRaw($message) * @param int $times * @return $this */ - public function reply($times = 1) + public function skipReply($times = 1) { foreach (range(1, $times) as $time) { $this->getReply(); From 925c5deccee39ae4715620cc02f2e37a5ce7e19f Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Mon, 9 Oct 2017 21:14:47 +0300 Subject: [PATCH 02/14] Fix for assertReplyIsNot dont shift out actual message --- src/Testing/BotManTester.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 7af5896..24bca9c 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -274,7 +274,14 @@ public function assertReplies($expectedMessages) */ public function assertReplyIsNot($text) { - PHPUnit::assertNotSame($this->getReply()->getText(), $text); + $message = $this->getReply(); + if($message instanceof OutgoingMessage){ + PHPUnit::assertNotSame($message->getText(), $text); + } else { + PHPUnit::assertNotEquals($message, $text); + } + + array_unshift($this->botMessages, $message); return $this; } From 3448ada02cc9a093261a55d7997f64a0196ec58a Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Mon, 9 Oct 2017 21:17:02 +0300 Subject: [PATCH 03/14] Fixed StyleCI --- src/Testing/BotManTester.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 24bca9c..18c1e99 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -275,7 +275,7 @@ public function assertReplies($expectedMessages) public function assertReplyIsNot($text) { $message = $this->getReply(); - if($message instanceof OutgoingMessage){ + if ($message instanceof OutgoingMessage) { PHPUnit::assertNotSame($message->getText(), $text); } else { PHPUnit::assertNotEquals($message, $text); From eb14d7b1c5be3c7457ad86f784e00b97b2a476f2 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Tue, 10 Oct 2017 11:57:39 +0300 Subject: [PATCH 04/14] Changed names back --- src/Testing/BotManTester.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 18c1e99..f08c989 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -96,7 +96,7 @@ public function setUser($user_info) * @param IncomingMessage $message * @return $this */ - public function receivesIncomingMessage($message) + public function receivesRaw($message) { $this->driver->messages = [$message]; @@ -114,7 +114,7 @@ public function receivesIncomingMessage($message) */ public function receives($message) { - return $this->receivesIncomingMessage(new IncomingMessage($message, $this->user_id, $this->channel)); + return $this->receivesRaw(new IncomingMessage($message, $this->user_id, $this->channel)); } /** @@ -138,7 +138,7 @@ public function receivesLocation($latitude = 24, $longitude = 57) $message = new IncomingMessage(Location::PATTERN, $this->user_id, $this->channel); $message->setLocation(new Location($latitude, $longitude, null)); - return $this->receivesIncomingMessage($message); + return $this->receivesRaw($message); } /** @@ -157,7 +157,7 @@ public function receivesImages(array $urls = null) $message = new IncomingMessage(Image::PATTERN, $this->user_id, $this->channel); $message->setImages($images); - return $this->receivesIncomingMessage($message); + return $this->receivesRaw($message); } /** @@ -177,7 +177,7 @@ public function receivesAudio(array $urls = null) $message = new IncomingMessage(Audio::PATTERN, $this->user_id, $this->channel); $message->setAudio($audio); - return $this->receivesIncomingMessage($message); + return $this->receivesRaw($message); } /** @@ -196,7 +196,7 @@ public function receivesVideos(array $urls = null) $message = new IncomingMessage(Video::PATTERN, $this->user_id, $this->channel); $message->setVideos($videos); - return $this->receivesIncomingMessage($message); + return $this->receivesRaw($message); } /** @@ -215,7 +215,7 @@ public function receivesFiles(array $urls = null) $message = new IncomingMessage(File::PATTERN, $this->user_id, $this->channel); $message->setFiles($files); - return $this->receivesIncomingMessage($message); + return $this->receivesRaw($message); } /** @@ -228,7 +228,7 @@ public function receivesEvent($name, $payload = null) $this->driver->setEventName($name); $this->driver->setEventPayload($payload); - return $this->receivesIncomingMessage(new IncomingMessage('', $this->user_id, $this->channel)); + return $this->receivesRaw(new IncomingMessage('', $this->user_id, $this->channel)); } /** @@ -357,7 +357,7 @@ public function assertTemplate($template, $strict = false) * @param OutgoingMessage $message * @return $this */ - public function assertOutgoingMessage($message) + public function assertRaw($message) { PHPUnit::assertSame($message, $this->getReply()); @@ -368,7 +368,7 @@ public function assertOutgoingMessage($message) * @param int $times * @return $this */ - public function skipReply($times = 1) + public function reply($times = 1) { foreach (range(1, $times) as $time) { $this->getReply(); From 96583c85e9ffe617f951b13ab6205947268d03d9 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Fri, 27 Oct 2017 16:31:32 +0300 Subject: [PATCH 05/14] progress --- src/Testing/BotManTester.php | 6 +- src/Testing/ButtonTester.php | 71 ++++++++++++++ src/Testing/ElementButtonTester.php | 80 ++++++++++++++++ src/Testing/QuestionTester.php | 95 +++++++++++++++++++ src/Testing/TemplateTester.php | 139 ++++++++++++++++++++++++++++ 5 files changed, 390 insertions(+), 1 deletion(-) create mode 100644 src/Testing/ButtonTester.php create mode 100644 src/Testing/ElementButtonTester.php create mode 100644 src/Testing/QuestionTester.php create mode 100644 src/Testing/TemplateTester.php diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index f08c989..3d591d3 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -322,7 +322,7 @@ public function assertReplyNothing() * @param null $text * @return $this */ - public function assertQuestion($text = null) + public function assertQuestion($text = null, $closure = null) { /** @var Question $question */ $question = $this->getReply(); @@ -332,6 +332,10 @@ public function assertQuestion($text = null) PHPUnit::assertSame($text, $question->getText()); } + if (is_callable($closure)){ + call_user_func($closure, new QuestionTester($question)); + } + return $this; } diff --git a/src/Testing/ButtonTester.php b/src/Testing/ButtonTester.php new file mode 100644 index 0000000..06af766 --- /dev/null +++ b/src/Testing/ButtonTester.php @@ -0,0 +1,71 @@ +button = $button; + } + + public function assertText($text) + { + PHPUnit::assertSame($text, $this->button['text']); + } + + public function assertTextIsNot($text) + { + PHPUnit::assertNotSame($text, $this->button['text']); + } + + public function assertName($name) + { + PHPUnit::assertSame($name, $this->button['name']); + } + + public function assertNameIsNot($name) + { + PHPUnit::assertNotSame($name, $this->button['name']); + } + + public function assertValue($value) + { + PHPUnit::assertSame($value, $this->button['value']); + } + + public function assertValueIsNot($value) + { + PHPUnit::assertNotSame($value, $this->button['value']); + } + + public function assertImage($image_url) + { + PHPUnit::assertSame($image_url, $this->button['image_url']); + } + + public function assertImageIsNot($image_url) + { + PHPUnit::assertNotSame($image_url, $this->button['image_url']); + } + + public function assertAdditional($additional) + { + PHPUnit::assertEqual($additional, $this->button['additional']); + } + + public function assertAdditionalIsNot($additional) + { + PHPUnit::assertNotEqual($additional, $this->button['additional']); + } +} \ No newline at end of file diff --git a/src/Testing/ElementButtonTester.php b/src/Testing/ElementButtonTester.php new file mode 100644 index 0000000..25566ab --- /dev/null +++ b/src/Testing/ElementButtonTester.php @@ -0,0 +1,80 @@ +element = $element; + } + + public function assertButtonCount($count) + { + $this->setMatch($count == count($this->element['buttons'])); + + return $this; + } + + public function assertHasButton(array $data) { + $button_matches = false; + + foreach ($this->element['buttons'] as $button) { + if($button_matches=$this->checkButton($button, $data)) { + break; + } + + } + $this->setMatch($button_matches); + } + + public function assertHasNotButton(array $data) { + $button_matches = false; + + foreach ($this->element['buttons'] as $button) { + if($button_matches=$this->checkButton($button, $data)) { + break; + } + + } + $this->setMatch(!$button_matches); + } + + public function setMatch(bool $match) { + if(!$match) { + $this->match = $match; + } + } + + public function getMatch() { + return $this->match; + } + + private function checkButton($button, $data) { + $attributes_matches = true; + + foreach ($data as $key => $value) { + + if(array_has($button, $key) && array_get($button, $key) == $value) { + continue; + } + + $attributes_matches = false; + break; + } + + return ($attributes_matches) ? true : false; + } +} \ No newline at end of file diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php new file mode 100644 index 0000000..1205d51 --- /dev/null +++ b/src/Testing/QuestionTester.php @@ -0,0 +1,95 @@ +question = $question->toArray(); + } + + public function assertText($text) + { + PHPUnit::assertSame($text, $this->question['text']); + } + + public function assertTextIsNot($text) + { + PHPUnit::assertNotSame($text, $this->question['text']); + } + + public function assertFallback($fallback) + { + PHPUnit::assertSame($fallback, $this->question['fallback']); + } + + public function assertFallbackIsNot($fallback) + { + PHPUnit::assertNotSame($fallback, $this->question['fallback']); + } + + public function assertCallbackId($callback) + { + PHPUnit::assertSame($callback, $this->question['callback_id']); + } + + public function assertCallbackIdIsNot($callback) + { + PHPUnit::assertNotSame($callback, $this->question['callback_id']); + } + + public function assertButtonCount($count) + { + PHPUnit::assertCount($count, $this->question['actions']); + } + + public function assertButton($index, $closure) + { + $button = $this->question['actions'][$index]; + call_user_func($closure, new ButtonTester($button)); + } + + public function assertHasButton($text) + { + PHPUnit::assertTrue(in_array($text, $this->pluck('text'))); + } + + public function assertHasNotButton($text) + { + PHPUnit::assertFalse(in_array($text, $this->pluck('text'))); + } + + public function assertHasValue($value) + { + PHPUnit::assertTrue(in_array($value, $this->pluck('value'))); + } + + public function assertHasNotValue($value) + { + PHPUnit::assertFalse(in_array($value, $this->pluck('value'))); + } + + public function assertButtons(array $buttons) + { + sort($buttons); + + PHPUnit::assertEquals(array_sort($this->pluck('text')), $buttons); + } + + + private function pluck ($type) { + return collect($this->question['actions']) + ->pluck($type) + ->toArray(); + } +} \ No newline at end of file diff --git a/src/Testing/TemplateTester.php b/src/Testing/TemplateTester.php new file mode 100644 index 0000000..c11e45e --- /dev/null +++ b/src/Testing/TemplateTester.php @@ -0,0 +1,139 @@ +template = $template->toArray()['attachment']; + } + + public function assertElementCount($count) + { + PHPUnit::assertCount($count, $this->template['payload']['elements']); + + return $this; + } + + public function assertElement($index, $closure) + { + + } + + public function assertHasElement(array $data, $closure = null) { + $element_matches = false; + + foreach ($this->template['payload']['elements'] as $element) { + if($element_matches = $this->checkElement($element, $data, $closure)){ + break; + } + } + + PHPUnit::assertTrue($element_matches, 'Failed asserting that template has given element'); + + return $this; + } + + public function assertHasNotElement(array $data, $closure = null) { + $element_matches = false; + + foreach ($this->template['payload']['elements'] as $element) { + if($element_matches = $this->checkElement($element, $data, $closure)){ + break; + } + } + + PHPUnit::assertFalse($element_matches, 'Failed asserting that template does not have given element'); + + return $this; + } + +// public function assertElement($index, array $data, $closure = null){ +// $element = $this->template['payload']['elements'][$index]; +// $element_matches = $this->checkElement($element, $data, $closure); +// +// PHPUnit::assertTrue($element_matches, "Failed asserting that templates element with index {$index}"); +// +// return $this; +// } + + public function __call($name, $arguments) + { + $kebab_name = kebab_case($name); + + if(starts_with($kebab_name, 'assert') && ends_with($kebab_name, 'element')){ + switch(explode('-', $kebab_name)[1]) { + case 'first': + $index = 0; + break; + case 'second': + $index = 1; + break; + case 'third': + $index = 2; + break; + case 'forth': + $index = 3; + break; + case 'fifth': + $index = 4; + break; + case 'sixth': + $index = 5; + break; + case 'seventh': + $index = 6; + break; + case 'eighth': + $index = 7; + break; + case 'ninth': + $index = 8; + break; + case 'last': + $index = count($this->template['payload']['elements']) - 1; + break; + default: + $index = null; + } + if($index) { + return call_user_func([$this, 'assertElement'], $index, $arguments[0], $arguments[1] ?? null); + } + } + + throw new \Exception("There is no method {$name}"); + } + + private function checkElement($actual, $data, $closure) { + $attributes_matches = true; + + foreach ($data as $key => $value) { + if(array_has($actual, $key) && array_get($actual, $key) == $value) { + continue; + } + $attributes_matches = false; + break; + } + + $buttons_matches = true; + + if (is_callable($closure)) { + $elementButtonTester = new ElementButtonTester($actual); + call_user_func($closure, $elementButtonTester); + $buttons_matches = $elementButtonTester->getMatch(); + } + + return ($attributes_matches && $buttons_matches) ? true : false; + } +} \ No newline at end of file From cd44401f30801b13ab9d5faf49bf7ebb249bd5b9 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 12:23:58 +0300 Subject: [PATCH 06/14] Added closure to assertQuestion --- src/Testing/BotManTester.php | 2 +- src/Testing/ButtonTester.php | 2 - src/Testing/ElementTester.php | 71 ++++++++++++++++++++++++++++++++++ src/Testing/QuestionTester.php | 34 ---------------- 4 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 src/Testing/ElementTester.php diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 3d591d3..b0d08ec 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -332,7 +332,7 @@ public function assertQuestion($text = null, $closure = null) PHPUnit::assertSame($text, $question->getText()); } - if (is_callable($closure)){ + if (! is_null($closure)) { call_user_func($closure, new QuestionTester($question)); } diff --git a/src/Testing/ButtonTester.php b/src/Testing/ButtonTester.php index 06af766..c64e099 100644 --- a/src/Testing/ButtonTester.php +++ b/src/Testing/ButtonTester.php @@ -2,8 +2,6 @@ namespace BotMan\Studio\Testing; -use BotMan\BotMan\Messages\Outgoing\Question; -use BotMan\Drivers\Facebook\Extensions\Element; use PHPUnit\Framework\Assert as PHPUnit; /** diff --git a/src/Testing/ElementTester.php b/src/Testing/ElementTester.php new file mode 100644 index 0000000..06af766 --- /dev/null +++ b/src/Testing/ElementTester.php @@ -0,0 +1,71 @@ +button = $button; + } + + public function assertText($text) + { + PHPUnit::assertSame($text, $this->button['text']); + } + + public function assertTextIsNot($text) + { + PHPUnit::assertNotSame($text, $this->button['text']); + } + + public function assertName($name) + { + PHPUnit::assertSame($name, $this->button['name']); + } + + public function assertNameIsNot($name) + { + PHPUnit::assertNotSame($name, $this->button['name']); + } + + public function assertValue($value) + { + PHPUnit::assertSame($value, $this->button['value']); + } + + public function assertValueIsNot($value) + { + PHPUnit::assertNotSame($value, $this->button['value']); + } + + public function assertImage($image_url) + { + PHPUnit::assertSame($image_url, $this->button['image_url']); + } + + public function assertImageIsNot($image_url) + { + PHPUnit::assertNotSame($image_url, $this->button['image_url']); + } + + public function assertAdditional($additional) + { + PHPUnit::assertEqual($additional, $this->button['additional']); + } + + public function assertAdditionalIsNot($additional) + { + PHPUnit::assertNotEqual($additional, $this->button['additional']); + } +} \ No newline at end of file diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php index 1205d51..d4e73df 100644 --- a/src/Testing/QuestionTester.php +++ b/src/Testing/QuestionTester.php @@ -58,38 +58,4 @@ public function assertButton($index, $closure) $button = $this->question['actions'][$index]; call_user_func($closure, new ButtonTester($button)); } - - public function assertHasButton($text) - { - PHPUnit::assertTrue(in_array($text, $this->pluck('text'))); - } - - public function assertHasNotButton($text) - { - PHPUnit::assertFalse(in_array($text, $this->pluck('text'))); - } - - public function assertHasValue($value) - { - PHPUnit::assertTrue(in_array($value, $this->pluck('value'))); - } - - public function assertHasNotValue($value) - { - PHPUnit::assertFalse(in_array($value, $this->pluck('value'))); - } - - public function assertButtons(array $buttons) - { - sort($buttons); - - PHPUnit::assertEquals(array_sort($this->pluck('text')), $buttons); - } - - - private function pluck ($type) { - return collect($this->question['actions']) - ->pluck($type) - ->toArray(); - } } \ No newline at end of file From 2c71752d7c57a2620bdadaa2c21520203bc8a10a Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 13:03:48 +0300 Subject: [PATCH 07/14] Methods can be chained --- src/Testing/ButtonTester.php | 20 ++++++++++++++++++++ src/Testing/QuestionTester.php | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Testing/ButtonTester.php b/src/Testing/ButtonTester.php index c64e099..c82aecf 100644 --- a/src/Testing/ButtonTester.php +++ b/src/Testing/ButtonTester.php @@ -20,50 +20,70 @@ public function __construct(array $button) public function assertText($text) { PHPUnit::assertSame($text, $this->button['text']); + + return $this; } public function assertTextIsNot($text) { PHPUnit::assertNotSame($text, $this->button['text']); + + return $this; } public function assertName($name) { PHPUnit::assertSame($name, $this->button['name']); + + return $this; } public function assertNameIsNot($name) { PHPUnit::assertNotSame($name, $this->button['name']); + + return $this; } public function assertValue($value) { PHPUnit::assertSame($value, $this->button['value']); + + return $this; } public function assertValueIsNot($value) { PHPUnit::assertNotSame($value, $this->button['value']); + + return $this; } public function assertImage($image_url) { PHPUnit::assertSame($image_url, $this->button['image_url']); + + return $this; } public function assertImageIsNot($image_url) { PHPUnit::assertNotSame($image_url, $this->button['image_url']); + + return $this; } public function assertAdditional($additional) { PHPUnit::assertEqual($additional, $this->button['additional']); + + return $this; } public function assertAdditionalIsNot($additional) { PHPUnit::assertNotEqual($additional, $this->button['additional']); + + return $this; } } \ No newline at end of file diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php index d4e73df..59656b3 100644 --- a/src/Testing/QuestionTester.php +++ b/src/Testing/QuestionTester.php @@ -21,41 +21,57 @@ public function __construct(Question $question) public function assertText($text) { PHPUnit::assertSame($text, $this->question['text']); + + return $this; } public function assertTextIsNot($text) { PHPUnit::assertNotSame($text, $this->question['text']); + + return $this; } public function assertFallback($fallback) { PHPUnit::assertSame($fallback, $this->question['fallback']); + + return $this; } public function assertFallbackIsNot($fallback) { PHPUnit::assertNotSame($fallback, $this->question['fallback']); + + return $this; } public function assertCallbackId($callback) { PHPUnit::assertSame($callback, $this->question['callback_id']); + + return $this; } public function assertCallbackIdIsNot($callback) { PHPUnit::assertNotSame($callback, $this->question['callback_id']); + + return $this; } public function assertButtonCount($count) { PHPUnit::assertCount($count, $this->question['actions']); + + return $this; } public function assertButton($index, $closure) { $button = $this->question['actions'][$index]; call_user_func($closure, new ButtonTester($button)); + + return $this; } } \ No newline at end of file From da205f0e58a17947c85b13401ba6a87c83fd9322 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 14:11:15 +0300 Subject: [PATCH 08/14] Added methods for template testing --- composer.json | 1 + src/Testing/BotManTester.php | 10 +- src/Testing/ElementButtonTester.php | 107 ++++++++++++++-------- src/Testing/ElementTester.php | 66 ++++++++------ src/Testing/QuestionTester.php | 12 +++ src/Testing/TemplateTester.php | 137 ++++++++++------------------ 6 files changed, 174 insertions(+), 159 deletions(-) diff --git a/composer.json b/composer.json index cc4f853..dcf2b86 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "require": { "php": ">=7.0", "botman/botman": "~2.0", + "botman/driver-facebook": "^1.0", "guzzlehttp/guzzle": "~6.0", "illuminate/support": "~5.5.0", "illuminate/contracts": "~5.5.0", diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index b0d08ec..a0d7ff2 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -344,14 +344,14 @@ public function assertQuestion($text = null, $closure = null) * @param bool $strict * @return $this */ - public function assertTemplate($template, $strict = false) + public function assertTemplate($template, $closure = null) { $message = $this->getReply(); - if ($strict) { - PHPUnit::assertEquals($template, $message); - } else { - PHPUnit::assertInstanceOf($template, $message); + PHPUnit::assertInstanceOf($template, $message); + + if(is_callable($closure)) { + call_user_func($closure, new TemplateTester($message)); } return $this; diff --git a/src/Testing/ElementButtonTester.php b/src/Testing/ElementButtonTester.php index 25566ab..c109a7b 100644 --- a/src/Testing/ElementButtonTester.php +++ b/src/Testing/ElementButtonTester.php @@ -7,74 +7,103 @@ use PHPUnit\Framework\Assert as PHPUnit; /** - * Class QuestionTester. + * Class ElementButtonTester. */ class ElementButtonTester { - protected $element; + protected $button; - protected $match = true; + public function __construct(array $button) + { + $this->button = $button; - public function __construct(array $element) + return $this; + } + + public function assertTitle($title) { - $this->element = $element; + PHPUnit::assertSame($title, $this->button['title']); + + return $this; } - public function assertButtonCount($count) + public function assertTitleIsNot($title) { - $this->setMatch($count == count($this->element['buttons'])); + PHPUnit::assertNotSame($title, $this->button['title']); return $this; } - public function assertHasButton(array $data) { - $button_matches = false; + public function assertType($type) + { + PHPUnit::assertSame($type, $this->button['type']); + + return $this; + } - foreach ($this->element['buttons'] as $button) { - if($button_matches=$this->checkButton($button, $data)) { - break; - } + public function assertTypeIsNot($type) + { + PHPUnit::assertNotSame($type, $this->button['type']); - } - $this->setMatch($button_matches); + return $this; } - public function assertHasNotButton(array $data) { - $button_matches = false; + public function assertUrl($url) + { + PHPUnit::assertSame($url, $this->button['url']); + + return $this; + } - foreach ($this->element['buttons'] as $button) { - if($button_matches=$this->checkButton($button, $data)) { - break; - } + public function assertUrlIsNot($url) + { + PHPUnit::assertNotSame($url, $this->button['url']); - } - $this->setMatch(!$button_matches); + return $this; } - public function setMatch(bool $match) { - if(!$match) { - $this->match = $match; - } + public function assertPayload($payload) + { + PHPUnit::assertSame($payload, $this->button['payload']); + + return $this; } - public function getMatch() { - return $this->match; + public function assertPayloadIsNot($payload) + { + PHPUnit::assertNotSame($payload, $this->button['payload']); + + return $this; } - private function checkButton($button, $data) { - $attributes_matches = true; - foreach ($data as $key => $value) { + public function assertHeightRatio($webview_height_ratio) + { + PHPUnit::assertSame($webview_height_ratio, $this->button['webview_height_ratio']); - if(array_has($button, $key) && array_get($button, $key) == $value) { - continue; - } + return $this; + } - $attributes_matches = false; - break; - } + public function assertMessengerExtension($messenger_extensions = true) + { + PHPUnit::assertSame($messenger_extensions, $this->button['messenger_extensions']); - return ($attributes_matches) ? true : false; + return $this; + } + + public function assertFallbackUrl($fallback_url) + { + PHPUnit::assertSame($fallback_url, $this->button['fallback_url']); + + return $this; + } + + public function assertShareContents($closure) + { + $share_content = $this->button['share_contents']; + call_user_func($closure, new TemplateTester($share_content)); + + return $this; } } \ No newline at end of file diff --git a/src/Testing/ElementTester.php b/src/Testing/ElementTester.php index 06af766..4555531 100644 --- a/src/Testing/ElementTester.php +++ b/src/Testing/ElementTester.php @@ -7,65 +7,77 @@ use PHPUnit\Framework\Assert as PHPUnit; /** - * Class QuestionTester. + * Class ElementTester */ -class ButtonTester +class ElementTester { - protected $button; + protected $element; - public function __construct(array $button) + public function __construct(array $element) { - $this->button = $button; + $this->element = $element; } - public function assertText($text) + public function assertTitle($title) { - PHPUnit::assertSame($text, $this->button['text']); - } + PHPUnit::assertSame($title, $this->element['title']); - public function assertTextIsNot($text) - { - PHPUnit::assertNotSame($text, $this->button['text']); + return $this; } - public function assertName($name) + public function assertTitleIsNot($title) { - PHPUnit::assertSame($name, $this->button['name']); - } + PHPUnit::assertNotSame($title, $this->element['title']); - public function assertNameIsNot($name) - { - PHPUnit::assertNotSame($name, $this->button['name']); + return $this; } - public function assertValue($value) + public function assertSubtitle($subtitle) { - PHPUnit::assertSame($value, $this->button['value']); + PHPUnit::assertSame($subtitle, $this->element['subtitle']); + + return $this; } - public function assertValueIsNot($value) + public function assertSubtitleIsNot($subtitle) { - PHPUnit::assertNotSame($value, $this->button['value']); + PHPUnit::assertNotSame($subtitle, $this->element['subtitle']); + + return $this; } public function assertImage($image_url) { - PHPUnit::assertSame($image_url, $this->button['image_url']); + PHPUnit::assertSame($image_url, $this->element['image_url']); + + return $this; } public function assertImageIsNot($image_url) { - PHPUnit::assertNotSame($image_url, $this->button['image_url']); + PHPUnit::assertNotSame($image_url, $this->element['image_url']); + + return $this; + } + + public function assertButton($index, $closure) + { + $button = $this->element['buttons'][$index]; + call_user_func($closure, new ElementButtonTester($button)); + + return $this; } - public function assertAdditional($additional) + public function assertFirstButton($closure) { - PHPUnit::assertEqual($additional, $this->button['additional']); + return $this->assertButton(0, $closure); } - public function assertAdditionalIsNot($additional) + public function assertLastButton($closure) { - PHPUnit::assertNotEqual($additional, $this->button['additional']); + $last_index = count($this->element['buttons']) - 1; + + return $this->assertButton($last_index, $closure); } } \ No newline at end of file diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php index 59656b3..a906aa0 100644 --- a/src/Testing/QuestionTester.php +++ b/src/Testing/QuestionTester.php @@ -74,4 +74,16 @@ public function assertButton($index, $closure) return $this; } + + public function assertFirstButton($closure) + { + return $this->assertButton(0, $closure); + } + + public function assertLastButton($closure) + { + $last_index = count($this->question['actions']) - 1; + + return $this->assertButton($last_index, $closure); + } } \ No newline at end of file diff --git a/src/Testing/TemplateTester.php b/src/Testing/TemplateTester.php index c11e45e..c3fc071 100644 --- a/src/Testing/TemplateTester.php +++ b/src/Testing/TemplateTester.php @@ -12,128 +12,89 @@ class TemplateTester { - protected $template; + protected $payload; public function __construct($template) { - $this->template = $template->toArray()['attachment']; + $this->payload = $template->toArray()['attachment']['payload']; } - public function assertElementCount($count) + public function assertText($text) { - PHPUnit::assertCount($count, $this->template['payload']['elements']); + PHPUnit::assertSame($text, $this->payload['text']); return $this; } - public function assertElement($index, $closure) + public function assertTextIsNot($text) { + PHPUnit::assertNotSame($text, $this->payload['text']); + return $this; } - public function assertHasElement(array $data, $closure = null) { - $element_matches = false; - - foreach ($this->template['payload']['elements'] as $element) { - if($element_matches = $this->checkElement($element, $data, $closure)){ - break; - } - } +// public function assertSharable($sharable) +// { +// PHPUnit::assertSame($sharable, $this->payload['sharable']); +// +// return $this; +// } - PHPUnit::assertTrue($element_matches, 'Failed asserting that template has given element'); + public function assertImageAspectRatio($image_aspect_ratio) + { + PHPUnit::assertSame($image_aspect_ratio, $this->payload['image_aspect_ratio']); return $this; } - public function assertHasNotElement(array $data, $closure = null) { - $element_matches = false; + public function assertTopElementStyle($top_element_style) + { + PHPUnit::assertSame($top_element_style, $this->payload['top_element_style']); - foreach ($this->template['payload']['elements'] as $element) { - if($element_matches = $this->checkElement($element, $data, $closure)){ - break; - } - } + return $this; + } - PHPUnit::assertFalse($element_matches, 'Failed asserting that template does not have given element'); + public function assertButtons($closure) + { + $button = $this->payload['buttons'][0]; + call_user_func($closure, new ElementButtonTester($button)); return $this; } -// public function assertElement($index, array $data, $closure = null){ -// $element = $this->template['payload']['elements'][$index]; -// $element_matches = $this->checkElement($element, $data, $closure); -// -// PHPUnit::assertTrue($element_matches, "Failed asserting that templates element with index {$index}"); -// -// return $this; -// } + public function assertElementCount($count) + { + PHPUnit::assertCount($count, $this->payload['elements']); + + return $this; + } - public function __call($name, $arguments) + public function assertElement($index, $closure) { - $kebab_name = kebab_case($name); - - if(starts_with($kebab_name, 'assert') && ends_with($kebab_name, 'element')){ - switch(explode('-', $kebab_name)[1]) { - case 'first': - $index = 0; - break; - case 'second': - $index = 1; - break; - case 'third': - $index = 2; - break; - case 'forth': - $index = 3; - break; - case 'fifth': - $index = 4; - break; - case 'sixth': - $index = 5; - break; - case 'seventh': - $index = 6; - break; - case 'eighth': - $index = 7; - break; - case 'ninth': - $index = 8; - break; - case 'last': - $index = count($this->template['payload']['elements']) - 1; - break; - default: - $index = null; - } - if($index) { - return call_user_func([$this, 'assertElement'], $index, $arguments[0], $arguments[1] ?? null); - } - } + $element = $this->payload['elements'][$index]; + call_user_func($closure, new ElementTester($element)); - throw new \Exception("There is no method {$name}"); + return $this; } - private function checkElement($actual, $data, $closure) { - $attributes_matches = true; + public function assertFirstElement($closure) + { + return $this->assertElement(0, $closure); + } - foreach ($data as $key => $value) { - if(array_has($actual, $key) && array_get($actual, $key) == $value) { - continue; - } - $attributes_matches = false; - break; - } + public function assertLastElement($closure) + { + $last_index = count($this->payload['elements']) - 1; - $buttons_matches = true; + return $this->assertElement($last_index, $closure); + } - if (is_callable($closure)) { - $elementButtonTester = new ElementButtonTester($actual); - call_user_func($closure, $elementButtonTester); - $buttons_matches = $elementButtonTester->getMatch(); + public function assertAttributes($attributes) + { + foreach ($attributes as $key => $value) { + PHPUnit::assertSame($value, array_get($this->payload, $key)); } - return ($attributes_matches && $buttons_matches) ? true : false; + return $this; } } \ No newline at end of file From 505c6250db58f5a029fc8faae025c329e8314b18 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 14:11:39 +0300 Subject: [PATCH 09/14] Created tests for question and template testing --- tests/BotManTesterTest.php | 386 +++++++++++++++++++++++++++++++++++++ 1 file changed, 386 insertions(+) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 7725e9e..d7f4841 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -2,6 +2,17 @@ namespace Tests; +use BotMan\BotMan\Messages\Outgoing\Actions\Button; +use BotMan\Drivers\Facebook\Extensions\ButtonTemplate; +use BotMan\Drivers\Facebook\Extensions\Element; +use BotMan\Drivers\Facebook\Extensions\ElementButton; +use BotMan\Drivers\Facebook\Extensions\GenericTemplate; +use BotMan\Drivers\Facebook\Extensions\ListTemplate; +use BotMan\Drivers\Facebook\Extensions\ReceiptAddress; +use BotMan\Drivers\Facebook\Extensions\ReceiptElement; +use BotMan\Drivers\Facebook\Extensions\ReceiptSummary; +use BotMan\Drivers\Facebook\Extensions\ReceiptTemplate; +use BotMan\Studio\Testing\ButtonTester; use Mockery as m; use BotMan\BotMan\BotMan; use PHPUnit\Framework\TestCase; @@ -311,4 +322,379 @@ public function it_can_test_questions() $this->tester->receives('answer'); $this->tester->assertReply('success'); } + + /** @test */ + public function it_can_test_question_with_closure() + { + $this->botman->hears('message', function ($bot) { + $bot->ask(Question::create('question'), function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertText('question'); + }); + $this->tester->receives('answer'); + $this->tester->assertReply('success'); + } + + /** @test */ + public function it_can_test_question_callback_and_fallback() + { + $question = Question::create('question') + ->fallback('fallback') + ->callbackId('callback_id'); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertFallback('fallback'); + $q->assertCallbackId('callback_id'); + }); + $this->tester->receives('answer'); + $this->tester->assertReply('success'); + } + + /** @test */ + public function it_can_test_question_button_count() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + Button::create('Second')->value('second'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertButtonCount(2); + }); + } + + /** @test */ + public function it_can_test_question_specific_button() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + Button::create('Second')->value('second'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertButton(0, function($b) { + $b->assertText('First'); + $b->assertValue('first'); + }); + $q->assertButton(1, function($b) { + $b->assertText('Second'); + $b->assertValue('second'); + }); + }); + } + + /** @test */ + public function it_can_test_question_specific_button_is_not() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + Button::create('Second')->value('second'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertButton(0, function($b) { + $b->assertTextisNot('Second'); + $b->assertValueisNot('second'); + }); + $q->assertButton(1, function($b) { + $b->assertTextisNot('First'); + $b->assertValueisNot('first'); + }); + }); + } + + /** @test */ + public function it_can_test_question_first_and_last_button() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + Button::create('Second')->value('second'), + Button::create('Third')->value('third'), + Button::create('Forth')->value('forth'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function($q) { + $q->assertFirstButton( function($b) { + $b->assertText('First'); + }); + $q->assertLastButton(function($b) { + $b->assertText('Forth'); + }); + }); + } + + /** @test */ + public function it_can_test_template() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElement(Element::create('title')) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class); + } + + /** @test */ + public function it_can_test_template_with_closure() + { + $this->botman->hears('button', function ($bot){ + $bot->reply(ButtonTemplate::create('text') + ->addButton(ElementButton::create('First')->type('web_url')->url('www.botman.io')) + ); + }); + + $this->tester->receives('button'); + $this->tester->assertTemplate(ButtonTemplate::class, function($t) { + $t->assertText('text'); + }); + } + + /** @test */ + public function it_can_test_template_image_aspect_ratio() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElement(Element::create('title')) + ->addImageAspectRatio(GenericTemplate::RATIO_HORIZONTAL) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertImageAspectRatio(GenericTemplate::RATIO_HORIZONTAL); + }); + } + + /** @test */ + public function it_can_test_list_template_attributes() + { + $this->botman->hears('list', function ($bot){ + $bot->reply(ListTemplate::create() + ->addElement(Element::create('title')) + ->useCompactView() + ->addGlobalButton(ElementButton::create('First')) + ); + }); + + $this->tester->receives('list'); + $this->tester->assertTemplate(ListTemplate::class, function($t) { + $t->assertTopElementStyle('compact'); + $t->assertButtons(function ($b) { + $b->assertTitle('First'); + }); + }); + } + + /** @test */ + public function it_can_test_template_attributes_is_not() + { + $this->botman->hears('button', function ($bot){ + $bot->reply(ButtonTemplate::create('text') + ->addButton(ElementButton::create('First')->type('web_url')->url('www.botman.io')) + ); + }); + + $this->tester->receives('button'); + $this->tester->assertTemplate(ButtonTemplate::class, function($t) { + $t->assertTextIsNot('txet'); + }); + } + + /** @test */ + public function it_can_test_template_element_count() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First'), + Element::create('Second') + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertElementCount(2); + }); + } + + /** @test */ + public function it_can_test_template_specific_element() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First')->subtitle('This number is before "2"')->image('www.one.com/image'), + Element::create('Second') + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertElement(0, function($e) { + $e->assertTitle('First'); + $e->assertSubtitle('This number is before "2"'); + $e->assertImage('www.one.com/image'); + }); + }); + } + + /** @test */ + public function it_can_test_template_nested_attributes() + { + $this->botman->hears('receipt', function ($bot){ + $bot->reply(ReceiptTemplate::create() + ->recipientName('Marcel') + ->addElements([ + ReceiptElement::create('First')->price(1)->currency('EUR'), + ReceiptElement::create('Second')->price(2)->currency('EUR') + ]) + ->addAddress(ReceiptAddress::create()->city('Barcelona')) + ->addSummary(ReceiptSummary::create()->totalCost(3)) + ); + }); + + $this->tester->receives('receipt'); + $this->tester->assertTemplate(ReceiptTemplate::class, function($t) { + $t->assertAttributes([ + 'recipient_name' => 'Marcel', + 'elements.0.title' => 'First', + 'elements.0.price' => 1, + 'elements.0.currency' => 'EUR', + 'elements.1.title' => 'Second', + 'address.city' => 'Barcelona', + 'summary.total_cost' => 3, + ]); + }); + } + + /** @test */ + public function it_can_test_template_first_and_last_element() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First'), + Element::create('Second'), + Element::create('Third'), + Element::create('Forth'), + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertFirstElement(function($e) { + $e->assertTitle('First'); + }); + $t->assertLastElement(function($e) { + $e->assertTitle('Forth'); + }); + }); + } + + /** @test */ + public function it_can_test_template_specific_element_specific_button() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First') + ->addButton(ElementButton::create('First') + ->type('web_url') + ->url('www.botman.io') + ->enableExtensions() + ->heightRatio(ElementButton::RATIO_COMPACT) + ->fallbackUrl('www.botman.io/fallback') + ), + Element::create('Second') + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertElement(0, function($e) { + $e->assertButton(0, function($b) { + $b->assertTitle('First') + ->assertType('web_url') + ->assertUrl('www.botman.io') + ->assertHeightRatio(ElementButton::RATIO_COMPACT) + ->assertMessengerExtension(true) + ->assertFallbackUrl('www.botman.io/fallback'); + }); + }); + }); + } + + /** @test */ + public function it_can_test_template_specific_element_first_and_last_button() + { + $this->botman->hears('generic', function ($bot){ + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First')->addButtons([ + ElementButton::create('First')->type('web_url')->url('www.botman.io'), + ElementButton::create('Second')->type('web_url')->url('www.botman.io'), + ElementButton::create('Third')->type('web_url')->url('www.botman.io'), + ]), + Element::create('Second') + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $t->assertElement(0, function($e) { + $e->assertFirstButton(function($b) { + $b->assertTitle('First'); + }); + $e->assertLastButton(function($b) { + $b->assertTitle('Third'); + }); + }); + }); + } } From 4fc9ee074efadd82f678eb398f9dba2a203ae7c0 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 14:46:45 +0300 Subject: [PATCH 10/14] Fixed StyleCI issues --- src/Testing/BotManTester.php | 2 +- src/Testing/ButtonTester.php | 3 +- src/Testing/ElementButtonTester.php | 4 -- src/Testing/ElementTester.php | 5 +- src/Testing/QuestionTester.php | 3 +- src/Testing/TemplateTester.php | 5 +- tests/BotManTesterTest.php | 95 ++++++++++++++--------------- 7 files changed, 52 insertions(+), 65 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index a0d7ff2..58b7d6b 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -350,7 +350,7 @@ public function assertTemplate($template, $closure = null) PHPUnit::assertInstanceOf($template, $message); - if(is_callable($closure)) { + if( is_callable($closure)) { call_user_func($closure, new TemplateTester($message)); } diff --git a/src/Testing/ButtonTester.php b/src/Testing/ButtonTester.php index c82aecf..9329797 100644 --- a/src/Testing/ButtonTester.php +++ b/src/Testing/ButtonTester.php @@ -5,11 +5,10 @@ use PHPUnit\Framework\Assert as PHPUnit; /** - * Class QuestionTester. + * Class ButtonTester. */ class ButtonTester { - protected $button; public function __construct(array $button) diff --git a/src/Testing/ElementButtonTester.php b/src/Testing/ElementButtonTester.php index c109a7b..0c71eed 100644 --- a/src/Testing/ElementButtonTester.php +++ b/src/Testing/ElementButtonTester.php @@ -2,8 +2,6 @@ namespace BotMan\Studio\Testing; -use BotMan\BotMan\Messages\Outgoing\Question; -use BotMan\Drivers\Facebook\Extensions\Element; use PHPUnit\Framework\Assert as PHPUnit; /** @@ -11,7 +9,6 @@ */ class ElementButtonTester { - protected $button; public function __construct(array $button) @@ -77,7 +74,6 @@ public function assertPayloadIsNot($payload) return $this; } - public function assertHeightRatio($webview_height_ratio) { PHPUnit::assertSame($webview_height_ratio, $this->button['webview_height_ratio']); diff --git a/src/Testing/ElementTester.php b/src/Testing/ElementTester.php index 4555531..1cc241d 100644 --- a/src/Testing/ElementTester.php +++ b/src/Testing/ElementTester.php @@ -2,16 +2,13 @@ namespace BotMan\Studio\Testing; -use BotMan\BotMan\Messages\Outgoing\Question; -use BotMan\Drivers\Facebook\Extensions\Element; use PHPUnit\Framework\Assert as PHPUnit; /** - * Class ElementTester + * Class ElementTester. */ class ElementTester { - protected $element; public function __construct(array $element) diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php index a906aa0..c1a2c06 100644 --- a/src/Testing/QuestionTester.php +++ b/src/Testing/QuestionTester.php @@ -2,15 +2,14 @@ namespace BotMan\Studio\Testing; -use BotMan\BotMan\Messages\Outgoing\Question; use PHPUnit\Framework\Assert as PHPUnit; +use BotMan\BotMan\Messages\Outgoing\Question; /** * Class QuestionTester. */ class QuestionTester { - protected $question; public function __construct(Question $question) diff --git a/src/Testing/TemplateTester.php b/src/Testing/TemplateTester.php index c3fc071..f7ce59c 100644 --- a/src/Testing/TemplateTester.php +++ b/src/Testing/TemplateTester.php @@ -2,16 +2,13 @@ namespace BotMan\Studio\Testing; -use BotMan\BotMan\Messages\Outgoing\Question; -use function GuzzleHttp\default_ca_bundle; use PHPUnit\Framework\Assert as PHPUnit; /** - * Class QuestionTester. + * Class TemplateTester. */ class TemplateTester { - protected $payload; public function __construct($template) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index d7f4841..1ed8748 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -2,30 +2,29 @@ namespace Tests; -use BotMan\BotMan\Messages\Outgoing\Actions\Button; -use BotMan\Drivers\Facebook\Extensions\ButtonTemplate; -use BotMan\Drivers\Facebook\Extensions\Element; -use BotMan\Drivers\Facebook\Extensions\ElementButton; -use BotMan\Drivers\Facebook\Extensions\GenericTemplate; -use BotMan\Drivers\Facebook\Extensions\ListTemplate; -use BotMan\Drivers\Facebook\Extensions\ReceiptAddress; -use BotMan\Drivers\Facebook\Extensions\ReceiptElement; -use BotMan\Drivers\Facebook\Extensions\ReceiptSummary; -use BotMan\Drivers\Facebook\Extensions\ReceiptTemplate; -use BotMan\Studio\Testing\ButtonTester; use Mockery as m; use BotMan\BotMan\BotMan; use PHPUnit\Framework\TestCase; + use BotMan\BotMan\BotManFactory; use BotMan\Studio\Testing\BotManTester; use BotMan\BotMan\Drivers\Tests\FakeDriver; -use BotMan\BotMan\Messages\Attachments\File; -use BotMan\BotMan\Messages\Attachments\Audio; + use BotMan\BotMan\Messages\Attachments\Image; use BotMan\BotMan\Messages\Attachments\Video; use BotMan\BotMan\Messages\Outgoing\Question; +use BotMan\Drivers\Facebook\Extensions\Element; use BotMan\BotMan\Messages\Attachments\Location; +use BotMan\BotMan\Messages\Outgoing\Actions\Button; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; +use BotMan\Drivers\Facebook\Extensions\ListTemplate; +use BotMan\Drivers\Facebook\Extensions\ElementButton; +use BotMan\Drivers\Facebook\Extensions\ButtonTemplate; +use BotMan\Drivers\Facebook\Extensions\ReceiptAddress; +use BotMan\Drivers\Facebook\Extensions\ReceiptElement; +use BotMan\Drivers\Facebook\Extensions\ReceiptSummary; +use BotMan\Drivers\Facebook\Extensions\GenericTemplate; +use BotMan\Drivers\Facebook\Extensions\ReceiptTemplate; class BotManTesterTest extends TestCase { @@ -333,7 +332,7 @@ public function it_can_test_question_with_closure() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { + $this->tester->assertQuestion(null, function ($q) { $q->assertText('question'); }); $this->tester->receives('answer'); @@ -353,7 +352,7 @@ public function it_can_test_question_callback_and_fallback() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { + $this->tester->assertQuestion(null, function ($q) { $q->assertFallback('fallback'); $q->assertCallbackId('callback_id'); }); @@ -376,7 +375,7 @@ public function it_can_test_question_button_count() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { + $this->tester->assertQuestion(null, function ($q) { $q->assertButtonCount(2); }); } @@ -396,7 +395,7 @@ public function it_can_test_question_specific_button() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { + $this->tester->assertQuestion(null, function ($q) { $q->assertButton(0, function($b) { $b->assertText('First'); $b->assertValue('first'); @@ -423,7 +422,7 @@ public function it_can_test_question_specific_button_is_not() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { + $this->tester->assertQuestion(null, function ($q) { $q->assertButton(0, function($b) { $b->assertTextisNot('Second'); $b->assertValueisNot('second'); @@ -452,11 +451,11 @@ public function it_can_test_question_first_and_last_button() }); $this->tester->receives('message'); - $this->tester->assertQuestion(null, function($q) { - $q->assertFirstButton( function($b) { + $this->tester->assertQuestion(null, function ($q) { + $q->assertFirstButton( function ($b) { $b->assertText('First'); }); - $q->assertLastButton(function($b) { + $q->assertLastButton(function ($b) { $b->assertText('Forth'); }); }); @@ -485,7 +484,7 @@ public function it_can_test_template_with_closure() }); $this->tester->receives('button'); - $this->tester->assertTemplate(ButtonTemplate::class, function($t) { + $this->tester->assertTemplate(ButtonTemplate::class, function ($t) { $t->assertText('text'); }); } @@ -493,7 +492,7 @@ public function it_can_test_template_with_closure() /** @test */ public function it_can_test_template_image_aspect_ratio() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElement(Element::create('title')) ->addImageAspectRatio(GenericTemplate::RATIO_HORIZONTAL) @@ -501,7 +500,7 @@ public function it_can_test_template_image_aspect_ratio() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { $t->assertImageAspectRatio(GenericTemplate::RATIO_HORIZONTAL); }); } @@ -509,7 +508,7 @@ public function it_can_test_template_image_aspect_ratio() /** @test */ public function it_can_test_list_template_attributes() { - $this->botman->hears('list', function ($bot){ + $this->botman->hears('list', function ($bot) { $bot->reply(ListTemplate::create() ->addElement(Element::create('title')) ->useCompactView() @@ -518,7 +517,7 @@ public function it_can_test_list_template_attributes() }); $this->tester->receives('list'); - $this->tester->assertTemplate(ListTemplate::class, function($t) { + $this->tester->assertTemplate(ListTemplate::class, function ($t) { $t->assertTopElementStyle('compact'); $t->assertButtons(function ($b) { $b->assertTitle('First'); @@ -529,14 +528,14 @@ public function it_can_test_list_template_attributes() /** @test */ public function it_can_test_template_attributes_is_not() { - $this->botman->hears('button', function ($bot){ + $this->botman->hears('button', function ($bot) { $bot->reply(ButtonTemplate::create('text') ->addButton(ElementButton::create('First')->type('web_url')->url('www.botman.io')) ); }); $this->tester->receives('button'); - $this->tester->assertTemplate(ButtonTemplate::class, function($t) { + $this->tester->assertTemplate(ButtonTemplate::class, function ($t) { $t->assertTextIsNot('txet'); }); } @@ -544,7 +543,7 @@ public function it_can_test_template_attributes_is_not() /** @test */ public function it_can_test_template_element_count() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First'), @@ -554,7 +553,7 @@ public function it_can_test_template_element_count() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { $t->assertElementCount(2); }); } @@ -562,7 +561,7 @@ public function it_can_test_template_element_count() /** @test */ public function it_can_test_template_specific_element() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First')->subtitle('This number is before "2"')->image('www.one.com/image'), @@ -572,8 +571,8 @@ public function it_can_test_template_specific_element() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { - $t->assertElement(0, function($e) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { $e->assertTitle('First'); $e->assertSubtitle('This number is before "2"'); $e->assertImage('www.one.com/image'); @@ -584,7 +583,7 @@ public function it_can_test_template_specific_element() /** @test */ public function it_can_test_template_nested_attributes() { - $this->botman->hears('receipt', function ($bot){ + $this->botman->hears('receipt', function ($bot) { $bot->reply(ReceiptTemplate::create() ->recipientName('Marcel') ->addElements([ @@ -597,7 +596,7 @@ public function it_can_test_template_nested_attributes() }); $this->tester->receives('receipt'); - $this->tester->assertTemplate(ReceiptTemplate::class, function($t) { + $this->tester->assertTemplate(ReceiptTemplate::class, function ($t) { $t->assertAttributes([ 'recipient_name' => 'Marcel', 'elements.0.title' => 'First', @@ -613,7 +612,7 @@ public function it_can_test_template_nested_attributes() /** @test */ public function it_can_test_template_first_and_last_element() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First'), @@ -625,11 +624,11 @@ public function it_can_test_template_first_and_last_element() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { - $t->assertFirstElement(function($e) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertFirstElement(function ($e) { $e->assertTitle('First'); }); - $t->assertLastElement(function($e) { + $t->assertLastElement(function ($e) { $e->assertTitle('Forth'); }); }); @@ -638,7 +637,7 @@ public function it_can_test_template_first_and_last_element() /** @test */ public function it_can_test_template_specific_element_specific_button() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First') @@ -655,9 +654,9 @@ public function it_can_test_template_specific_element_specific_button() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { - $t->assertElement(0, function($e) { - $e->assertButton(0, function($b) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertButton(0, function ($b) { $b->assertTitle('First') ->assertType('web_url') ->assertUrl('www.botman.io') @@ -672,7 +671,7 @@ public function it_can_test_template_specific_element_specific_button() /** @test */ public function it_can_test_template_specific_element_first_and_last_button() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First')->addButtons([ @@ -686,12 +685,12 @@ public function it_can_test_template_specific_element_first_and_last_button() }); $this->tester->receives('generic'); - $this->tester->assertTemplate(GenericTemplate::class, function($t) { - $t->assertElement(0, function($e) { - $e->assertFirstButton(function($b) { + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertFirstButton(function ($b) { $b->assertTitle('First'); }); - $e->assertLastButton(function($b) { + $e->assertLastButton(function ($b) { $b->assertTitle('Third'); }); }); From 965ec7b9eac618bc8c32e5db7aa0de467e5c8057 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 14:53:03 +0300 Subject: [PATCH 11/14] more StyleCI changes --- src/Testing/BotManTester.php | 2 +- tests/BotManTesterTest.php | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 58b7d6b..7df6a86 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -350,7 +350,7 @@ public function assertTemplate($template, $closure = null) PHPUnit::assertInstanceOf($template, $message); - if( is_callable($closure)) { + if (is_callable($closure)) { call_user_func($closure, new TemplateTester($message)); } diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 1ed8748..7b87e16 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -5,11 +5,9 @@ use Mockery as m; use BotMan\BotMan\BotMan; use PHPUnit\Framework\TestCase; - use BotMan\BotMan\BotManFactory; use BotMan\Studio\Testing\BotManTester; use BotMan\BotMan\Drivers\Tests\FakeDriver; - use BotMan\BotMan\Messages\Attachments\Image; use BotMan\BotMan\Messages\Attachments\Video; use BotMan\BotMan\Messages\Outgoing\Question; @@ -396,11 +394,11 @@ public function it_can_test_question_specific_button() $this->tester->receives('message'); $this->tester->assertQuestion(null, function ($q) { - $q->assertButton(0, function($b) { + $q->assertButton(0, function ($b) { $b->assertText('First'); $b->assertValue('first'); }); - $q->assertButton(1, function($b) { + $q->assertButton(1, function ($b) { $b->assertText('Second'); $b->assertValue('second'); }); @@ -423,11 +421,11 @@ public function it_can_test_question_specific_button_is_not() $this->tester->receives('message'); $this->tester->assertQuestion(null, function ($q) { - $q->assertButton(0, function($b) { + $q->assertButton(0, function ($b) { $b->assertTextisNot('Second'); $b->assertValueisNot('second'); }); - $q->assertButton(1, function($b) { + $q->assertButton(1, function ($b) { $b->assertTextisNot('First'); $b->assertValueisNot('first'); }); @@ -452,7 +450,7 @@ public function it_can_test_question_first_and_last_button() $this->tester->receives('message'); $this->tester->assertQuestion(null, function ($q) { - $q->assertFirstButton( function ($b) { + $q->assertFirstButton(function ($b) { $b->assertText('First'); }); $q->assertLastButton(function ($b) { @@ -464,7 +462,7 @@ public function it_can_test_question_first_and_last_button() /** @test */ public function it_can_test_template() { - $this->botman->hears('generic', function ($bot){ + $this->botman->hears('generic', function ($bot) { $bot->reply(GenericTemplate::create() ->addElement(Element::create('title')) ); @@ -477,7 +475,7 @@ public function it_can_test_template() /** @test */ public function it_can_test_template_with_closure() { - $this->botman->hears('button', function ($bot){ + $this->botman->hears('button', function ($bot) { $bot->reply(ButtonTemplate::create('text') ->addButton(ElementButton::create('First')->type('web_url')->url('www.botman.io')) ); @@ -547,7 +545,7 @@ public function it_can_test_template_element_count() $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First'), - Element::create('Second') + Element::create('Second'), ]) ); }); @@ -565,7 +563,7 @@ public function it_can_test_template_specific_element() $bot->reply(GenericTemplate::create() ->addElements([ Element::create('First')->subtitle('This number is before "2"')->image('www.one.com/image'), - Element::create('Second') + Element::create('Second'), ]) ); }); @@ -588,7 +586,7 @@ public function it_can_test_template_nested_attributes() ->recipientName('Marcel') ->addElements([ ReceiptElement::create('First')->price(1)->currency('EUR'), - ReceiptElement::create('Second')->price(2)->currency('EUR') + ReceiptElement::create('Second')->price(2)->currency('EUR'), ]) ->addAddress(ReceiptAddress::create()->city('Barcelona')) ->addSummary(ReceiptSummary::create()->totalCost(3)) @@ -648,7 +646,7 @@ public function it_can_test_template_specific_element_specific_button() ->heightRatio(ElementButton::RATIO_COMPACT) ->fallbackUrl('www.botman.io/fallback') ), - Element::create('Second') + Element::create('Second'), ]) ); }); @@ -679,7 +677,7 @@ public function it_can_test_template_specific_element_first_and_last_button() ElementButton::create('Second')->type('web_url')->url('www.botman.io'), ElementButton::create('Third')->type('web_url')->url('www.botman.io'), ]), - Element::create('Second') + Element::create('Second'), ]) ); }); From d1ed04cd0a4781eb0457cedca32398929639628e Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 15:07:24 +0300 Subject: [PATCH 12/14] Fixed TravisCI issue --- tests/BotManTesterTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 7b87e16..78deab3 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -10,6 +10,8 @@ use BotMan\BotMan\Drivers\Tests\FakeDriver; use BotMan\BotMan\Messages\Attachments\Image; use BotMan\BotMan\Messages\Attachments\Video; +use BotMan\BotMan\Messages\Attachments\Audio; +use BotMan\BotMan\Messages\Attachments\File; use BotMan\BotMan\Messages\Outgoing\Question; use BotMan\Drivers\Facebook\Extensions\Element; use BotMan\BotMan\Messages\Attachments\Location; From 6889f65f99bcfdfba22ee939f319762819bb1b8e Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sat, 28 Oct 2017 15:31:42 +0300 Subject: [PATCH 13/14] Added In and NotIn methods for Button, ElementButton, Element, Question --- src/Testing/BotManTester.php | 3 +- src/Testing/ButtonTester.php | 14 +++ src/Testing/ElementButtonTester.php | 14 +++ src/Testing/ElementTester.php | 28 +++++ src/Testing/QuestionTester.php | 14 +++ tests/BotManTesterTest.php | 164 ++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+), 1 deletion(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 7df6a86..191f545 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -320,6 +320,7 @@ public function assertReplyNothing() /** * @param null $text + * @param null $closure * @return $this */ public function assertQuestion($text = null, $closure = null) @@ -341,7 +342,7 @@ public function assertQuestion($text = null, $closure = null) /** * @param string $template - * @param bool $strict + * @param null $closure * @return $this */ public function assertTemplate($template, $closure = null) diff --git a/src/Testing/ButtonTester.php b/src/Testing/ButtonTester.php index 9329797..e63bc73 100644 --- a/src/Testing/ButtonTester.php +++ b/src/Testing/ButtonTester.php @@ -30,6 +30,20 @@ public function assertTextIsNot($text) return $this; } + public function assertTextIn(array $haystack) + { + PHPUnit::assertTrue(in_array($this->button['text'], $haystack)); + + return $this; + } + + public function assertTextNotIn(array $haystack) + { + PHPUnit::assertFalse(in_array($this->button['text'], $haystack)); + + return $this; + } + public function assertName($name) { PHPUnit::assertSame($name, $this->button['name']); diff --git a/src/Testing/ElementButtonTester.php b/src/Testing/ElementButtonTester.php index 0c71eed..0886996 100644 --- a/src/Testing/ElementButtonTester.php +++ b/src/Testing/ElementButtonTester.php @@ -32,6 +32,20 @@ public function assertTitleIsNot($title) return $this; } + public function assertTitleIn(array $haystack) + { + PHPUnit::assertTrue(in_array($this->button['title'], $haystack)); + + return $this; + } + + public function assertTitleNotIn(array $haystack) + { + PHPUnit::assertFalse(in_array($this->button['title'], $haystack)); + + return $this; + } + public function assertType($type) { PHPUnit::assertSame($type, $this->button['type']); diff --git a/src/Testing/ElementTester.php b/src/Testing/ElementTester.php index 1cc241d..ff2ebec 100644 --- a/src/Testing/ElementTester.php +++ b/src/Testing/ElementTester.php @@ -30,6 +30,20 @@ public function assertTitleIsNot($title) return $this; } + public function assertTitleIn(array $haystack) + { + PHPUnit::assertTrue(in_array($this->element['title'], $haystack)); + + return $this; + } + + public function assertTitleNotIn(array $haystack) + { + PHPUnit::assertFalse(in_array($this->element['title'], $haystack)); + + return $this; + } + public function assertSubtitle($subtitle) { PHPUnit::assertSame($subtitle, $this->element['subtitle']); @@ -44,6 +58,20 @@ public function assertSubtitleIsNot($subtitle) return $this; } + public function assertSubtitleIn(array $haystack) + { + PHPUnit::assertTrue(in_array($this->element['subtitle'], $haystack)); + + return $this; + } + + public function assertSubtitleNotIn(array $haystack) + { + PHPUnit::assertFalse(in_array($this->element['subtitle'], $haystack)); + + return $this; + } + public function assertImage($image_url) { PHPUnit::assertSame($image_url, $this->element['image_url']); diff --git a/src/Testing/QuestionTester.php b/src/Testing/QuestionTester.php index c1a2c06..19217e5 100644 --- a/src/Testing/QuestionTester.php +++ b/src/Testing/QuestionTester.php @@ -31,6 +31,20 @@ public function assertTextIsNot($text) return $this; } + public function assertTextIn(array $haystack) + { + PHPUnit::assertTrue(in_array($this->question['text'], $haystack)); + + return $this; + } + + public function assertTextNotIn(array $haystack) + { + PHPUnit::assertFalse(in_array($this->question['text'], $haystack)); + + return $this; + } + public function assertFallback($fallback) { PHPUnit::assertSame($fallback, $this->question['fallback']); diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 78deab3..42f1aec 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -339,6 +339,36 @@ public function it_can_test_question_with_closure() $this->tester->assertReply('success'); } + /** @test */ + public function it_can_test_question_text_in() + { + $this->botman->hears('message', function ($bot) { + $bot->ask(Question::create('question'), function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function ($q) { + $q->assertTextIn(['question', 'Another question']); + }); + } + + /** @test */ + public function it_can_test_question_text_not_in() + { + $this->botman->hears('message', function ($bot) { + $bot->ask(Question::create('question'), function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function ($q) { + $q->assertTextNotIn(['Another question', 'And another question']); + }); + } + /** @test */ public function it_can_test_question_callback_and_fallback() { @@ -434,6 +464,48 @@ public function it_can_test_question_specific_button_is_not() }); } + /** @test */ + public function it_can_test_question_specific_button_is_in() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function ($q) { + $q->assertButton(0, function ($b) { + $b->assertTextIn(['First', 'Number one']); + }); + }); + } + + /** @test */ + public function it_can_test_question_specific_button_is_not_in() + { + $question = Question::create('question') + ->addButtons([ + Button::create('First')->value('first'), + ]); + $this->botman->hears('message', function ($bot) use ($question) { + $bot->ask($question, function ($answer) { + $this->say('success'); + }); + }); + + $this->tester->receives('message'); + $this->tester->assertQuestion(null, function ($q) { + $q->assertButton(0, function ($b) { + $b->assertTextNotIn(['Second', 'Number two']); + }); + }); + } + /** @test */ public function it_can_test_question_first_and_last_button() { @@ -580,6 +652,48 @@ public function it_can_test_template_specific_element() }); } + /** @test */ + public function it_can_test_template_specific_element_title_and_subtitle_in() + { + $this->botman->hears('generic', function ($bot) { + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First')->subtitle('This number is before "2"')->image('www.one.com/image'), + Element::create('Second'), + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertTitleIn(['First', 'Number one']); + $e->assertSubtitleIn(['This number is before "2"', 'Next one is 2']); + }); + }); + } + + /** @test */ + public function it_can_test_template_specific_element_title_and_subtitle_not_in() + { + $this->botman->hears('generic', function ($bot) { + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First')->subtitle('This number is before "2"')->image('www.one.com/image'), + Element::create('Second'), + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertTitleNotIn(['Second', 'Number two']); + $e->assertSubtitleNotIn(['This number is before "3"', 'Next one is 3']); + }); + }); + } + /** @test */ public function it_can_test_template_nested_attributes() { @@ -668,6 +782,56 @@ public function it_can_test_template_specific_element_specific_button() }); } + /** @test */ + public function it_can_test_template_specific_element_specific_button_title_in() + { + $this->botman->hears('generic', function ($bot) { + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First') + ->addButton(ElementButton::create('First') + ->type('web_url') + ->url('www.botman.io') + ), + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertButton(0, function ($b) { + $b->assertTitleIn(['First', 'The one before 2']); + }); + }); + }); + } + + /** @test */ + public function it_can_test_template_specific_element_specific_button_title_not_in() + { + $this->botman->hears('generic', function ($bot) { + $bot->reply(GenericTemplate::create() + ->addElements([ + Element::create('First') + ->addButton(ElementButton::create('First') + ->type('web_url') + ->url('www.botman.io') + ), + ]) + ); + }); + + $this->tester->receives('generic'); + $this->tester->assertTemplate(GenericTemplate::class, function ($t) { + $t->assertElement(0, function ($e) { + $e->assertButton(0, function ($b) { + $b->assertTitleNotIn(['Second', 'The one before 3']); + }); + }); + }); + } + /** @test */ public function it_can_test_template_specific_element_first_and_last_button() { From 97fc8dd0ec86a1d6948d10216d5bd0ddb18ecb82 Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Sun, 29 Oct 2017 00:45:42 +0300 Subject: [PATCH 14/14] added assertButtonCount to ElementTester --- src/Testing/ElementTester.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Testing/ElementTester.php b/src/Testing/ElementTester.php index ff2ebec..663e312 100644 --- a/src/Testing/ElementTester.php +++ b/src/Testing/ElementTester.php @@ -86,6 +86,13 @@ public function assertImageIsNot($image_url) return $this; } + public function assertButtonCount($count) + { + PHPUnit::assertCount($count, $this->element['buttons']); + + return $this; + } + public function assertButton($index, $closure) { $button = $this->element['buttons'][$index];