Skip to content

Commit

Permalink
Add test for allCOmmandExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanlv committed Mar 28, 2024
1 parent 3038aa8 commit bf53b67
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions tests/Command/AllCommandExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

declare(strict_types=1);

namespace Tests\Ragnarok\Fenrir\Command;

use Fakes\Ragnarok\Fenrir\DiscordFake;
use PHPUnit\Framework\TestCase;
use Ragnarok\Fenrir\Command\AllCommandExtension;
use Ragnarok\Fenrir\Constants\Events;
use Ragnarok\Fenrir\Discord;
use Ragnarok\Fenrir\Enums\ApplicationCommandOptionType;
use Ragnarok\Fenrir\Enums\InteractionType;
use Ragnarok\Fenrir\Gateway\Events\InteractionCreate;
use Ragnarok\Fenrir\Interaction\CommandInteraction;
use Ragnarok\Fenrir\Parts\ApplicationCommandInteractionDataOptionStructure;
use Ragnarok\Fenrir\Parts\InteractionData;

class AllCommandExtensionTest extends TestCase
{
private Discord $discord;

protected function setUp(): void
{
$this->discord = DiscordFake::get();
}

public function testItEmitsEventsForApplicationCommands()
{
$extension = new AllCommandExtension();
$extension->initialize($this->discord);

$hasRun = [false, false, false];

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

$extension->on('command-2', function (CommandInteraction $firedCommand) use (&$hasRun) {
$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->name = 'command-1';

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

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

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

$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 testItEmitsEventsForGuildCommands()
{
$extension = new AllCommandExtension();
$extension->initialize($this->discord);

$hasRun = [false, false, false];

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

$extension->on('command-2', function (CommandInteraction $firedCommand) use (&$hasRun) {
$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->name = 'command-1';

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

$interaction->data->name = 'command-2';

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

$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 static function nameMappingProvider(): array
{
return [
'Plain 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' => [
'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 = 'sub';
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND;

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

'Nested 2 layer' => [
'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;

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

'Nested 3 layer' => [ // NOTE: Not supported by Discord
'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;
})(),
'expectedName' => 'command-name.double.sub.dub'
],
];
}
}

0 comments on commit bf53b67

Please sign in to comment.