Skip to content

Commit

Permalink
New method to access stored conversation questions
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 24, 2017
1 parent 6cc38f4 commit c8915c6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Traits/HandlesConversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public function getStoredConversation($message = null)
return $conversation;
}

/**
* Get the question that was asked in the currently stored conversation
* for a given message.
*
* @param null|IncomingMessage $message
* @return string|Question
*/
public function getStoredConversationQuestion($message = null)
{
$conversation = $this->getStoredConversation($message);
return unserialize($conversation['question']);
}

/**
* Remove a stored conversation array from the cache for a given message.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/BotManMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public function it_calls_captured_middleware()
$this->assertSame('My Answer', $_SERVER['middleware_captured']);
}

/** @test */
public function it_has_access_to_previous_question()
{
$this->botman->middleware->captured(new TestCustomMiddleware());

$this->botman->hears('Foo', function ($bot) {
$bot->ask('My Question', function ($answer) {
});
});

$this->replyWithFakeMessage('Foo');

$this->replyWithFakeMessage('My Answer');
$this->assertSame('My Question', $_SERVER['middleware_captured_question']);
}

/** @test */
public function it_calls_global_matching_middleware()
{
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/TestCustomMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BotMan\BotMan\Tests\Fixtures;

use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Interfaces\MiddlewareInterface;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;

Expand All @@ -20,6 +21,10 @@ class TestCustomMiddleware implements MiddlewareInterface
public function captured(IncomingMessage $message, $next, BotMan $bot)
{
$_SERVER['middleware_captured'] = $message->getText();
$conversation = $bot->getStoredConversation();
/** @var Question $question */
$question = unserialize($conversation['question']);
$_SERVER['middleware_captured_question'] = $question;

return $next($message);
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/BotManTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace BotMan\BotMan\tests\Unit;

use Mockery as m;
use BotMan\BotMan\BotMan;
use PHPUnit_Framework_TestCase;
use BotMan\BotMan\BotManFactory;
use Illuminate\Support\Collection;
use BotMan\BotMan\Cache\ArrayCache;
use BotMan\BotMan\Drivers\Tests\FakeDriver;
use BotMan\BotMan\Tests\Fixtures\TestConversation;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;

class BotManTest extends PHPUnit_Framework_TestCase
{

protected $cache;

public function tearDown()
{
m::close();
}

public function setUp()
{
parent::setUp();
$this->cache = new ArrayCache();
}

/**
* @param $data
* @return BotMan
*/
protected function getBot($data)
{
$botman = BotManFactory::create([], $this->cache);

$data = Collection::make($data);
/** @var FakeDriver $driver */
$driver = m::mock(FakeDriver::class)->makePartial();

$driver->isBot = $data->get('is_from_bot', false);
$driver->messages = [new IncomingMessage($data->get('message'), $data->get('sender'), $data->get('recipient'))];

$botman->setDriver($driver);

return $botman;
}

/** @test */
public function it_can_return_stored_questions()
{
$botman = $this->getBot([]);

$botman->storeConversation(new TestConversation(), function (){}, 'This is my question');

$this->assertSame('This is my question', $botman->getStoredConversationQuestion());
}
}

0 comments on commit c8915c6

Please sign in to comment.