Skip to content

Commit 99104b3

Browse files
author
Christoph Rumpel
authored
Merge pull request #18 from christophrumpel/feature/addTemplateAssertions
Add new template-in assertions and tests
2 parents c0e2152 + 46b1ff0 commit 99104b3

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

.discovery/Discovery.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\Discovery;
5+
6+
/**
7+
* Use this class to access data stored in the discovery.json files at the root of your packages.
8+
*/
9+
class Discovery implements DiscoveryInterface
10+
{
11+
private static $instance;
12+
13+
/**
14+
* @var string[]
15+
*/
16+
private $values;
17+
/**
18+
* @var AssetType[]
19+
*/
20+
private $assetTypes;
21+
/**
22+
* @var array[]
23+
*/
24+
private $assetTypesArray;
25+
26+
/**
27+
* Singleton. Noone creates this object directly.
28+
*/
29+
private function __construct()
30+
{
31+
$this->values = require __DIR__.'/discovery_values.php';
32+
$this->assetTypesArray = require __DIR__.'/discovery_asset_types.php';
33+
}
34+
35+
/**
36+
* Returns the unique instance of this class (singleton).
37+
*
38+
* @return self
39+
*/
40+
public static function getInstance(): self
41+
{
42+
if (!self::$instance) {
43+
self::$instance = new self();
44+
}
45+
return self::$instance;
46+
}
47+
48+
/**
49+
* Returns the asset values of the requested type.
50+
*
51+
* If no assets are found, an empty array is returned.
52+
*
53+
* @param string $assetType
54+
* @return string[]
55+
*/
56+
public function get(string $assetType) : array
57+
{
58+
return $this->values[$assetType] ?? [];
59+
}
60+
61+
/**
62+
* Returns an asset type object for the requested type.
63+
*
64+
* If no assets are found, an AssetType object containing no assets is returned.
65+
*
66+
* @param string $assetType
67+
* @return AssetTypeInterface
68+
*/
69+
public function getAssetType(string $assetType) : AssetTypeInterface
70+
{
71+
if (!isset($this->assetTypes[$assetType])) {
72+
if (isset($this->assetTypesArray[$assetType])) {
73+
$this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, $this->assetTypesArray[$assetType]);
74+
} else {
75+
$this->assetTypes[$assetType] = ImmutableAssetType::fromArray($assetType, []);
76+
}
77+
}
78+
return $this->assetTypes[$assetType];
79+
}
80+
}

.discovery/discovery_asset_types.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
return array (
3+
);

.discovery/discovery_values.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
return array (
3+
);

src/Testing/BotManTester.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,30 @@ public function assertTemplate($template, $strict = false)
354354
return $this;
355355
}
356356

357+
/**
358+
* @param array $templates
359+
* @return $this
360+
*/
361+
public function assertTemplateIn(array $templates)
362+
{
363+
$message = $this->getReply();
364+
PHPUnit::assertTrue(in_array($message, $templates));
365+
366+
return $this;
367+
}
368+
369+
/**
370+
* @param array $templates
371+
* @return $this
372+
*/
373+
public function assertTemplateNotIn(array $templates)
374+
{
375+
$message = $this->getReply();
376+
PHPUnit::assertFalse(in_array($message, $templates));
377+
378+
return $this;
379+
}
380+
357381
/**
358382
* @param OutgoingMessage $message
359383
* @return $this

tests/BotManTesterTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
use BotMan\BotMan\Messages\Attachments\Location;
1717
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
1818

19+
class TemplateFake
20+
{
21+
public $text;
22+
23+
public function __construct($text)
24+
{
25+
$this->text = $text;
26+
}
27+
}
28+
1929
class BotManTesterTest extends TestCase
2030
{
2131
/** @var BotManTester */
@@ -182,6 +192,62 @@ public function it_can_assert_multiple_replies()
182192
]);
183193
}
184194

195+
/** @test */
196+
public function it_can_assert_a_template_class()
197+
{
198+
$this->botman->hears('message', function ($bot) {
199+
$bot->reply(new TemplateFake('my message'));
200+
});
201+
202+
$this->tester->receives('message');
203+
$this->tester->assertTemplate(TemplateFake::class);
204+
}
205+
206+
/** @test */
207+
public function it_can_assert_a_template_object()
208+
{
209+
$this->botman->hears('message', function ($bot) {
210+
$bot->reply(new TemplateFake('my message'));
211+
});
212+
213+
$this->tester->receives('message');
214+
$this->tester->assertTemplate(new TemplateFake('my message'), true);
215+
}
216+
217+
/** @test */
218+
public function it_can_assert_a_template_is_in_an_array()
219+
{
220+
$this->botman->hears('message', function ($bot) {
221+
$bot->reply(new TemplateFake('message1'));
222+
});
223+
224+
$templates = [
225+
new TemplateFake('message1'),
226+
new TemplateFake('message2'),
227+
new TemplateFake('message3'),
228+
];
229+
230+
$this->tester->receives('message');
231+
$this->tester->assertTemplateIn($templates);
232+
}
233+
234+
/** @test */
235+
public function it_can_assert_a_template_is_not_in_an_array()
236+
{
237+
$this->botman->hears('message', function ($bot) {
238+
$bot->reply(new TemplateFake('message4'));
239+
});
240+
241+
$templates = [
242+
new TemplateFake('message1'),
243+
new TemplateFake('message2'),
244+
new TemplateFake('message3'),
245+
];
246+
247+
$this->tester->receives('message');
248+
$this->tester->assertTemplateNotIn($templates);
249+
}
250+
185251
/** @test */
186252
public function it_can_fake_interactive_messages()
187253
{

0 commit comments

Comments
 (0)