|
@ -4,12 +4,16 @@ declare(strict_types=1); |
|
|
|
|
|
|
|
|
namespace App\Tests\Unit\Service\Mattermost; |
|
|
namespace App\Tests\Unit\Service\Mattermost; |
|
|
|
|
|
|
|
|
|
|
|
use App\Model\Alertmanager\Alert; |
|
|
|
|
|
use App\Model\Alertmanager\AlertmanagerMessage; |
|
|
|
|
|
use App\Model\Alertmanager\Annotation; |
|
|
use App\Model\Grafana\GrafanaMessage; |
|
|
use App\Model\Grafana\GrafanaMessage; |
|
|
use App\Service\BaseService; |
|
|
use App\Service\BaseService; |
|
|
use App\Service\Mattermost\MattermostService; |
|
|
use App\Service\Mattermost\MattermostService; |
|
|
use App\Service\Mattermost\MattermostServiceInterface; |
|
|
use App\Service\Mattermost\MattermostServiceInterface; |
|
|
use Faker\Factory; |
|
|
use Faker\Factory; |
|
|
use GuzzleHttp\Client; |
|
|
use GuzzleHttp\Client; |
|
|
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
|
use GuzzleHttp\Psr7\Request; |
|
|
use GuzzleHttp\Psr7\Request; |
|
|
use App\Tests\Unit\UnitTester; |
|
|
use App\Tests\Unit\UnitTester; |
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
use Psr\Http\Message\ResponseInterface; |
|
@ -31,18 +35,118 @@ final class MattermostServiceTest extends UnitTester |
|
|
self::assertInstanceOf(MattermostServiceInterface::class, $stub); |
|
|
self::assertInstanceOf(MattermostServiceInterface::class, $stub); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @dataProvider sendAlertmanagerMessageDataProvider |
|
|
|
|
|
* @throws GuzzleException |
|
|
|
|
|
* @throws \JsonException |
|
|
|
|
|
* @throws ReflectionException |
|
|
|
|
|
*/ |
|
|
|
|
|
public function testSendAlertmanagerMessage( |
|
|
|
|
|
string $mattermostUrl, |
|
|
|
|
|
string $mattermostUri, |
|
|
|
|
|
string $channelName, |
|
|
|
|
|
string $botName, |
|
|
|
|
|
string $botIcon, |
|
|
|
|
|
AlertmanagerMessage $alertmanagerMessage, |
|
|
|
|
|
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->sendAlertmanagerMessage($alertmanagerMessage) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @throws GuzzleException |
|
|
|
|
|
* @throws \JsonException |
|
|
|
|
|
*/ |
|
|
|
|
|
public function sendAlertmanagerMessageDataProvider(): array |
|
|
|
|
|
{ |
|
|
|
|
|
$faker = (Factory::create('en_EN')); |
|
|
|
|
|
$channelName = $faker->sha256(); |
|
|
|
|
|
$botName = $faker->sha256(); |
|
|
|
|
|
$botIcon = $faker->sha256(); |
|
|
|
|
|
$description = [ |
|
|
|
|
|
$faker->sha256(), |
|
|
|
|
|
$faker->sha256() |
|
|
|
|
|
]; |
|
|
|
|
|
$summary = [ |
|
|
|
|
|
$faker->sha256(), |
|
|
|
|
|
$faker->sha256() |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
[ |
|
|
|
|
|
'mattermostUrl' => $faker->sha256(), |
|
|
|
|
|
'mattermostUri' => $faker->sha256(), |
|
|
|
|
|
'channelName' => $channelName, |
|
|
|
|
|
'botName' => $botName, |
|
|
|
|
|
'botIcon' => $botIcon, |
|
|
|
|
|
'message' => |
|
|
|
|
|
(new AlertmanagerMessage()) |
|
|
|
|
|
->setAlerts([ |
|
|
|
|
|
(new Alert()) |
|
|
|
|
|
->setAnnotations( |
|
|
|
|
|
(new Annotation()) |
|
|
|
|
|
->setDescription($description[0]) |
|
|
|
|
|
->setSummary($summary[0]) |
|
|
|
|
|
), |
|
|
|
|
|
(new Alert()) |
|
|
|
|
|
->setAnnotations( |
|
|
|
|
|
(new Annotation()) |
|
|
|
|
|
->setDescription($description[1]) |
|
|
|
|
|
->setSummary($summary[1]) |
|
|
|
|
|
) |
|
|
|
|
|
] |
|
|
|
|
|
), |
|
|
|
|
|
'options' => [ |
|
|
|
|
|
'headers' => [ |
|
|
|
|
|
'Content-Type' => 'application/json', |
|
|
|
|
|
], |
|
|
|
|
|
'body' => json_encode([ |
|
|
|
|
|
'channel' => $channelName, |
|
|
|
|
|
'username' => $botName, |
|
|
|
|
|
'icon_url' => $botIcon, |
|
|
|
|
|
'text' => sprintf( |
|
|
|
|
|
"message %s\n - description: %s\n - summary: %s\n\n --- \nmessage %s\n - description: %s\n - summary: %s\n", |
|
|
|
|
|
0, $description[0], $summary[0], 1, $description[1], $summary[1], |
|
|
|
|
|
), |
|
|
|
|
|
], JSON_THROW_ON_ERROR), |
|
|
|
|
|
] |
|
|
|
|
|
] |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @dataProvider sendGrafanaMessageDataProvider |
|
|
* @dataProvider sendGrafanaMessageDataProvider |
|
|
* @throws ReflectionException |
|
|
* @throws ReflectionException |
|
|
*/ |
|
|
*/ |
|
|
public function testSendGrafanaMessage( |
|
|
public function testSendGrafanaMessage( |
|
|
string $mattermostUrl, |
|
|
|
|
|
string $mattermostUri, |
|
|
|
|
|
string $channelName, |
|
|
|
|
|
string $botName, |
|
|
|
|
|
string $botIcon, |
|
|
|
|
|
|
|
|
string $mattermostUrl, |
|
|
|
|
|
string $mattermostUri, |
|
|
|
|
|
string $channelName, |
|
|
|
|
|
string $botName, |
|
|
|
|
|
string $botIcon, |
|
|
GrafanaMessage $grafanaMessage, |
|
|
GrafanaMessage $grafanaMessage, |
|
|
array $options |
|
|
|
|
|
|
|
|
array $options |
|
|
): void |
|
|
): void |
|
|
{ |
|
|
{ |
|
|
$mattermostService = new MattermostService( |
|
|
$mattermostService = new MattermostService( |
|
|