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.
 
 
 
 

102 lines
3.0 KiB

<?php
declare(strict_types=1);
namespace App\Tests\Unit\Service\Mattermost;
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;
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 sendMessageDataProvider
* @throws \ReflectionException
*/
public function testSendMessage(
string $mattermostUrl,
string $mattermostUri,
string $channelName,
string $botName,
string $botIcon,
string $message,
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->sendMessage($message)
);
}
/**
* @throws \JsonException
*/
public function sendMessageDataProvider(): array
{
$faker = (Factory::create('en_EN'));
$channelName = $faker->sha256();
$botName = $faker->sha256();
$botIcon = $faker->sha256();
$message = $faker->sha256();
return [
[
'mattermostUrl' => $faker->sha256(),
'mattermostUri' => $faker->sha256(),
'channelName' => $channelName,
'botName' => $botName,
'botIcon' => $botIcon,
'message' => $message,
'options' => [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'channel' => $channelName,
'username' => $botName,
'icon_url' => $botIcon,
'text' => $message,
], JSON_THROW_ON_ERROR),
]
]
];
}
}