Skip to content

Commit

Permalink
Merge pull request #18 from christophrumpel/feature/addTemplateAssert…
Browse files Browse the repository at this point in the history
…ions

Add new template-in assertions and tests
  • Loading branch information
Christoph Rumpel authored Nov 7, 2017
2 parents c0e2152 + 46b1ff0 commit 99104b3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .discovery/Discovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php


namespace TheCodingMachine\Discovery;

/**
* Use this class to access data stored in the discovery.json files at the root of your packages.
*/
class Discovery implements DiscoveryInterface
{
private static $instance;

/**
* @var string[]
*/
private $values;
/**
* @var AssetType[]
*/
private $assetTypes;
/**
* @var array[]
*/
private $assetTypesArray;

/**
* Singleton. Noone creates this object directly.
*/
private function __construct()
{
$this->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];
}
}
3 changes: 3 additions & 0 deletions .discovery/discovery_asset_types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
return array (
);
3 changes: 3 additions & 0 deletions .discovery/discovery_values.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
return array (
);
24 changes: 24 additions & 0 deletions src/Testing/BotManTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,30 @@ public function assertTemplate($template, $strict = false)
return $this;
}

/**
* @param array $templates
* @return $this
*/
public function assertTemplateIn(array $templates)
{
$message = $this->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
Expand Down
66 changes: 66 additions & 0 deletions tests/BotManTesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 99104b3

Please sign in to comment.