diff --git a/src/Command/GlobalCommandExtension.php b/src/Command/GlobalCommandExtension.php index 91bdeb9..0c73c9c 100644 --- a/src/Command/GlobalCommandExtension.php +++ b/src/Command/GlobalCommandExtension.php @@ -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'); } diff --git a/src/Command/GuildCommandExtension.php b/src/Command/GuildCommandExtension.php index a9e619e..3eb3c58 100644 --- a/src/Command/GuildCommandExtension.php +++ b/src/Command/GuildCommandExtension.php @@ -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'); diff --git a/tests/Command/GlobalCommandExtensionTest.php b/tests/Command/GlobalCommandExtensionTest.php index dd293e4..3c2029f 100644 --- a/tests/Command/GlobalCommandExtensionTest.php +++ b/tests/Command/GlobalCommandExtensionTest.php @@ -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; @@ -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 @@ -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; @@ -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, @@ -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; @@ -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; @@ -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, @@ -146,9 +119,11 @@ 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; })(), @@ -156,13 +131,15 @@ public static function nameMappingProvider(): array ], '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; })(), @@ -170,17 +147,19 @@ public static function nameMappingProvider(): array ], '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; })(), @@ -188,21 +167,23 @@ public static function nameMappingProvider(): array ], '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; })(), diff --git a/tests/Command/GuildCommandExtensionTest.php b/tests/Command/GuildCommandExtensionTest.php index 4eebc7c..8270aac 100644 --- a/tests/Command/GuildCommandExtensionTest.php +++ b/tests/Command/GuildCommandExtensionTest.php @@ -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\GuildCommandExtension; use Ragnarok\Fenrir\Constants\Events; @@ -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 GuildCommandExtensionTest extends TestCase @@ -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->guildCommand->shouldReceive() - ->getCommands('::guild id::', '::application id::') - ->andReturns(PromiseFake::get($commands)); - - $extension = new GuildCommandExtension('::application id::', '::guild id::'); + $extension = new GuildCommandExtension(null, '::guild id::'); $extension->initialize($this->discord); - $hasRun = [false, false]; + $hasRun = [false, false, false]; $extension->on('command-1', function (CommandInteraction $firedCommand) use (&$hasRun) { $hasRun[0] = true; @@ -54,17 +40,22 @@ 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'; + $interaction->data->guild_id = '::guild id::'; $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, @@ -73,20 +64,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 testItDoesNotEmitEventsForDifferentGuilds() { - $command = new ApplicationCommand(); - - $command->id = '::application command::'; - $command->name = 'command'; - - $this->discord->rest->guildCommand->shouldReceive() - ->getCommands('::guild id::', '::application id::') - ->andReturns(PromiseFake::get([$command])); - - $extension = new GuildCommandExtension('::application id::', '::guild id::'); + $extension = new GuildCommandExtension(null, '::guild id::'); $extension->initialize($this->discord); $hasRun = false; @@ -96,31 +79,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 2::'; $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->guildCommand->shouldReceive() - ->getCommands('::guild id::', '::application id::') - ->andReturns(PromiseFake::get([$command])); - - $extension = new GuildCommandExtension('::application id::', '::guild id::'); + $extension = new GuildCommandExtension(null, '::guild id::'); $extension->initialize($this->discord); $hasRun = false; @@ -129,11 +107,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, [$interaction] @@ -146,9 +119,12 @@ 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'; + $command->data->guild_id = '::guild id::'; return $command; })(), @@ -156,13 +132,16 @@ public static function nameMappingProvider(): array ], '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->data->guild_id = '::guild id::'; - $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; })(), @@ -170,17 +149,20 @@ public static function nameMappingProvider(): array ], '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->data->guild_id = '::guild id::'; - $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; })(), @@ -188,21 +170,24 @@ public static function nameMappingProvider(): array ], '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->guild_id = '::guild id::'; + + $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; })(), diff --git a/tests/EventHandlerTest.php b/tests/EventHandlerTest.php index c04b53a..b3cfcbd 100644 --- a/tests/EventHandlerTest.php +++ b/tests/EventHandlerTest.php @@ -10,7 +10,6 @@ use Ragnarok\Fenrir\EventHandler; use Ragnarok\Fenrir\Gateway\Objects\Payload; use PHPUnit\Framework\TestCase; -use React\Promise\Promise; final class EventHandlerTest extends TestCase {