Skip to content

Commit

Permalink
Merge pull request #1 from nutgram/update_to_4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 authored May 16, 2023
2 parents 536efb0 + ea0e8f6 commit 9046d47
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 27 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Test Suite

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ 8.2 ]

name: PHP ${{ matrix.php }}
steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php }}
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: |
composer update --prefer-dist --no-interaction --no-suggest
- name: Run test suite
run: composer run-script test
coverage:
name: Coverage
needs: [ tests ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-8.2-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-8.2
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-interaction --no-suggest

- name: Run test suite
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_REPORTER_ID }}
with:
coverageCommand: composer run-script test-coverage
coverageLocations: ${{github.workspace}}/coverage.xml:clover
27 changes: 23 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@
"name": "nutgram/symfony-bundle",
"description": "Nutgram for Symfony",
"type": "symfony-bundle",
"license": "MIT",
"require": {
"php": "^8.0",
"nutgram/nutgram": "^3.0",
"php": "^8.2",
"nutgram/nutgram": "^4.0",
"symfony/cache": "^6.0",
"symfony/config": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/console": "^6.0"
},
"license": "MIT",
"require-dev": {
"symfony/framework-bundle": "^6.0",
"symfony/yaml": "^6.0",
"pestphp/pest": "^2.6"
},
"autoload": {
"psr-4": {
"SergiX44\\NutgramBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"SergiX44\\NutgramBundle\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Sergio Brighenti",
"email": "[email protected]"
}
],
"scripts": {
"test": "@php vendor/bin/pest --fail-on-warning",
"test-coverage": "@php vendor/bin/pest --coverage --coverage-clover=coverage.xml --fail-on-warning"
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
8 changes: 4 additions & 4 deletions config/nutgram.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ nutgram:
token: '%env(string:TELEGRAM_TOKEN)%'

# If true, the webhook mode validates the incoming IP range is from a Telegram server
safe_mode: false

# Extra or specific configuration
config: [ ]
safeMode: false

# If the nutgram bundle should automatically load the routes from config/telegram.php
routes: true

# Extra or specific configuration
config: [ ]
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ services:
arguments: [ '%nutgram.config%', '@service_container' ]
configurator: [ '@SergiX44\NutgramBundle\NutgramConfigurator', 'configure' ]

nutgram:
alias: 'SergiX44\Nutgram\Nutgram'

SergiX44\NutgramBundle\Console\:
resource: '../src/Console/'
autowire: true
Expand Down
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="test"/>
<env name="KERNEL_CLASS" value="SergiX44\NutgramBundle\Tests\Fixtures\AppKernel"/>
<env name="TELEGRAM_TOKEN" value="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Console/LogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dropPendingUpdates = (bool)$input->getOption('drop-pending-updates');

try {
$this->bot->deleteWebhook(['drop_pending_updates' => $dropPendingUpdates]);
$this->bot->deleteWebhook($dropPendingUpdates);
} finally {
$io->info('Webhook deleted.');
}
Expand Down
4 changes: 1 addition & 3 deletions src/Console/WebhookRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$dropPendingUpdates = (bool)$input->getOption('drop-pending-updates');

$this->bot->deleteWebhook([
'drop_pending_updates' => $dropPendingUpdates,
]);
$this->bot->deleteWebhook($dropPendingUpdates);

if ($dropPendingUpdates) {
$io->info('Pending updates dropped.');
Expand Down
2 changes: 1 addition & 1 deletion src/Console/WebhookSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$max_connections = (int)$max_connections;
}

$this->bot->setWebhook($url, array_filter(compact('ip_address', 'max_connections')));
$this->bot->setWebhook($url, ip_address: $ip_address, max_connections: $max_connections);

$io->info("Bot webhook set with url: $url");

Expand Down
21 changes: 16 additions & 5 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ public function getConfigTreeBuilder(): TreeBuilder

$treeBuilder->getRootNode()
->children()
->scalarNode('token')->end()
->booleanNode('safe_mode')->end()
->arrayNode('config')->end()
->booleanNode('routes')->end()
->end()
->scalarNode('token')->end()
->booleanNode('safeMode')->end()
->booleanNode('routes')->end()
->arrayNode('config')
->children()
->scalarNode('apiUrl')->defaultValue(\SergiX44\Nutgram\Configuration::DEFAULT_API_URL)->end()
->scalarNode('botId')->defaultNull()->end()
->scalarNode('botName')->defaultNull()->end()
->booleanNode('testEnv')->defaultFalse()->end()
->scalarNode('clientTimeout')->defaultValue(\SergiX44\Nutgram\Configuration::DEFAULT_CLIENT_TIMEOUT)->end()
->arrayNode('clientOptions')->end()
->scalarNode('localPathTransformer')->defaultNull()->end()
->scalarNode('pollingTimeout')->defaultValue(\SergiX44\Nutgram\Configuration::DEFAULT_POLLING_TIMEOUT)->end()
->scalarNode('pollingLimit')->defaultValue(\SergiX44\Nutgram\Configuration::DEFAULT_POLLING_LIMIT)->end()
->arrayNode('pollingAllowedUpdates')->end()
->end()
->end();

return $treeBuilder;
Expand Down
27 changes: 19 additions & 8 deletions src/DependencyInjection/Factory/NutgramFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use SergiX44\Nutgram\Configuration;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\RunningMode\Polling;
use SergiX44\Nutgram\RunningMode\Webhook;
Expand All @@ -23,18 +24,28 @@ public function createNutgram(
?LoggerInterface $nutgramLogger,
?LoggerInterface $nutgramConsoleLogger
): Nutgram {
$isCli = \PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg';

$configuration = new Configuration(
apiUrl: $config['config']['apiUrl'],
botId: $config['config']['botId'],
botName: $config['config']['botName'],
testEnv: $config['config']['testEnv'],
clientTimeout: $config['config']['clientTimeout'],
clientOptions: $config['config']['clientOptions'] ?? [],
container: $container,
cache: new Psr16Cache($nutgramCache),
logger: $isCli ? $nutgramConsoleLogger : $nutgramLogger,
localPathTransformer: $config['config']['localPathTransformer'],
pollingTimeout: $config['config']['pollingTimeout'],
pollingLimit: $config['config']['pollingLimit'],
);

if ($kernel->getEnvironment() === 'test') {
return Nutgram::fake();
return Nutgram::fake(config: $configuration);
}

$isCli = \PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg';

$bot = new Nutgram($config['token'], array_merge([
'container' => $container,
'cache' => new Psr16Cache($nutgramCache),
'logger' => $isCli ? $nutgramConsoleLogger : $nutgramLogger,
], $config['config'] ?? []));
$bot = new Nutgram($config['token'], $configuration);

if ($isCli) {
$bot->setRunningMode(Polling::class);
Expand Down
2 changes: 1 addition & 1 deletion src/NutgramConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(array $config, Nutgram $bot, KernelInterface $kernel

public function configure()
{
if (!$this->config['routes']) {
if (!($this->config['routes'] ?? false)) {
return;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace SergiX44\NutgramBundle\Tests\Fixtures;

use SergiX44\NutgramBundle\NutgramBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{

public function registerBundles(): iterable
{
return [new FrameworkBundle(), new NutgramBundle()];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/test_config.yaml');
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
framework:
secret: "Three can keep a secret, if two of them are dead."
test: ~

nutgram:
token: '%env(string:TELEGRAM_TOKEN)%'
safeMode: false
routes: true
config:
botId: 123
apiUrl: 'BlaBla'
botName: 'EEE'
testEnv: true
pollingTimeout: 123
pollingLimit: 456
17 changes: 17 additions & 0 deletions tests/Functional/BundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use SergiX44\Nutgram\Nutgram;

it('returns an instance', function () {
/** @var Nutgram $instance */
$instance = static::getContainer()->get(Nutgram::class);

expect($instance)->toBeInstanceOf(Nutgram::class);
});

it('returns an instance with the alias', function () {
/** @var Nutgram $instance */
$instance = static::getContainer()->get('nutgram');

expect($instance)->toBeInstanceOf(Nutgram::class);
});
Loading

0 comments on commit 9046d47

Please sign in to comment.