From 354351e5fc562656a91e5abfb5cac5903699cb83 Mon Sep 17 00:00:00 2001 From: Chimit Date: Mon, 16 Nov 2020 14:38:27 +0800 Subject: [PATCH 1/3] Added channel setter for tests Currently, there is only one way to set recipient (channel) in tests by instantiating the `IncomingMessage` class: ```php $incoming1 = new IncomingMessage('hi', $sender, $recipient, ['foo' => 'bar']); $incoming2 = new IncomingMessage('good', $sender, $recipient, ['foo' => 'bar']); $this->bot ->setUser($sender) ->receivesRaw($incoming1) ->assertQuestion('How are you?') ->receivesRaw($incoming2); ``` This PR adds an ability to make it easier through a setter: ```php $this->bot ->setUser($sender) ->setChannel($channel) ->receives('hi', ['foo' => 'bar']) ->assertQuestion('How are you?') ->receives('good', ['foo' => 'bar']); ``` It's really useful in conversations. Otherwise, you should instantiate `IncomingMessage` for every message in a conversation. --- src/Testing/BotManTester.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 7c685da..5a00ef0 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -92,6 +92,17 @@ public function setUser($user_info) return $this; } + /** + * @param string $channel + * @return $this + */ + public function setChannel($channel) + { + $this->channel = $channel ?? $this->channel; + + return $this; + } + /** * @param IncomingMessage $message * @return $this From 787c30769f81b75ae2cf2962b216d35cf6cb38c5 Mon Sep 17 00:00:00 2001 From: Chimit Date: Mon, 16 Nov 2020 15:06:54 +0800 Subject: [PATCH 2/3] Added tests for setChannel --- tests/BotManTesterTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index cf3b391..1f6d437 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -95,6 +95,21 @@ public function it_can_fake_user_information() $this->tester->assertReply('User: marcelpociot'); } + /** @test */ + public function it_can_fake_channel() + { + $this->botman->hears('message', function ($bot) { + $bot->reply('Recipient: ' . $bot->getMessage()->getRecipient()); + }); + + $this->tester->setChannel(123); + $this->tester->receives('message'); + $messages = $this->tester->getMessages(); + + $this->assertCount(1, $messages); + $this->tester->assertReply('Recipient: 123'); + } + /** @test */ public function it_can_assert_replies() { From a454ec0be4de651e52992faa9101eaeeb3fe4dc4 Mon Sep 17 00:00:00 2001 From: Chimit Date: Mon, 16 Nov 2020 15:09:52 +0800 Subject: [PATCH 3/3] Styling --- tests/BotManTesterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 1f6d437..d436f80 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -99,7 +99,7 @@ public function it_can_fake_user_information() public function it_can_fake_channel() { $this->botman->hears('message', function ($bot) { - $bot->reply('Recipient: ' . $bot->getMessage()->getRecipient()); + $bot->reply('Recipient: '.$bot->getMessage()->getRecipient()); }); $this->tester->setChannel(123);