Skip to content

Commit

Permalink
feat: add unit test and add composer metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lefuturiste committed Aug 1, 2019
1 parent ec506d6 commit 703b646
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env.example
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
40 changes: 35 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,50 @@
"name": "lefuturiste/monolog-discord-handler",
"description": "A simple monolog handler for support Discord webhooks",
"type": "library",
"require": {
"monolog/monolog": "*",
"guzzlehttp/guzzle": "*"
},
"keywords": [
"log",
"logging",
"psr3",
"webhook",
"discord",
"integration",
"monolog"
],
"readme": "README.md",
"license": "MIT",
"homepage": "https://github.com/lefuturiste/monolog-discord-handler",
"authors": [
{
"name": "Matthieu Bessat",
"email": "mail@matthieubessat.fr"
"email": "mail@matthieubessat.fr",
"homepage": "https://matthieubessat.fr"
}
],
"support": {
"email": "mail@matthieubessat.fr",
"issues": "https://github.com/lefuturiste/monolog-discord-handler/issues",
"source": "https://github.com/lefuturiste/monolog-discord-handler",
"docs": "https://github.com/lefuturiste/monolog-discord-handler/blob/master/README.md"
},
"scripts": {
"test": "vendor/bin/phpunit tests"
},
"require": {
"monolog/monolog": "*",
"guzzlehttp/guzzle": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.2",
"vlucas/phpdotenv": "^3.4"
},
"autoload": {
"psr-4": {
"DiscordHandler\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"DiscordHandler\\Tests\\": "tests"
}
}
}
33 changes: 33 additions & 0 deletions tests/DiscordHandlerTest.php
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']
);
}

}
66 changes: 66 additions & 0 deletions tests/Helpers/DiscordHelper.php
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;
}
}
46 changes: 46 additions & 0 deletions tests/Wrapper.php
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;
}
}

0 comments on commit 703b646

Please # to comment.