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 f08c989..191f545 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -320,9 +320,10 @@ public function assertReplyNothing() /** * @param null $text + * @param null $closure * @return $this */ - public function assertQuestion($text = null) + public function assertQuestion($text = null, $closure = null) { /** @var Question $question */ $question = $this->getReply(); @@ -332,22 +333,26 @@ public function assertQuestion($text = null) PHPUnit::assertSame($text, $question->getText()); } + if (! is_null($closure)) { + call_user_func($closure, new QuestionTester($question)); + } + return $this; } /** * @param string $template - * @param bool $strict + * @param null $closure * @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/ButtonTester.php b/src/Testing/ButtonTester.php new file mode 100644 index 0000000..e63bc73 --- /dev/null +++ b/src/Testing/ButtonTester.php @@ -0,0 +1,102 @@ +button = $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 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']); + + 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/ElementButtonTester.php b/src/Testing/ElementButtonTester.php new file mode 100644 index 0000000..0886996 --- /dev/null +++ b/src/Testing/ElementButtonTester.php @@ -0,0 +1,119 @@ +button = $button; + + return $this; + } + + public function assertTitle($title) + { + PHPUnit::assertSame($title, $this->button['title']); + + return $this; + } + + public function assertTitleIsNot($title) + { + PHPUnit::assertNotSame($title, $this->button['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']); + + return $this; + } + + public function assertTypeIsNot($type) + { + PHPUnit::assertNotSame($type, $this->button['type']); + + return $this; + } + + public function assertUrl($url) + { + PHPUnit::assertSame($url, $this->button['url']); + + return $this; + } + + public function assertUrlIsNot($url) + { + PHPUnit::assertNotSame($url, $this->button['url']); + + return $this; + } + + public function assertPayload($payload) + { + PHPUnit::assertSame($payload, $this->button['payload']); + + return $this; + } + + public function assertPayloadIsNot($payload) + { + PHPUnit::assertNotSame($payload, $this->button['payload']); + + return $this; + } + + public function assertHeightRatio($webview_height_ratio) + { + PHPUnit::assertSame($webview_height_ratio, $this->button['webview_height_ratio']); + + return $this; + } + + public function assertMessengerExtension($messenger_extensions = true) + { + PHPUnit::assertSame($messenger_extensions, $this->button['messenger_extensions']); + + 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 new file mode 100644 index 0000000..663e312 --- /dev/null +++ b/src/Testing/ElementTester.php @@ -0,0 +1,115 @@ +element = $element; + } + + public function assertTitle($title) + { + PHPUnit::assertSame($title, $this->element['title']); + + return $this; + } + + public function assertTitleIsNot($title) + { + PHPUnit::assertNotSame($title, $this->element['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']); + + return $this; + } + + public function assertSubtitleIsNot($subtitle) + { + PHPUnit::assertNotSame($subtitle, $this->element['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']); + + return $this; + } + + public function assertImageIsNot($image_url) + { + PHPUnit::assertNotSame($image_url, $this->element['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]; + call_user_func($closure, new ElementButtonTester($button)); + + return $this; + } + + public function assertFirstButton($closure) + { + return $this->assertButton(0, $closure); + } + + public function assertLastButton($closure) + { + $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 new file mode 100644 index 0000000..19217e5 --- /dev/null +++ b/src/Testing/QuestionTester.php @@ -0,0 +1,102 @@ +question = $question->toArray(); + } + + 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 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']); + + 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; + } + + 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 new file mode 100644 index 0000000..f7ce59c --- /dev/null +++ b/src/Testing/TemplateTester.php @@ -0,0 +1,97 @@ +payload = $template->toArray()['attachment']['payload']; + } + + public function assertText($text) + { + PHPUnit::assertSame($text, $this->payload['text']); + + return $this; + } + + public function assertTextIsNot($text) + { + PHPUnit::assertNotSame($text, $this->payload['text']); + + return $this; + } + +// public function assertSharable($sharable) +// { +// PHPUnit::assertSame($sharable, $this->payload['sharable']); +// +// return $this; +// } + + public function assertImageAspectRatio($image_aspect_ratio) + { + PHPUnit::assertSame($image_aspect_ratio, $this->payload['image_aspect_ratio']); + + return $this; + } + + public function assertTopElementStyle($top_element_style) + { + PHPUnit::assertSame($top_element_style, $this->payload['top_element_style']); + + return $this; + } + + public function assertButtons($closure) + { + $button = $this->payload['buttons'][0]; + call_user_func($closure, new ElementButtonTester($button)); + + return $this; + } + + public function assertElementCount($count) + { + PHPUnit::assertCount($count, $this->payload['elements']); + + return $this; + } + + public function assertElement($index, $closure) + { + $element = $this->payload['elements'][$index]; + call_user_func($closure, new ElementTester($element)); + + return $this; + } + + public function assertFirstElement($closure) + { + return $this->assertElement(0, $closure); + } + + public function assertLastElement($closure) + { + $last_index = count($this->payload['elements']) - 1; + + return $this->assertElement($last_index, $closure); + } + + public function assertAttributes($attributes) + { + foreach ($attributes as $key => $value) { + PHPUnit::assertSame($value, array_get($this->payload, $key)); + } + + return $this; + } +} \ No newline at end of file diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 7725e9e..42f1aec 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -8,13 +8,23 @@ 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\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; +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 { @@ -311,4 +321,543 @@ 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_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() + { + $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_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() + { + $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_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() + { + $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_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() + { + $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'); + }); + }); + }); + } }