-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit test and add composer metadata
- Loading branch information
1 parent
ec506d6
commit 703b646
Showing
5 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DISCORD_GUILD_ID=XXXX | ||
DISCORD_WEBHOOK_URL=https://discordapp.com/api/webhooks/XXX/XXX | ||
DISCORD_TEXT_CHANNEL_ID=XXXX | ||
DISCORD_TEXT_CHANNEL_NAME=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace DiscordHandler\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use GuzzleHttp\Client; | ||
use Monolog\Logger; | ||
use DiscordHandler\Tests\Helpers\DiscordHelper; | ||
|
||
class DiscordHandlerTest extends Wrapper { | ||
|
||
public function testSimpleWebhook() | ||
{ | ||
$this->beforeTest(); | ||
$discordHelper = $this->getDiscordHelper(); | ||
$logger = new Logger('name'); | ||
$logger->pushHandler(new \DiscordHandler\DiscordHandler( | ||
self::$webhookUrl, | ||
'foo', | ||
'bar', | ||
'DEBUG' | ||
)); | ||
$message = uniqid(); | ||
$time = date('Y-m-d H:i:s'); | ||
$logger->warning($message); | ||
|
||
$this->assertEquals( | ||
'**[' . $time . ']** foo.bar.__WARNING__: ' . $message, | ||
$discordHelper->getLastMessage()['content'] | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace DiscordHandler\Tests\Helpers; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class DiscordHelper { | ||
private $token; | ||
private $guildId; | ||
private $channelId; | ||
private $client; | ||
|
||
public function __construct($token, $guildId) { | ||
$this->token = $token; | ||
$this->guildId = $guildId; | ||
$this->client = new Client([ | ||
'base_uri' => 'https://discordapp.com', | ||
'http_errors' => true, | ||
'headers' => [ | ||
'Authorization' => 'Bot ' . $this->token | ||
] | ||
]); | ||
} | ||
|
||
public function fetchChannelIdFromName($channelName) | ||
{ | ||
$response = $this->client->get( | ||
'/api/guilds/' . $this->guildId . '/channels' | ||
); | ||
$body = $this->parseBody($response); | ||
$this->channelId = array_values(array_filter($body, function($c) use ($channelName) { | ||
return $c['name'] === $channelName; | ||
}))[0]['id']; | ||
|
||
return $this->channelId; | ||
} | ||
|
||
public function getLastMessage() | ||
{ | ||
$response = $this->client->get( | ||
'/api/channels/' . $this->channelId . '/messages' | ||
); | ||
$body = $this->parseBody($response); | ||
return array_values($body)[0]; | ||
} | ||
|
||
private function parseBody($response) | ||
{ | ||
return json_decode($response->getBody()->getContents(), true); | ||
} | ||
|
||
public function setChannelId($channelId) | ||
{ | ||
$this->channelId = $channelId; | ||
} | ||
|
||
public function getGuildId() | ||
{ | ||
return $this->guildId; | ||
} | ||
|
||
public function getToken() | ||
{ | ||
return $this->token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace DiscordHandler\Tests; | ||
|
||
use DiscordHandler\Tests\Helpers\DiscordHelper; | ||
|
||
class Wrapper extends \PHPUnit\Framework\TestCase | ||
{ | ||
private static $discordHelper = NULL; | ||
|
||
protected static $webhookUrl = ""; | ||
|
||
private static $botToken = ""; | ||
|
||
private static $guildId = ""; | ||
|
||
private static $textChannelId = ""; | ||
|
||
private static $textChannelName = ""; | ||
|
||
protected function beforeTest() { | ||
$dotEnvPath = dirname(__DIR__); | ||
\Dotenv\Dotenv::create($dotEnvPath)->load(); | ||
self::$webhookUrl = getenv('DISCORD_WEBHOOK_URL'); | ||
self::$botToken = getenv('DISCORD_BOT_TOKEN'); | ||
self::$guildId = getenv('DISCORD_GUILD_ID'); | ||
self::$textChannelName = getenv('DISCORD_TEXT_CHANNEL_NAME', ''); | ||
self::$textChannelId = getenv('DISCORD_TEXT_CHANNEL_ID', ''); | ||
} | ||
|
||
protected function getDiscordHelper() | ||
{ | ||
if (self::$discordHelper === NULL) { | ||
self::$discordHelper = new DiscordHelper( | ||
self::$botToken, | ||
self::$guildId | ||
); | ||
if (self::$textChannelName !== '') { | ||
self::$discordHelper->fetchChannelIdFromName(self::$textChannelName); | ||
} else { | ||
self::$discordHelper->setChannelId(self::$textChannelId); | ||
} | ||
} | ||
return self::$discordHelper; | ||
} | ||
} |