Skip to content

Commit 18bd6ed

Browse files
committed
Add invalid action to error message.
Add random test to verify valid action.
1 parent ce22f41 commit 18bd6ed

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/Action.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct($action = 'handle')
3939
$this->action = $action ?: 'handle';
4040

4141
if (!$this->isAction(self::$valid_actions)) {
42-
throw new \InvalidArgumentException('Invalid action');
42+
throw new \InvalidArgumentException('Invalid action: ' . $this->action);
4343
}
4444
}
4545

@@ -64,4 +64,14 @@ public function getAction(): string
6464
{
6565
return $this->action;
6666
}
67+
68+
/**
69+
* Return a list of valid actions.
70+
*
71+
* @return array
72+
*/
73+
public static function getValidActions(): array
74+
{
75+
return self::$valid_actions;
76+
}
6777
}

tests/TelegramBotManager/Tests/ActionTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414

1515
class ActionTest extends \PHPUnit_Framework_TestCase
1616
{
17-
/**
18-
* @expectedException \InvalidArgumentException
19-
* @expectedExceptionMessage Invalid action
20-
*/
2117
public function testConstruct()
2218
{
2319
self::assertEquals('set', (new Action('set'))->getAction());
2420
self::assertEquals('unset', (new Action('unset'))->getAction());
2521
self::assertEquals('reset', (new Action('reset'))->getAction());
2622
self::assertEquals('handle', (new Action('handle'))->getAction());
23+
}
24+
25+
/**
26+
* @expectedException \InvalidArgumentException
27+
* @expectedExceptionMessageRegExp /^Invalid action: non-existent$/
28+
*/
29+
public function testConstructFail()
30+
{
2731
new Action('non-existent');
2832
}
2933

@@ -35,6 +39,12 @@ public function testIsAction()
3539
self::assertFalse($action->isAction('unset'));
3640
self::assertFalse($action->isAction(['unset', 'reset']));
3741

42+
// Random action.
43+
$valid_actions = Action::getValidActions();
44+
$random_action = $valid_actions[array_rand($valid_actions)];
45+
$action = new Action($random_action);
46+
self::assertTrue($action->isAction(Action::getValidActions()));
47+
3848
// Test some weird values.
3949
self::assertFalse($action->isAction(null));
4050
self::assertFalse($action->isAction(true));

0 commit comments

Comments
 (0)