Skip to content
This repository has been archived by the owner on Dec 23, 2022. It is now read-only.

possibility to add info that is not directly linked to laravel #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ class MessageParser {
"views",
"vite",
];

protected $notDocs = [
// phpugly
"235102104509743106" => [
"exit-vim" => "`:q`",
"ben" => "**BEST PHP RELEASE MANAGER EVER**",
],
];

public function __invoke($message)
{
Expand All @@ -106,8 +114,19 @@ public function __invoke($message)

return "<https://laravel.com/docs/$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;
}
}
}
72 changes: 71 additions & 1 deletion tests/MessageParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,68 @@ public function testMessageParserReturnsFalseOnBadInput($content, $expected)
);
}

public function testMessageParserDoesNotFailIfGuildIdIsNotSet()
{
$parser = new MessageParser();
$message = new \stdClass();
$message->content = 'docs foo_bar'; // must be missing

$this->assertFalse($parser($message));
}

public function testMessageParserDoesNotFailIfGuildIdIsNull()
{
$parser = new MessageParser();
$message = new \stdClass();
$message->content = 'docs foo_bar'; // must be missing
$message->guild_id = null;

$this->assertFalse($parser($message));
}

public function testMessageParserDoesNotFailIfGuildIdIsUnknown()
{
$parser = new MessageParser();
$message = new \stdClass();
$message->content = 'docs foo_bar'; // must be missing
$message->guild_id = '123';

$this->assertFalse($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 = 'docs foo_bar';
$message->guild_id = $guidId;

$this->assertFalse(
$parser($message)
);
}

public function messageInputProvider()
{
return [
Expand Down Expand Up @@ -127,4 +189,12 @@ public function messageInputProvider()
["docs vite", "<https://laravel.com/docs/vite>"],
];
}
}

public function guildSpecificMessage(): array
{
return [
['235102104509743106', 'docs exit-vim', "`:q`"],
['235102104509743106', 'docs ben', "**BEST PHP RELEASE MANAGER EVER**"],
];
}
}