From 4d1088912c6ec65776ea64bc3625fef61810ce27 Mon Sep 17 00:00:00 2001 From: Christoph Rumpel Date: Sun, 5 Nov 2017 18:11:40 +0100 Subject: [PATCH 1/3] Add new template in assertions and tests --- .discovery/Discovery.php | 80 ++++++++++++++++++++++++++++ .discovery/discovery_asset_types.php | 3 ++ .discovery/discovery_values.php | 3 ++ src/Testing/BotManTester.php | 24 +++++++++ tests/BotManTesterTest.php | 66 +++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 .discovery/Discovery.php create mode 100644 .discovery/discovery_asset_types.php create mode 100644 .discovery/discovery_values.php 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..36548ca 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() { From 9d18c000439ba8d250586ed900d0941bb2a5219a Mon Sep 17 00:00:00 2001 From: Christoph Rumpel Date: Sun, 5 Nov 2017 18:15:19 +0100 Subject: [PATCH 2/3] Fix style --- tests/BotManTesterTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index 36548ca..cb422c2 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -16,11 +16,13 @@ use BotMan\BotMan\Messages\Attachments\Location; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; -class TemplateFake { +class TemplateFake +{ public $text; - public function __construct($text) { + public function __construct($text) + { $this->text = $text; } From 46b1ff001153bad597381fb78772217f41d61645 Mon Sep 17 00:00:00 2001 From: Christoph Rumpel Date: Sun, 5 Nov 2017 18:16:18 +0100 Subject: [PATCH 3/3] Fix style --- tests/BotManTesterTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/BotManTesterTest.php b/tests/BotManTesterTest.php index cb422c2..85e69b8 100644 --- a/tests/BotManTesterTest.php +++ b/tests/BotManTesterTest.php @@ -18,12 +18,10 @@ class TemplateFake { - public $text; public function __construct($text) { - $this->text = $text; } }