diff --git a/.discovery/Discovery.php b/.discovery/Discovery.php new file mode 100644 index 0000000..db92a7c --- /dev/null +++ b/.discovery/Discovery.php @@ -0,0 +1,80 @@ +values = require __DIR__.'/discovery_values.php'; + $this->assetTypesArray = require __DIR__.'/discovery_asset_types.php'; + } + + /** + * Returns the unique instance of this class (singleton). + * + * @return self + */ + public static function getInstance(): self + { + if (!self::$instance) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Returns the asset values of the requested type. + * + * If no assets are found, an empty array is returned. + * + * @param string $assetType + * @return string[] + */ + public function get(string $assetType) : array + { + return $this->values[$assetType] ?? []; + } + + /** + * Returns an asset type object for the requested type. + * + * If no assets are found, an AssetType object containing no assets is returned. + * + * @param string $assetType + * @return AssetTypeInterface + */ + public function getAssetType(string $assetType) : AssetTypeInterface + { + if (!isset($this->assetTypes[$assetType])) { + if (isset($this->assetTypesArray[$assetType])) { + $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, $this->assetTypesArray[$assetType]); + } else { + $this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, []); + } + } + return $this->assetTypes[$assetType]; + } +} diff --git a/.discovery/discovery_asset_types.php b/.discovery/discovery_asset_types.php new file mode 100644 index 0000000..aa85dce --- /dev/null +++ b/.discovery/discovery_asset_types.php @@ -0,0 +1,3 @@ +getReply(); + PHPUnit::assertTrue(in_array($message, $templates)); + + return $this; + } + + /** + * @param array $templates + * @return $this + */ + public function assertTemplateNotIn(array $templates) + { + $message = $this->getReply(); + PHPUnit::assertFalse(in_array($message, $templates)); + + return $this; + } + /** * @param OutgoingMessage $message * @return $this diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 7725e9e..85e69b8 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -16,6 +16,16 @@ use BotMan\BotMan\Messages\Attachments\Location; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; +class TemplateFake +{ + public $text; + + public function __construct($text) + { + $this->text = $text; + } +} + class BotManTesterTest extends TestCase { /** @var BotManTester */ @@ -182,6 +192,62 @@ public function it_can_assert_multiple_replies() ]); } + /** @test */ + public function it_can_assert_a_template_class() + { + $this->botman->hears('message', function ($bot) { + $bot->reply(new TemplateFake('my message')); + }); + + $this->tester->receives('message'); + $this->tester->assertTemplate(TemplateFake::class); + } + + /** @test */ + public function it_can_assert_a_template_object() + { + $this->botman->hears('message', function ($bot) { + $bot->reply(new TemplateFake('my message')); + }); + + $this->tester->receives('message'); + $this->tester->assertTemplate(new TemplateFake('my message'), true); + } + + /** @test */ + public function it_can_assert_a_template_is_in_an_array() + { + $this->botman->hears('message', function ($bot) { + $bot->reply(new TemplateFake('message1')); + }); + + $templates = [ + new TemplateFake('message1'), + new TemplateFake('message2'), + new TemplateFake('message3'), + ]; + + $this->tester->receives('message'); + $this->tester->assertTemplateIn($templates); + } + + /** @test */ + public function it_can_assert_a_template_is_not_in_an_array() + { + $this->botman->hears('message', function ($bot) { + $bot->reply(new TemplateFake('message4')); + }); + + $templates = [ + new TemplateFake('message1'), + new TemplateFake('message2'), + new TemplateFake('message3'), + ]; + + $this->tester->receives('message'); + $this->tester->assertTemplateNotIn($templates); + } + /** @test */ public function it_can_fake_interactive_messages() {