Skip to content

Commit

Permalink
Fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanlv committed Mar 28, 2024
1 parent 20a4028 commit 3038aa8
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 160 deletions.
3 changes: 2 additions & 1 deletion src/Command/GlobalCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/
class GlobalCommandExtension extends CommandExtension
{
public function __construct(?string $applicationId = null) {
public function __construct(?string $applicationId = null)
{
if (!is_null($applicationId)) {
trigger_error('Providing an application ID is no longer required and will be removed in a later version');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GuildCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class GuildCommandExtension extends CommandExtension
{
public function __construct(?string $applicationId = null, private readonly string $guildId)
public function __construct(?string $applicationId, private readonly string $guildId)
{
if (!is_null($applicationId)) {
trigger_error('Providing an application ID is no longer required and will be removed in a later version');
Expand Down
137 changes: 59 additions & 78 deletions tests/Command/GlobalCommandExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Tests\Ragnarok\Fenrir\Command;

use Fakes\Ragnarok\Fenrir\DiscordFake;
use Fakes\Ragnarok\Fenrir\PromiseFake;
use PHPUnit\Framework\TestCase;
use Ragnarok\Fenrir\Command\GlobalCommandExtension;
use Ragnarok\Fenrir\Constants\Events;
Expand All @@ -14,8 +13,7 @@
use Ragnarok\Fenrir\Enums\InteractionType;
use Ragnarok\Fenrir\Gateway\Events\InteractionCreate;
use Ragnarok\Fenrir\Interaction\CommandInteraction;
use Ragnarok\Fenrir\Parts\ApplicationCommand;
use Ragnarok\Fenrir\Parts\ApplicationCommandOptionStructure;
use Ragnarok\Fenrir\Parts\ApplicationCommandInteractionDataOptionStructure;
use Ragnarok\Fenrir\Parts\InteractionData;

class GlobalCommandExtensionTest extends TestCase
Expand All @@ -29,22 +27,10 @@ protected function setUp(): void

public function testItEmitsEventsForApplicationCommands()
{
$commands = [new ApplicationCommand(), new ApplicationCommand()];

$commands[0]->id = '::application command 1::';
$commands[0]->name = 'command-1';

$commands[1]->id = '::application command 2::';
$commands[1]->name = 'command-2';

$this->discord->rest->globalCommand->shouldReceive()
->getCommands('::application id::')
->andReturns(PromiseFake::get($commands));

$extension = new GlobalCommandExtension('::application id::');
$extension = new GlobalCommandExtension();
$extension->initialize($this->discord);

$hasRun = [false, false];
$hasRun = [false, false, false];

$extension->on('command-1', function (CommandInteraction $firedCommand) use (&$hasRun) {
$hasRun[0] = true;
Expand All @@ -54,17 +40,21 @@ public function testItEmitsEventsForApplicationCommands()
$hasRun[1] = true;
});

$extension->on('command-3', function (CommandInteraction $firedCommand) use (&$hasRun) {
$hasRun[2] = true;
});

$interaction = new InteractionCreate();
$interaction->type = InteractionType::APPLICATION_COMMAND;
$interaction->data = new InteractionData();
$interaction->data->id = '::application command 1::';
$interaction->data->name = 'command-1';

$this->discord->gateway->events->emit(
Events::INTERACTION_CREATE,
[$interaction]
);

$interaction->data->id = '::application command 2::';
$interaction->data->name = 'command-2';

$this->discord->gateway->events->emit(
Events::INTERACTION_CREATE,
Expand All @@ -73,20 +63,12 @@ public function testItEmitsEventsForApplicationCommands()

$this->assertTrue($hasRun[0], 'Command 1 did not run');
$this->assertTrue($hasRun[1], 'Command 2 did not run');
$this->assertFalse($hasRun[2], 'Command 3 should not have been run');
}

public function testItDoesNotEmitCommandIfDifferentInteractionOccured()
public function testItDoesNotEmitEventsForGuilds()
{
$command = new ApplicationCommand();

$command->id = '::application command::';
$command->name = 'command';

$this->discord->rest->globalCommand->shouldReceive()
->getCommands('::application id::')
->andReturns(PromiseFake::get([$command]));

$extension = new GlobalCommandExtension('::application id::');
$extension = new GlobalCommandExtension();
$extension->initialize($this->discord);

$hasRun = false;
Expand All @@ -96,31 +78,26 @@ public function testItDoesNotEmitCommandIfDifferentInteractionOccured()
});

$interaction = new InteractionCreate();
$interaction->type = InteractionType::PING;
$interaction->type = InteractionType::APPLICATION_COMMAND;
$interaction->data = new InteractionData();
$interaction->data->id = '::application command::';
$interaction->data->name = 'command';
$interaction->data->guild_id = '::guild id::';

$this->discord->gateway->events->emit(
Events::INTERACTION_CREATE,
[$interaction]
);

$this->assertFalse($hasRun, 'Command was emitted wrongfully');
$this->assertFalse($hasRun, 'Command 1 did not run');
}

/**
* @dataProvider nameMappingProvider
* @depends testItEmitsEventsForApplicationCommands
*/
public function testItMapsNamesCorrectly(ApplicationCommand $command, string $expectedName)
public function testItMapsNamesCorrectly(InteractionCreate $interaction, string $expectedName)
{
$command->id = '::application command::';

$this->discord->rest->globalCommand->shouldReceive()
->getCommands('::application id::')
->andReturns(PromiseFake::get([$command]));

$extension = new GlobalCommandExtension('::application id::');
$extension = new GlobalCommandExtension();
$extension->initialize($this->discord);

$hasRun = false;
Expand All @@ -129,10 +106,6 @@ public function testItMapsNamesCorrectly(ApplicationCommand $command, string $ex
$hasRun = true;
});

$interaction = new InteractionCreate();
$interaction->type = InteractionType::APPLICATION_COMMAND;
$interaction->data = new InteractionData();
$interaction->data->id = '::application command::';

$this->discord->gateway->events->emit(
Events::INTERACTION_CREATE,
Expand All @@ -146,63 +119,71 @@ public static function nameMappingProvider(): array
{
return [
'Plain name' => [
'command' => (function () {
$command = new ApplicationCommand();
$command->name = 'command-name';
'interaction' => (function () {
$command = new InteractionCreate();
$command->type = InteractionType::APPLICATION_COMMAND;
$command->data = new InteractionData();
$command->data->name = 'command-name';

return $command;
})(),
'expectedName' => 'command-name'
],

'Nested 1 layer' => [
'command' => (function () {
$command = new ApplicationCommand();
$command->name = 'command-name';
'interaction' => (function () {
$command = new InteractionCreate();
$command->type = InteractionType::APPLICATION_COMMAND;
$command->data = new InteractionData();
$command->data->name = 'command-name';

$command->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->name = 'sub';
$command->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND;
$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->name = 'sub';
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND;

return $command;
})(),
'expectedName' => 'command-name.sub'
],

'Nested 2 layer' => [
'command' => (function () {
$command = new ApplicationCommand();
$command->name = 'command-name';
'interaction' => (function () {
$command = new InteractionCreate();
$command->type = InteractionType::APPLICATION_COMMAND;
$command->data = new InteractionData();
$command->data->name = 'command-name';

$command->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->name = 'double';
$command->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;
$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->name = 'double';
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

$command->options[0]->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->options[0]->name = 'sub';
$command->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;
$command->data->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->options[0]->name = 'sub';
$command->data->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

return $command;
})(),
'expectedName' => 'command-name.double.sub'
],

'Nested 3 layer' => [ // NOTE: Not supported by Discord
'command' => (function () {
$command = new ApplicationCommand();
$command->name = 'command-name';

$command->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->name = 'double';
$command->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

$command->options[0]->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->options[0]->name = 'sub';
$command->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

$command->options[0]->options[0]->options = [new ApplicationCommandOptionStructure()];
$command->options[0]->options[0]->options[0]->name = 'dub';
$command->options[0]->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;
'interaction' => (function () {
$command = new InteractionCreate();
$command->type = InteractionType::APPLICATION_COMMAND;
$command->data = new InteractionData();
$command->data->name = 'command-name';

$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->name = 'double';
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

$command->data->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->options[0]->name = 'sub';
$command->data->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

$command->data->options[0]->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()];
$command->data->options[0]->options[0]->options[0]->name = 'dub';
$command->data->options[0]->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP;

return $command;
})(),
Expand Down
Loading

0 comments on commit 3038aa8

Please sign in to comment.