Skip to content

Commit

Permalink
Add PSR-11 ContainerInterface (botman#713) (botman#714)
Browse files Browse the repository at this point in the history
* Add PSR-11 ContainerInterface (botman#713)

* fix cs
  • Loading branch information
kubk authored and mpociot committed Feb 19, 2018
1 parent 021f1e5 commit bdc39b2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"opis/closure": "^2.3",
"mpociot/pipeline": "^1.0",
"react/socket": "~0.4",
"spatie/macroable": "^1.0"
"spatie/macroable": "^1.0",
"psr/container": "^1.0"
},
"require-dev": {
"codeigniter/framework": "~3.0",
Expand Down
18 changes: 17 additions & 1 deletion src/BotMan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use BotMan\BotMan\Commands\Command;
use BotMan\BotMan\Messages\Matcher;
use Psr\Container\ContainerInterface;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\BotMan\Traits\ProvidesStorage;
use BotMan\BotMan\Interfaces\UserInterface;
Expand All @@ -19,6 +20,7 @@
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Attachments\Video;
use BotMan\BotMan\Messages\Outgoing\Question;
use Psr\Container\NotFoundExceptionInterface;
use BotMan\BotMan\Interfaces\StorageInterface;
use BotMan\BotMan\Traits\HandlesConversations;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -98,6 +100,9 @@ class BotMan
/** @var CacheInterface */
private $cache;

/** @var ContainerInterface */
protected $container;

/** @var StorageInterface */
protected $storage;

Expand Down Expand Up @@ -167,6 +172,14 @@ public function getDriver()
return $this->driver;
}

/**
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}

/**
* Retrieve the chat message.
*
Expand Down Expand Up @@ -630,6 +643,7 @@ protected function makeInvokableAction($action)
* @param $callback
* @return array|string|Closure
* @throws UnexpectedValueException
* @throws NotFoundExceptionInterface
*/
protected function getCallable($callback)
{
Expand All @@ -647,7 +661,9 @@ protected function getCallable($callback)

list($class, $method) = explode('@', $callback);

return [new $class($this), $method];
$command = $this->container ? $this->container->get($class) : new $class($this);

return [$command, $method];
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/BotManTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Collection;
use BotMan\BotMan\Cache\ArrayCache;
use BotMan\BotMan\Drivers\NullDriver;
use Psr\Container\ContainerInterface;
use BotMan\BotMan\Drivers\DriverManager;
use BotMan\BotMan\Drivers\Tests\FakeDriver;
use BotMan\BotMan\Interfaces\UserInterface;
Expand All @@ -19,6 +20,7 @@
use BotMan\BotMan\Messages\Attachments\Audio;
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Attachments\Video;
use Psr\Container\NotFoundExceptionInterface;
use BotMan\BotMan\Tests\Fixtures\TestFallback;
use BotMan\BotMan\Middleware\MiddlewareManager;
use BotMan\BotMan\Messages\Attachments\Location;
Expand Down Expand Up @@ -291,6 +293,56 @@ public function it_hears_matching_commands_without_closures()
$this->assertTrue(TestClass::$called);
}

/** @test */
public function it_hears_matching_commands_with_container()
{
$botman = $this->getBot([
'sender' => 'UX12345',
'recipient' => 'general',
'message' => 'Foo',
]);
TestClass::$called = false;

/** @var ContainerInterface|m\Mock $containerMock */
$containerMock = m::mock(ContainerInterface::class);
$containerMock->shouldReceive('get')
->with(TestClass::class)
->once()
->andReturn(new TestClass($botman));

$botman->setContainer($containerMock);

$botman->hears('foo', TestClass::class.'@foo');
$botman->listen();
$this->assertTrue(TestClass::$called);
}

/** @test */
public function it_throws_not_found_exception_when_command_is_not_registered_in_container()
{
$botman = $this->getBot([
'sender' => 'UX12345',
'recipient' => 'general',
'message' => 'Foo',
]);
TestClass::$called = false;

/** @var ContainerInterface|m\Mock $containerMock */
$containerMock = m::mock(ContainerInterface::class);
$exceptionMock = new class() extends \Exception implements NotFoundExceptionInterface {
};
$containerMock->shouldReceive('get')->once()->andThrow($exceptionMock);

$botman->setContainer($containerMock);

$botman->hears('foo', TestClass::class.'@foo');

$this->expectException(NotFoundExceptionInterface::class);

$botman->listen();
$this->assertFalse(TestClass::$called);
}

/** @test */
public function it_uses_invoke_method()
{
Expand Down

0 comments on commit bdc39b2

Please sign in to comment.