Skip to content

Commit

Permalink
Add sources and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaoka committed Jun 25, 2024
1 parent b05cf1e commit d1d59a7
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024+ SUNAOKA Norifumi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "sunaoka/laravel-slack-block-kit-web-hook-driver",
"description": "Laravel Log Driver for sending Block Kit messages to incoming webhooks in Slack.",
"keywords": ["laravel", "log", "driver", "slack", "slack-block-kit", "slack-incoming-webhooks"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "sunaoka",
"email": "[email protected]"
}
],
"require": {
"php": "^8.1",
"ext-curl": "*",
"illuminate/log": "^10.0 || ^11.0",
"monolog/monolog": "^3.0"
},
"require-dev": {
"larastan/larastan": "^2.9.6",
"laravel/pint": "^1.6",
"mockery/mockery": "^1.6",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.5 || ^11.0"
},
"autoload": {
"psr-4": {
"Sunaoka\\Laravel\\Log\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sunaoka\\Laravel\\Log\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.0-dev"
},
"laravel": {
"providers": [
],
"aliases": {
}
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- vendor/phpstan/phpstan-mockery/extension.neon

parameters:

level: 9

paths:
- src/
- tests/
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="test">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
43 changes: 43 additions & 0 deletions src/Handler/SlackBlockKitWebhookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Sunaoka\Laravel\Log\Handler;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\Curl;
use Monolog\Level;
use Monolog\LogRecord;
use Psr\Log\LogLevel;

class SlackBlockKitWebhookHandler extends AbstractProcessingHandler
{
/**
* @param string $webhookUrl Slack Webhook URL
* @param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(
private readonly string $webhookUrl,
int|string|Level $level = Level::Debug,
bool $bubble = true
) {
parent::__construct($level, $bubble);
}

#[\Override]
protected function write(LogRecord $record): void
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->webhookUrl,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-type: application/json'],
CURLOPT_POSTFIELDS => $record->message,
CURLOPT_TIMEOUT => 10,
]);

Curl\Util::execute($ch);
}
}
27 changes: 27 additions & 0 deletions src/Slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Sunaoka\Laravel\Log;

use Monolog\Level;
use Monolog\Logger as Monolog;
use Psr\Log\LogLevel;
use Sunaoka\Laravel\Log\Handler\SlackBlockKitWebhookHandler;

class Slack
{
/**
* @param array{url: string, level?: value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::*, bubble?: bool} $config
*/
public function __invoke(array $config): Monolog
{
$handler = new SlackBlockKitWebhookHandler(
$config['url'],
$config['level'] ?? 'debug',
$config['bubble'] ?? true
);

return new Monolog('slack', [$handler]);
}
}
23 changes: 23 additions & 0 deletions tests/SlackBlockKitLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sunaoka\Laravel\Log\Tests;

use Sunaoka\Laravel\Log\Handler\SlackBlockKitWebhookHandler;
use Sunaoka\Laravel\Log\Slack;

class SlackBlockKitLoggerTest extends TestCase
{
public function test_successful(): void
{
$actual = (new Slack())->__invoke([
'url' => 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX',
'level' => 'debug',
'bubble' => true,
]);

self::assertSame('slack', $actual->getName());
self::assertInstanceOf(SlackBlockKitWebhookHandler::class, $actual->getHandlers()[0]);
}
}
42 changes: 42 additions & 0 deletions tests/SlackBlockKitWebhookHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Sunaoka\Laravel\Log\Tests;

use Monolog\Handler\Curl;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Sunaoka\Laravel\Log\Handler\SlackBlockKitWebhookHandler;

class SlackBlockKitWebhookHandlerTest extends TestCase
{
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
public function test_successful(): void
{
\Mockery::mock('overload:'.Curl\Util::class)
->makePartial()
->shouldReceive('execute')
->andReturn('ok');

$handler = new SlackBlockKitWebhookHandler(
webhookUrl: 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX',
level: 'debug',
bubble: false,
);

$actual = $handler->handle(new LogRecord(
datetime: new \DateTimeImmutable(),
channel: 'channel',
level: Level::Debug,
message: 'message',
context: [],

));

self::assertTrue($actual);
}
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Sunaoka\Laravel\Log\Tests;

abstract class TestCase extends \PHPUnit\Framework\TestCase {}

0 comments on commit d1d59a7

Please sign in to comment.