Skip to content

Commit

Permalink
Merge pull request #26 from tanhongit/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
tanhongit authored Nov 2, 2023
2 parents e5da16f + aad9017 commit e45b5a7
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 13 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ jobs:
with:
php-version: '8.1'

- name: Install composer dependencies
uses: ramsey/composer-install@v2
- name: Checkout code
uses: actions/checkout@v4

- name: Install dependencies
run: |
composer install --no-interaction --no-progress --no-suggest
- name: Run PHPStan
run: composer analyse --error-format=github
run: |
composer analyse --error-format=github
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ npm-debug.log
/storage/fonts
/storage/installing
/storage/installed
/storage/json
/log

package-lock.json
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and manage customization through messages and buttons on Telegram.
</p>

![License](https://img.shields.io/github/license/lbiltech/telegram-git-notifier.svg?style=flat-square)
[![Latest Version](https://img.shields.io/github/release/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://github.com/lbiltech/telegram-git-notifier/releases)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://packagist.org/lbiltech/telegram-git-notifier)
[![Total Downloads](https://img.shields.io/packagist/dt/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://packagist.org/packages/lbiltech/telegram-git-notifier)
[![StyleCI](https://styleci.io/repos/683908657/shield)](https://styleci.io/repos/683908657)
[![Quality Score](https://img.shields.io/scrutinizer/g/lbiltech/telegram-git-notifier.svg?style=flat-square)](https://scrutinizer-ci.com/g/lbiltech/telegram-git-notifier)
Expand Down
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,29 @@
"vlucas/phpdotenv": "^5.5"
},
"require-dev": {
"phpstan/phpstan": "^1.10.39",
"friendsofphp/php-cs-fixer": "^v3.37.1"
"friendsofphp/php-cs-fixer": "^v3.37.1",
"pestphp/pest": "^2.24",
"phpstan/phpstan": "^1.10.39"
},
"scripts": {
"analyse": "vendor/bin/phpstan",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"test": "vendor/bin/pest",
"post-install-cmd": [
"bash ./install.sh"
],
"post-update-cmd": [
"bash ./install.sh"
]
},
"support": {
"issues": "https://github.com/lbiltech/telegram-git-notifier/issues"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
24 changes: 24 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

echo "Setting up config files for Telegram Git Notifier..."

mkdir -p storage/json/tgn

json_files=(
"github-events.json"
"gitlab-events.json"
"tgn-settings.json"
)

for file in "${json_files[@]}"; do
if [ ! -f "storage/json/tgn/$file" ]; then
cp "./config/jsons/$file" "storage/json/tgn/$file"
echo "Created storage/json/tgn/$file"
fi
done

if [[ "$(uname -s -r)" == *"Linux"* && "$(cat /etc/os-release)" == *"Ubuntu"* ]]; then
chmod 777 storage/json/tgn/*.json
fi

echo "Telegram Git Notifier config files are ready!"
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Telegram Git Notifier Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
7 changes: 6 additions & 1 deletion src/Interfaces/Structures/NotificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LbilTech\TelegramGitNotifier\Interfaces\Structures;

use LbilTech\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -31,6 +32,7 @@ public function accessDenied(
*
* @return mixed|void
* @throws InvalidViewTemplateException
* @throws MessageIsEmptyException
* @see Notification::setPayload()
*/
public function setPayload(Request $request, string $event);
Expand Down Expand Up @@ -60,7 +62,10 @@ public function getActionOfEvent(object $payload): string;
/**
* Convert chat and thread ids to array
* Example: 1234567890;1234567890:thread1;1234567890:thread1,thread2
*
* @param string|null $chatIds
*
* @return array
*/
public function parseNotifyChatIds(): array;
public function parseNotifyChatIds(?string $chatIds = null): array;
}
4 changes: 2 additions & 2 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function __construct(
$this->client = $client ?? new Client();
}

public function parseNotifyChatIds(): array
public function parseNotifyChatIds(?string $chatIds = null): array
{
$chatData = explode(
NotificationConstant::CHAT_ID_PAIRS_SEPARATOR,
config('telegram-git-notifier.bot.notify_chat_ids')
$chatIds ?? config('telegram-git-notifier.bot.notify_chat_ids')
);
$chatThreadMapping = [];

Expand Down
5 changes: 3 additions & 2 deletions src/Objects/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public function isAccessEvent(
$eventConfig = $eventConfig[$action];
}

if (!$eventConfig) {
if (is_array($eventConfig) || !$eventConfig) {
$eventConfig = false;
error_log('\n Event config is not found \n');
}

return (bool)$eventConfig;
return $eventConfig;
}
}
39 changes: 39 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use LbilTech\TelegramGitNotifier\Bot;

beforeEach(function () {
$this->bot = new Bot();
});

it('should return true', function () {
$this->assertTrue(true);
});

it('platform can be set for event with platform parameter', function () {
$this->bot->setPlatFormForEvent('gitlab');
expect($this->bot->event->platform)->toBe('gitlab');
});

it('platform can be set for event with null parameter', function () {
$this->bot->setPlatFormForEvent();
expect($this->bot->event->platform)->toBe('github');
});

it('platform can be set for event with platform file', function () {
$this->bot->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json');
expect($this->bot->event->platform)->toBe('gitlab');
expect($this->bot->event->getPlatformFile())->toBe('storage/json/tgn/gitlab-events.json');
});

it('can get json config for event - github', function () {
$this->bot->setPlatFormForEvent();
expect($this->bot->event->getEventConfig())->toBeArray();
expect($this->bot->event->getEventConfig())->toHaveKey('issue_comment');
});

it('can get json config for event - gitlab', function () {
$this->bot->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json');
expect($this->bot->event->getEventConfig())->toBeArray();
expect($this->bot->event->getEventConfig())->toHaveKey('tag_push');
});
25 changes: 25 additions & 0 deletions tests/NotifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use LbilTech\TelegramGitNotifier\Notifier;

beforeEach(function () {
$this->nofitier = new Notifier();
});

it('validates that the event files exist', function () {
$this->nofitier->setPlatFormForEvent('gitlab', 'storage/json/tgn/gitlab-events.json');
expect($this->nofitier->event->getEventConfig())->toBeArray();
expect($this->nofitier->event->getEventConfig())->toHaveKey('tag_push');

$this->nofitier->setPlatFormForEvent('github', 'storage/json/tgn/github-events.json');
expect($this->nofitier->event->getEventConfig())->toBeArray();
expect($this->nofitier->event->getEventConfig())->toHaveKey('issue_comment');
});

it('can parse notification chat IDs', function () {
$chatThreadMapping = $this->nofitier->parseNotifyChatIds('-1201937489183;-1008168942527:46,2');
expect($chatThreadMapping)->toBeArray();
expect($chatThreadMapping)->toHaveKey('-1008168942527');
expect($chatThreadMapping['-1008168942527'])->toBeArray();
expect($chatThreadMapping['-1008168942527'])->toBe([0 => '46', 1 => '2']);
});
Empty file removed tests/TelegramGitNorifierTest.php
Empty file.
48 changes: 48 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use LbilTech\TelegramGitNotifier\Bot;
use LbilTech\TelegramGitNotifier\Objects\Validator;

beforeEach(function () {
$this->bot = new Bot();
$this->bot->updateSetting('storage/json/tgn/setting.json');
$this->bot->setPlatFormForEvent();
$this->validator = new Validator($this->bot->setting, $this->bot->event);
});

it('can validate the event that has no action to send a notification - GitHub', function () {
$result = $this->validator->isAccessEvent('github', 'push', (object)[]);
expect($result)->toBeTrue();
});

it('can validate the event that has an action to send a notification - GitHub', function () {
$result = $this->validator->isAccessEvent('github', 'pull_request', (object)[
'action' => 'opened',
]);
expect($result)->toBeTrue();
});

it('can\'t validate the event that has an action but no payload to send a notification - GitHub', function () {
$result = $this->validator->isAccessEvent('github', 'pull_request', (object)[]);
expect($result)->toBeFalse();
});

it('can validate the event that has no action to send a notification - gitlab', function () {
$this->bot->setPlatFormForEvent('gitlab');
$result = $this->validator->isAccessEvent('gitlab', 'push', (object)[]);
expect($result)->toBeTrue();
});

it('can validate the event that has an action to send a notification - gitlab', function () {
$this->bot->setPlatFormForEvent('gitlab');
$result = $this->validator->isAccessEvent('gitlab', 'merge_request', (object)[
'action' => 'open',
]);
expect($result)->toBeTrue();
});

it('can\'t validate the event that has an action but no payload to send a notification - gitlab', function () {
$this->bot->setPlatFormForEvent('gitlab');
$result = $this->validator->isAccessEvent('gitlab', 'merge_request', (object)[]);
expect($result)->toBeFalse();
});

0 comments on commit e45b5a7

Please sign in to comment.