From 59f934e69dae473e9244968b3d3998e26678bb0b Mon Sep 17 00:00:00 2001 From: Dmitri Goosens <1250047+dgoosens@users.noreply.github.com> Date: Wed, 19 Oct 2022 09:47:16 +0200 Subject: [PATCH 1/4] possibility to add info that is not directly linked to laravel if the `$query` is not found in the `$docs` array, suggesting to check in the `$notDocs` array --- src/MessageParser.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/MessageParser.php b/src/MessageParser.php index cd95b1b..1624520 100644 --- a/src/MessageParser.php +++ b/src/MessageParser.php @@ -91,6 +91,11 @@ class MessageParser { "views", "vite", ]; + + protected $notDocs = [ + "exit-vim" => "`:q`", + "ben" => "**BEST PHP RELEASE MANAGER EVER**", + ]; public function __invoke($message) { @@ -106,8 +111,12 @@ public function __invoke($message) return ""; } + + if(in_array($query, $this->notDocs)) { + return $this->notDocs[$query]; + } } return false; } -} \ No newline at end of file +} From 68032783c6e8799cefb8cee103de1db28e7ed003 Mon Sep 17 00:00:00 2001 From: Dmitri Goosens <1250047+dgoosens@users.noreply.github.com> Date: Wed, 19 Oct 2022 21:56:18 +0200 Subject: [PATCH 2/4] Guild specific messages added the possibility to create Guild specific messages --- src/MessageParser.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/MessageParser.php b/src/MessageParser.php index 1624520..7ef5311 100644 --- a/src/MessageParser.php +++ b/src/MessageParser.php @@ -93,8 +93,11 @@ class MessageParser { ]; protected $notDocs = [ - "exit-vim" => "`:q`", - "ben" => "**BEST PHP RELEASE MANAGER EVER**", + // phpugly + "235102104509743106" => [ + "exit-vim" => "`:q`", + "ben" => "**BEST PHP RELEASE MANAGER EVER**", + ], ]; public function __invoke($message) @@ -112,9 +115,16 @@ public function __invoke($message) return ""; } - if(in_array($query, $this->notDocs)) { - return $this->notDocs[$query]; - } + try { + if(null !== $message->guild_id ) { + $guildId = (string) $message->guild_id; + if(isset($this->notDocs[$guildId]) + && isset($this->notDocs[$guildId][$query])) + { + return $this->notDocs[$guildId][$query]; + } + } + } catch (\Throwable $t) {} } return false; From 7e5e3073b2349f2b7eae2a430aecc755f3f59798 Mon Sep 17 00:00:00 2001 From: Dmitri Goosens Date: Wed, 19 Oct 2022 22:55:50 +0200 Subject: [PATCH 3/4] added missing tests --- tests/MessageParserTest.php | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/MessageParserTest.php b/tests/MessageParserTest.php index e76b58e..f36ff4e 100644 --- a/tests/MessageParserTest.php +++ b/tests/MessageParserTest.php @@ -27,6 +27,68 @@ public function testMessageParserReturnsFalseOnBadInput($content, $expected) ); } + public function testMessageParserDoesNotFailIfGuildIdIsNotSet() + { + $parser = new MessageParser(); + $message = new \stdClass(); + $message->content = 'foo_bar'; // must be missing + + $this->assertNull($parser($message)); + } + + public function testMessageParserDoesNotFailIfGuildIdIsNull() + { + $parser = new MessageParser(); + $message = new \stdClass(); + $message->content = 'foo_bar'; // must be missing + $message->guild_id = null; + + $this->assertNull($parser($message)); + } + + public function testMessageParserDoesNotFailIfGuildIdIsUnknown() + { + $parser = new MessageParser(); + $message = new \stdClass(); + $message->content = 'foo_bar'; // must be missing + $message->guild_id = '123'; + + $this->assertNull($parser($message)); + } + + /** + * @dataProvider guildSpecificMessage + */ + public function testMessageParserReturnsResponseWhenGuildIdAndQueryMatch( + string $guidId, string $content, string $expected + ) { + $parser = new MessageParser(); + $message = new \stdClass(); + $message->content = $content; + $message->guild_id = $guidId; + + $this->assertEquals( + $expected, + $parser($message) + ); + } + + /** + * @dataProvider guildSpecificMessage + */ + public function testMessageParserReturnsNullIfContentDoesNotMatch( + string $guidId, string $content, string $expected + ) { + $parser = new MessageParser(); + $message = new \stdClass(); + $message->content = 'foo_bar'; + $message->guild_id = $guidId; + + $this->assertNull( + $parser($message) + ); + } + public function messageInputProvider() { return [ @@ -127,4 +189,12 @@ public function messageInputProvider() ["docs vite", ""], ]; } + + public function guildSpecificMessage(): array + { + return [ + ['235102104509743106', 'exit-vim', "`:q`"], + ['235102104509743106', 'ben', "**BEST PHP RELEASE MANAGER EVER**"], + ]; + } } \ No newline at end of file From 876764979bea53a5872678655f67ff8997d95fbd Mon Sep 17 00:00:00 2001 From: Dmitri Goosens <1250047+dgoosens@users.noreply.github.com> Date: Wed, 19 Oct 2022 23:18:32 +0200 Subject: [PATCH 4/4] corrected tests --- tests/MessageParserTest.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/MessageParserTest.php b/tests/MessageParserTest.php index f36ff4e..01c3b31 100644 --- a/tests/MessageParserTest.php +++ b/tests/MessageParserTest.php @@ -31,29 +31,29 @@ public function testMessageParserDoesNotFailIfGuildIdIsNotSet() { $parser = new MessageParser(); $message = new \stdClass(); - $message->content = 'foo_bar'; // must be missing + $message->content = 'docs foo_bar'; // must be missing - $this->assertNull($parser($message)); + $this->assertFalse($parser($message)); } public function testMessageParserDoesNotFailIfGuildIdIsNull() { $parser = new MessageParser(); $message = new \stdClass(); - $message->content = 'foo_bar'; // must be missing + $message->content = 'docs foo_bar'; // must be missing $message->guild_id = null; - $this->assertNull($parser($message)); + $this->assertFalse($parser($message)); } public function testMessageParserDoesNotFailIfGuildIdIsUnknown() { $parser = new MessageParser(); $message = new \stdClass(); - $message->content = 'foo_bar'; // must be missing + $message->content = 'docs foo_bar'; // must be missing $message->guild_id = '123'; - $this->assertNull($parser($message)); + $this->assertFalse($parser($message)); } /** @@ -81,10 +81,10 @@ public function testMessageParserReturnsNullIfContentDoesNotMatch( ) { $parser = new MessageParser(); $message = new \stdClass(); - $message->content = 'foo_bar'; + $message->content = 'docs foo_bar'; $message->guild_id = $guidId; - $this->assertNull( + $this->assertFalse( $parser($message) ); } @@ -193,8 +193,8 @@ public function messageInputProvider() public function guildSpecificMessage(): array { return [ - ['235102104509743106', 'exit-vim', "`:q`"], - ['235102104509743106', 'ben', "**BEST PHP RELEASE MANAGER EVER**"], + ['235102104509743106', 'docs exit-vim', "`:q`"], + ['235102104509743106', 'docs ben', "**BEST PHP RELEASE MANAGER EVER**"], ]; } -} \ No newline at end of file +}