Skip to content

Commit

Permalink
Merge branch 'KarlisJ-new_testing_methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 27, 2017
2 parents 91d611b + 75af49c commit f7cf5a6
Showing 1 changed file with 135 additions and 15 deletions.
150 changes: 135 additions & 15 deletions src/Testing/BotManTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
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\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\BotMan\Messages\Attachments\Location;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;

Expand All @@ -21,7 +27,7 @@ class BotManTester
private $driver;

/** @var string */
private $username = 'botman';
private $user_id = '1';

/** @var string */
private $channel = '#botman';
Expand Down Expand Up @@ -58,8 +64,22 @@ 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;
}

Expand All @@ -69,7 +89,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();
Expand All @@ -78,26 +107,117 @@ 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)
{
return $this->receives($value);
$this->driver->isInteractiveMessageReply = true;

return $this->receives($message);
}

/**
* @param $message
* @return BotManTester
* @param $latitude
* @param $longitude
* @return $this
*/
public function receivesInteractiveMessage($message)
public function receivesLocation($latitude = 24, $longitude = 57)
{
$this->driver->isInteractiveMessageReply = true;
$message = new IncomingMessage(Location::PATTERN, $this->user_id, $this->channel);
$message->setLocation(new Location($latitude, $longitude, null));

return $this->receives($message);
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')];
} else {
$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)
{
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) {
return new Audio(($url));
})->toArray();
}
$message = new IncomingMessage(Audio::PATTERN, $this->user_id, $this->channel);
$message->setAudio($audio);

return $this->receivesRaw($message);
}

/**
* @param array|null $urls
* @return $this
*/
public function receivesVideos(array $urls = null)
{
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();
}
$message = new IncomingMessage(Video::PATTERN, $this->user_id, $this->channel);
$message->setVideos($videos);

return $this->receivesRaw($message);
}

/**
* @param array|null $urls
* @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
} else {
$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);

return $this->receives(new IncomingMessage('', $this->user_id, $this->channel));
}

/**
Expand All @@ -107,7 +227,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());
}
Expand Down

0 comments on commit f7cf5a6

Please sign in to comment.