Skip to content

Commit

Permalink
Fix for get reply (#10)
Browse files Browse the repository at this point in the history
* progress in evening, no testing

* botMessages are shifted in order

* added reply method

* Fixed StyleCI complains

* Fix for StyleCI
  • Loading branch information
KarlisJ authored and mpociot committed Oct 5, 2017
1 parent 4d9f244 commit f562b0b
Showing 1 changed file with 61 additions and 38 deletions.
99 changes: 61 additions & 38 deletions src/Testing/BotManTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class BotManTester
/** @var FakeDriver */
private $driver;

/** @var array */
private $botMessages = [];

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

Expand Down Expand Up @@ -55,9 +58,15 @@ protected function listen()
*/
protected function getReply()
{
$messages = $this->getMessages();
return array_shift($this->botMessages);
}

return array_pop($messages);
/**
* @return Question[]|string[]|Template[]
*/
public function getMessages()
{
return $this->driver->getBotMessages();
}

/**
Expand All @@ -83,15 +92,6 @@ public function setUser($user_info)
return $this;
}

/**
* @param $message
* @return $this
*/
public function receives($message)
{
return $this->receivesRaw(new IncomingMessage($message, $this->user_id, $this->channel));
}

/**
* @param IncomingMessage $message
* @return $this
Expand All @@ -103,11 +103,22 @@ public function receivesRaw($message)
$this->driver->resetBotMessages();
$this->listen();

$this->botMessages = $this->getMessages();

return $this;
}

/**
* @param $message
* @param string $message
* @return $this
*/
public function receives($message)
{
return $this->receivesRaw(new IncomingMessage($message, $this->user_id, $this->channel));
}

/**
* @param string $message
* @return BotManTester
*/
public function receivesInteractiveMessage($message)
Expand Down Expand Up @@ -195,7 +206,7 @@ public function receivesVideos(array $urls = null)
public function receivesFiles(array $urls = null)
{
if (is_null($urls)) {
$files = [new File('https://www.youtube.com/watch?v=4zzSw-0IShE')]; // TODO : default video
$files = [new File('https://www.youtube.com/watch?v=4zzSw-0IShE')];
} else {
$files = Collection::make($urls)->map(function ($url) {
return new File(($url));
Expand All @@ -217,7 +228,7 @@ public function receivesEvent($name, $payload = null)
$this->driver->setEventName($name);
$this->driver->setEventPayload($payload);

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

/**
Expand All @@ -226,10 +237,11 @@ public function receivesEvent($name, $payload = null)
*/
public function assertReply($message)
{
if ($this->getReply() instanceof OutgoingMessage) {
PHPUnit::assertSame($message, $this->getReply()->getText());
$reply = $this->getReply();
if ($reply instanceof OutgoingMessage) {
PHPUnit::assertSame($message, $reply->getText());
} else {
PHPUnit::assertEquals($message, $this->getReply());
PHPUnit::assertEquals($message, $reply);
}

return $this;
Expand Down Expand Up @@ -289,59 +301,70 @@ public function assertReplyNotIn(array $haystack)
return $this;
}

/**
* @return $this
*/
public function assertReplyNothing()
{
PHPUnit::assertNull($this->getReply());

return $this;
}

/**
* @param null $text
* @return $this
*/
public function assertQuestion($text = null)
{
$messages = $this->getMessages();

/** @var Question $question */
$question = array_pop($messages);
$question = $this->getReply();
PHPUnit::assertInstanceOf(Question::class, $question);

if (! is_null($text)) {
PHPUnit::assertSame($question->getText(), $text);
PHPUnit::assertSame($text, $question->getText());
}

return $this;
}

/**
* @return Question[]|\string[]
* @param string $template
* @param bool $strict
* @return $this
*/
public function getMessages()
public function assertTemplate(string $template, $strict = false)
{
return $this->driver->getBotMessages();
$message = $this->getReply();
PHPUnit::assertInstanceOf($template, $message);

if ($strict) {
PHPUnit::assertSame($template, $message);
}

return $this;
}

/**
* @param string $template
* @param OutgoingMessage $message
* @return $this
*/
public function assertTemplate(string $template)
public function assertRaw($message)
{
$messages = $this->getMessages();

/** @var Question $question */
$message = array_pop($messages);
PHPUnit::assertInstanceOf($template, $message);
PHPUnit::assertSame($message, $this->getReply());

return $this;
}

/**
* @param array $payload
* @param int $times
* @return $this
*/
public function assertPayload(array $payload)
public function reply($times = 1)
{
$messages = $this->getMessages();

/** @var Question $question */
$message = array_pop($messages);
PHPUnit::assertEquals($message->toArray(), $payload);
foreach (range(1, $times) as $time) {
$this->getReply();
}

return $this;
}
Expand Down

0 comments on commit f562b0b

Please sign in to comment.