grafana -> notification-provider -> mattermost | see https://gitlab.fedy95.com/dev/notification-provider
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.5 KiB
112 lines
3.5 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Unit\Service\Mattermost;
|
|
|
|
use App\Model\Grafana\GrafanaMessage;
|
|
use App\Service\BaseService;
|
|
use App\Service\Mattermost\MattermostService;
|
|
use App\Service\Mattermost\MattermostServiceInterface;
|
|
use Faker\Factory;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Psr7\Request;
|
|
use App\Tests\Unit\UnitTester;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use ReflectionException;
|
|
|
|
final class MattermostServiceTest extends UnitTester
|
|
{
|
|
public function testIsExtends(): void
|
|
{
|
|
$stub = $this->createStub(MattermostService::class);
|
|
|
|
self::assertTrue(is_subclass_of($stub, BaseService::class));
|
|
}
|
|
|
|
public function testIsImplements(): void
|
|
{
|
|
$stub = $this->createStub(MattermostService::class);
|
|
|
|
self::assertInstanceOf(MattermostServiceInterface::class, $stub);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider sendGrafanaMessageDataProvider
|
|
* @throws ReflectionException
|
|
*/
|
|
public function testSendGrafanaMessage(
|
|
string $mattermostUrl,
|
|
string $mattermostUri,
|
|
string $channelName,
|
|
string $botName,
|
|
string $botIcon,
|
|
GrafanaMessage $grafanaMessage,
|
|
array $options
|
|
): void
|
|
{
|
|
$mattermostService = new MattermostService(
|
|
$mattermostUrl, $mattermostUri, $channelName, $botName, $botIcon
|
|
);
|
|
$clientMock = $this->getMockBuilder(Client::class)
|
|
->disableOriginalConstructor()
|
|
->onlyMethods(['send'])
|
|
->getMock();
|
|
$clientProperty = $this->getClassProperty($mattermostService, 'client');
|
|
$clientProperty->setValue($mattermostService, $clientMock);
|
|
$responseMock = $this->getMockForAbstractClass(ResponseInterface::class);
|
|
$request = new Request('POST', $mattermostUri);
|
|
|
|
$clientMock->expects(self::once())
|
|
->method('send')
|
|
->with($request, $options)
|
|
->willReturn($responseMock);
|
|
|
|
self::assertEquals(
|
|
$responseMock,
|
|
$mattermostService->sendGrafanaMessage($grafanaMessage)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws \JsonException
|
|
*/
|
|
public function sendGrafanaMessageDataProvider(): array
|
|
{
|
|
$faker = (Factory::create('en_EN'));
|
|
$channelName = $faker->sha256();
|
|
$botName = $faker->sha256();
|
|
$botIcon = $faker->sha256();
|
|
$title = $faker->sha256();
|
|
$message = $faker->sha256();
|
|
$imageUrl = $faker->sha256();
|
|
|
|
return [
|
|
[
|
|
'mattermostUrl' => $faker->sha256(),
|
|
'mattermostUri' => $faker->sha256(),
|
|
'channelName' => $channelName,
|
|
'botName' => $botName,
|
|
'botIcon' => $botIcon,
|
|
'message' =>
|
|
(new GrafanaMessage())
|
|
->setTitle($title)
|
|
->setMessage($message)
|
|
->setImageUrl($imageUrl),
|
|
'options' => [
|
|
'headers' => [
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'body' => json_encode([
|
|
'channel' => $channelName,
|
|
'username' => $botName,
|
|
'icon_url' => $botIcon,
|
|
'text' => sprintf("%s\n %s\n %s\n\n",
|
|
$title, $message, $imageUrl
|
|
),
|
|
], JSON_THROW_ON_ERROR),
|
|
]
|
|
]
|
|
];
|
|
}
|
|
}
|