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 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 * @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), ] ] ]; } }