Skip to content

Commit

Permalink
Merge pull request #15 from tanhongit/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
tanhongit authored Nov 7, 2023
2 parents ffc97f3 + f507dce commit f7439f1
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 14 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Lbil Technologies
Copyright (c) 2023 CSlant

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 24 additions & 2 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\TelegramGitNotifier;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Interfaces\BotInterface;
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
use CSlant\TelegramGitNotifier\Interfaces\SettingInterface;
Expand All @@ -29,6 +30,17 @@ class Bot implements AppInterface, BotInterface, EventInterface, SettingInterfac

public Setting $setting;

/**
* @param Telegram|null $telegram
* @param string|null $chatBotId
* @param Event|null $event
* @param string|null $platform
* @param string|null $platformFile
* @param Setting|null $setting
* @param string|null $settingFile
*
* @throws ConfigFileException
*/
public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Expand All @@ -38,12 +50,22 @@ public function __construct(
Setting $setting = null,
?string $settingFile = null,
) {
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);
$this->event = $event ?? new Event();
$this->setPlatFormForEvent($platform, $platformFile);
$this->validatePlatformFile();

$this->setting = $setting ?? new Setting();
$this->updateSetting($settingFile);
$this->validateSettingFile();

$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);
}

public function validateSettingFile(): void
{
if (empty($this->setting->getSettingFile())) {
throw ConfigFileException::settingFile($this->setting->getSettingFile());
}
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/ConfigFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CSlant\TelegramGitNotifier\Exceptions;

final class ConfigFileException extends TelegramGitNotifierException
{
public static function settingFile(?string $settingFile = null): self
{
return new self('Something went wrong while reading settings file. Check your settings file path: ' . ($settingFile ?? 'null'));
}

public static function platformFile(?string $platform = null, ?string $platformFile = null): self
{
return new self('Something went wrong while reading platform file. Check your platform file path: ' . ($platformFile ?? 'null') . ' for platform: ' . ($platform ?? 'null'));
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/SendNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

final class SendNotificationException extends TelegramGitNotifierException
{
public static function create(): self
public static function create(?string $exception = null): self
{
return new self('Can\'t send notification');
return new self('Can\'t send notification. ' . ($exception ?? ''));
}
}
7 changes: 7 additions & 0 deletions src/Interfaces/BotInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

interface BotInterface
Expand Down Expand Up @@ -46,4 +47,10 @@ public function isOwner(): bool;
* @return bool
*/
public function isNotifyChat(): bool;

/**
* @return void
* @throws ConfigFileException
*/
public function validateSettingFile(): void;
}
9 changes: 8 additions & 1 deletion src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -27,7 +28,7 @@ public function getActionOfEvent(object $payload): string;
* @return void
* @see EventTrait::setPlatFormForEvent()
*/
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void;
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void;

/**
* Set event config and get event name
Expand All @@ -38,4 +39,10 @@ public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_P
* @see EventTrait::handleEventFromRequest()
*/
public function handleEventFromRequest(Request $request): ?string;

/**
* @return void
* @throws ConfigFileException
*/
public function validatePlatformFile(): void;
}
18 changes: 15 additions & 3 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Constants\NotificationConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface;
Expand All @@ -25,6 +26,16 @@ class Notifier implements AppInterface, NotificationInterface, EventInterface

public Client $client;

/**
* @param Telegram|null $telegram
* @param string|null $chatBotId
* @param Event|null $event
* @param string|null $platform
* @param string|null $platformFile
* @param Client|null $client
*
* @throws ConfigFileException
*/
public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Expand All @@ -33,11 +44,12 @@ public function __construct(
?string $platformFile = null,
Client $client = null,
) {
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);

$this->event = $event ?? new Event();
$this->setPlatFormForEvent($platform, $platformFile);
$this->validatePlatformFile();

$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);

$this->client = $client ?? new Client();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Structures/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public function sendNotify(string $message = null, array $options = []): bool
return true;
}

throw SendNotificationException::create();
$body = (string) $response->getBody();

throw SendNotificationException::create($body);
} catch (GuzzleException $e) {
error_log($e->getMessage());
throw SendNotificationException::create($e->getMessage());
}

return false;
}
}
13 changes: 12 additions & 1 deletion src/Trait/EventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace CSlant\TelegramGitNotifier\Trait;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use Symfony\Component\HttpFoundation\Request;

trait EventTrait
{
use ActionEventTrait;

public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void
{
/** @var array $platformFileDefaults<platform, platformFile> */
$platformFileDefaults = config('telegram-git-notifier.data_file.platform');
Expand All @@ -31,4 +32,14 @@ public function handleEventFromRequest(Request $request): ?string

return null;
}

public function validatePlatformFile(): void
{
if (empty($this->event->getEventConfig())) {
throw ConfigFileException::platformFile(
$this->event->platform,
$this->event->getPlatformFile()
);
}
}
}
14 changes: 14 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@
expect($this->bot->event->getEventConfig())->toBeArray();
expect($this->bot->event->getEventConfig())->toHaveKey('tag_push');
});

it('setting file is valid', function () {
$this->bot->updateSetting();
$this->bot->validateSettingFile();

expect($this->bot->setting->getSettings())->toBeArray();
});

it('platform file is valid', function () {
$this->bot->setPlatFormForEvent();
$this->bot->validatePlatformFile();

expect($this->bot->event->getEventConfig())->toBeArray();
});

0 comments on commit f7439f1

Please sign in to comment.