From 1e897d2fcdf18b1291726621e18ab839b1da603e Mon Sep 17 00:00:00 2001 From: Karlis Janisels Date: Wed, 27 Sep 2017 23:06:53 +0300 Subject: [PATCH 1/2] Added methods: setDriver, setUser, receivesEvent, receivesImages, receivesVideos, receivesAudio, receivesFiles, receivesLocation, receivesRaw --- src/Testing/BotManTester.php | 150 +++++++++++++++++++++++++++++++---- 1 file changed, 136 insertions(+), 14 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index 700ebe0..de62ee4 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -8,6 +8,13 @@ use BotMan\BotMan\Messages\Outgoing\Question; use BotMan\BotMan\Messages\Incoming\IncomingMessage; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; +use BotMan\BotMan\Messages\Attachments\Audio; +use BotMan\BotMan\Messages\Attachments\File; +use BotMan\BotMan\Messages\Attachments\Image; +use BotMan\BotMan\Messages\Attachments\Location; +use BotMan\BotMan\Messages\Attachments\Video; + +use Illuminate\Support\Collection; /** * Class BotManTester. @@ -21,7 +28,7 @@ class BotManTester private $driver; /** @var string */ - private $username = 'botman'; + private $user_id = '1'; /** @var string */ private $channel = '#botman'; @@ -58,8 +65,20 @@ protected function getReply() * @param $driver * @return $this */ - public function usingDriver($driver) + public function setDriver($driver) { + $this->driver->setName($driver::DRIVER_NAME); + return $this; + } + + /** + * @param Array $user_info + * @return $this + */ + public function setUser($user_info) + { + $this->user_id = $user_info['id'] ?? $this->user_id; + $this->driver->setUser($user_info); return $this; } @@ -69,7 +88,16 @@ public function usingDriver($driver) */ public function receives($message) { - $this->driver->messages = [new IncomingMessage($message, $this->username, $this->channel)]; + return $this->receivesRaw(new IncomingMessage($message, $this->user_id, $this->channel)); + } + + /** + * @param IncomingMessage $message + * @return $this + */ + public function receivesRaw($message) + { + $this->driver->messages = [$message]; $this->driver->resetBotMessages(); $this->listen(); @@ -78,26 +106,120 @@ public function receives($message) } /** - * Simulates that we listen for a value - * but we use the receives method instead. - * - * @param $value + * @param $message * @return BotManTester */ - public function receivesValue($value) + public function receivesInteractiveMessage($message) + { + $this->driver->isInteractiveMessageReply = true; + + return $this->receives($message); + } + + /** + * @param $latitude + * @param $longitude + * @return $this + */ + 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); + } + + /** + * @param Array $urls + * @return $this + */ + public function receivesImages(Array $urls = null) + { + if(is_null($urls)){ + $images = [new Image('https://via.placeholder.com/350x150')]; // TODO : default image + } + if(is_array($urls)){ + $images = Collection::make($urls)->map(function($url){ + return new Image(($url)); + })->toArray(); + } + $message = new IncomingMessage(Image::PATTERN, $this->user_id, $this->channel); + $message->setImages($images); + + return $this->receivesRaw($message); + } + + /** + * @param Array $urls + * @return $this + */ + public function receivesAudio(Array $urls = null) { - return $this->receives($value); + if(is_null($urls)){ + $audio = [new Audio('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default audio + } + if(is_array($urls)){ + $audio = Collection::make($urls)->map(function($url){ + return new Audio(($url)); + })->toArray(); + } + $message = new IncomingMessage(Audio::PATTERN, $this->user_id, $this->channel); + $message->setAudio($audio); + + return $this->receivesRaw($message); } /** * @param $message - * @return BotManTester + * @return $this */ - public function receivesInteractiveMessage($message) + public function receivesVideos(Array $urls = null) { - $this->driver->isInteractiveMessageReply = true; + if(is_null($urls)){ + $videos = [new Video('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default video + } + if(is_array($urls)){ + $videos = Collection::make($urls)->map(function($url){ + return new Video(($url)); + })->toArray(); + } + $message = new IncomingMessage(Video::PATTERN, $this->user_id, $this->channel); + $message->setVideos($videos); - return $this->receives($message); + return $this->receivesRaw($message); + } + + /** + * @param $message + * @return $this + */ + public function receivesFiles(Array $urls = null) + { + if(is_null($urls)){ + $files = [new File('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default video + } + if(is_array($urls)){ + $files = Collection::make($urls)->map(function($url){ + return new File(($url)); + })->toArray(); + } + $message = new IncomingMessage(File::PATTERN, $this->user_id, $this->channel); + $message->setFiles($files); + + return $this->receivesRaw($message); + } + + /** + * @param $name + * @param $payload + * @return $this + */ + public function receivesEvent($name, $payload = null) { + $this->driver->setEventName($name); + $this->driver->setEventPayload($payload); + //$this->driver->hasMatchingEvent(); + + return $this->receives(new IncomingMessage('', $this->user_id, $this->channel)); } /** @@ -107,7 +229,7 @@ public function receivesInteractiveMessage($message) public function assertReply($message) { if ($this->getReply() instanceof OutgoingMessage) { - PHPUnit::assertSame($this->getReply()->getText(), $message); + PHPUnit::assertSame($message, $this->getReply()->getText()); } else { PHPUnit::assertEquals($message, $this->getReply()); } From 75af49ca5727f0731b7b840963c6e69aefc662f6 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Wed, 27 Sep 2017 23:46:04 +0200 Subject: [PATCH 2/2] Modify docblock and style --- src/Testing/BotManTester.php | 72 ++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/Testing/BotManTester.php b/src/Testing/BotManTester.php index de62ee4..f1ae91a 100644 --- a/src/Testing/BotManTester.php +++ b/src/Testing/BotManTester.php @@ -3,18 +3,17 @@ namespace BotMan\Studio\Testing; use BotMan\BotMan\BotMan; +use Illuminate\Support\Collection; use PHPUnit\Framework\Assert as PHPUnit; use BotMan\BotMan\Drivers\Tests\FakeDriver; -use BotMan\BotMan\Messages\Outgoing\Question; -use BotMan\BotMan\Messages\Incoming\IncomingMessage; -use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; -use BotMan\BotMan\Messages\Attachments\Audio; use BotMan\BotMan\Messages\Attachments\File; +use BotMan\BotMan\Messages\Attachments\Audio; use BotMan\BotMan\Messages\Attachments\Image; -use BotMan\BotMan\Messages\Attachments\Location; use BotMan\BotMan\Messages\Attachments\Video; - -use Illuminate\Support\Collection; +use BotMan\BotMan\Messages\Outgoing\Question; +use BotMan\BotMan\Messages\Attachments\Location; +use BotMan\BotMan\Messages\Incoming\IncomingMessage; +use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; /** * Class BotManTester. @@ -68,17 +67,19 @@ protected function getReply() public function setDriver($driver) { $this->driver->setName($driver::DRIVER_NAME); + return $this; } /** - * @param Array $user_info + * @param array $user_info * @return $this */ public function setUser($user_info) { $this->user_id = $user_info['id'] ?? $this->user_id; $this->driver->setUser($user_info); + return $this; } @@ -124,22 +125,21 @@ public function receivesInteractiveMessage($message) public function receivesLocation($latitude = 24, $longitude = 57) { $message = new IncomingMessage(Location::PATTERN, $this->user_id, $this->channel); - $message->setLocation( new Location($latitude, $longitude, null)); + $message->setLocation(new Location($latitude, $longitude, null)); return $this->receivesRaw($message); } /** - * @param Array $urls + * @param array $urls * @return $this */ - public function receivesImages(Array $urls = null) + public function receivesImages(array $urls = null) { - if(is_null($urls)){ - $images = [new Image('https://via.placeholder.com/350x150')]; // TODO : default image - } - if(is_array($urls)){ - $images = Collection::make($urls)->map(function($url){ + if (is_null($urls)) { + $images = [new Image('https://via.placeholder.com/350x150')]; + } else { + $images = Collection::make($urls)->map(function ($url) { return new Image(($url)); })->toArray(); } @@ -150,16 +150,16 @@ public function receivesImages(Array $urls = null) } /** - * @param Array $urls + * @param array $urls * @return $this */ - public function receivesAudio(Array $urls = null) + public function receivesAudio(array $urls = null) { - if(is_null($urls)){ - $audio = [new Audio('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default audio + if (is_null($urls)) { + $audio = [new Audio('https://www.youtube.com/watch?v=4zzSw-0IShE')]; } - if(is_array($urls)){ - $audio = Collection::make($urls)->map(function($url){ + if (is_array($urls)) { + $audio = Collection::make($urls)->map(function ($url) { return new Audio(($url)); })->toArray(); } @@ -170,16 +170,15 @@ public function receivesAudio(Array $urls = null) } /** - * @param $message + * @param array|null $urls * @return $this */ - public function receivesVideos(Array $urls = null) + public function receivesVideos(array $urls = null) { - if(is_null($urls)){ - $videos = [new Video('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default video - } - if(is_array($urls)){ - $videos = Collection::make($urls)->map(function($url){ + if (is_null($urls)) { + $videos = [new Video('https://www.youtube.com/watch?v=4zzSw-0IShE')]; + } else { + $videos = Collection::make($urls)->map(function ($url) { return new Video(($url)); })->toArray(); } @@ -190,16 +189,15 @@ public function receivesVideos(Array $urls = null) } /** - * @param $message + * @param array|null $urls * @return $this */ - public function receivesFiles(Array $urls = null) + public function receivesFiles(array $urls = null) { - if(is_null($urls)){ + if (is_null($urls)) { $files = [new File('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default video - } - if(is_array($urls)){ - $files = Collection::make($urls)->map(function($url){ + } else { + $files = Collection::make($urls)->map(function ($url) { return new File(($url)); })->toArray(); } @@ -214,10 +212,10 @@ public function receivesFiles(Array $urls = null) * @param $payload * @return $this */ - public function receivesEvent($name, $payload = null) { + public function receivesEvent($name, $payload = null) + { $this->driver->setEventName($name); $this->driver->setEventPayload($payload); - //$this->driver->hasMatchingEvent(); return $this->receives(new IncomingMessage('', $this->user_id, $this->channel)); }