Skip to content

Commit

Permalink
Intergation tests, dependencies reduce.
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Jul 2, 2023
1 parent 201731e commit 6671954
Show file tree
Hide file tree
Showing 23 changed files with 686 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
/tests export-ignore
/phpunit.xml export-ignore
/psalm.xml export-ignore
/.php-cs-fixer.dist.php export-ignore
3 changes: 3 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ jobs:

- name: Psalm
run: vendor/bin/psalm --no-cache

- name: Tests
run: vendor/bin/phpunit
28 changes: 20 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,31 @@
"email": "anton.z@live.com"
}
],
"autoload": {
"psr-4": {
"Luzrain\\TelegramBotBundle\\": "src/"
}
},
"require": {
"php" : "^8.2",
"php": "^8.2",
"luzrain/telegram-bot-api": "^2.1.1",
"symfony/config": "^6.3",
"symfony/console": "^6.3",
"symfony/framework-bundle": "^6.3",
"luzrain/telegram-bot-api": "^2.1.1"
"symfony/dependency-injection": "^6.3",
"symfony/http-kernel": "^6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.20",
"phpunit/phpunit": "^10.2",
"symfony/framework-bundle": "^6.3",
"vimeo/psalm": "^5.13"
},
"autoload": {
"psr-4": {
"Luzrain\\TelegramBotBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Luzrain\\TelegramBotBundle\\Test\\": "tests"
}
},
"config": {
"sort-packages": true
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
cacheDirectory="var"
>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<server name="APP_ENV" value="test" force="true"/>
<server name="KERNEL_CLASS" value="Luzrain\TelegramBotBundle\Test\TestKernel" force="true"/>
</php>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/DependencyInjection/CommandCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function process(ContainerBuilder $container)
// Sort by priority
usort($controllersMap, fn (array $a, array $b) => $b['priority'] <=> $a['priority']);

foreach ($controllersMap as &$row) {
unset($row['priority']);
foreach ($controllersMap as $id => $row) {
unset($controllersMap[$id]['priority']);
}

$container
Expand Down
3 changes: 1 addition & 2 deletions src/DependencyInjection/TelegramBotExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ public function load(array $configs, ContainerBuilder $container)
;

$container
->autowire('telegram_bot.client_api', ClientApi::class)
->register('telegram_bot.client_api', ClientApi::class)
;

$container
->register('telegram_bot.webhook_controller', WebHookController::class)
->setArgument('$updateHandler', new Reference('telegram_bot.update_handler'))
->setArgument('$secretToken', $config['secret_token'])
->addTag('controller.service_arguments')
->addMethodCall('setContainer', [new Reference('service_container')])
;

$container
Expand Down
8 changes: 1 addition & 7 deletions src/TelegramBot/Controller/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@

namespace Luzrain\TelegramBotBundle\TelegramBot\Controller;

use Luzrain\TelegramBotApi\Exception\TelegramCallbackException;
use Luzrain\TelegramBotApi\Exception\TelegramTypeException;
use Luzrain\TelegramBotApi\Type\Update;
use Luzrain\TelegramBotBundle\TelegramBot\UpdateHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

final class WebHookController extends AbstractController
final class WebHookController
{
public function __construct(
private UpdateHandler $updateHandler,
private string|null $secretToken,
) {
}

/**
* @throws TelegramTypeException
* @throws TelegramCallbackException
*/
public function __invoke(Request $request): Response
{
if ($request->getMethod() !== 'POST') {
Expand Down
44 changes: 44 additions & 0 deletions tests/CommandMetadataProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test;

use Luzrain\TelegramBotApi\BotApi;
use Luzrain\TelegramBotApi\ClientApi;
use Luzrain\TelegramBotBundle\Attribute\OnCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\ButtonDeleteCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\ButtonSetCommandsCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\DeleteWebhookCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\PolllingStartCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\SetWebhookCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\WebhookInfoCommand;
use Luzrain\TelegramBotBundle\TelegramBot\CommandMetadataProvider;
use Luzrain\TelegramBotBundle\TelegramBot\Controller\WebHookController;
use Luzrain\TelegramBotBundle\TelegramBot\LongPollingService;
use Luzrain\TelegramBotBundle\TelegramBot\UpdateHandler;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class CommandMetadataProviderTest extends KernelTestCase
{
public function testCommandsMetadata(): void
{
$commandMetadataProvider = self::getContainer()->get('telegram_bot.command_metadata_provider');
$list = $commandMetadataProvider->gelMetadataList();

$this->assertCount(4, $list);
$this->assertObjectInArray(new OnCommand('/start', '', false, 0), $list);
$this->assertObjectInArray(new OnCommand('/test1', 'test1 command description', true, 0), $list);
$this->assertObjectInArray(new OnCommand('/test2', 'test2 command description', true, 0), $list);
$this->assertObjectInArray(new OnCommand('/test3', 'test3 command description', false, 0), $list);
}

private function assertObjectInArray(object $obj, array $arra): void
{
$this->assertContains(json_encode($obj), array_map(fn ($a) => json_encode($a), $arra));
}
}
19 changes: 19 additions & 0 deletions tests/Controller/AnyUpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Controller;

use Luzrain\TelegramBotApi\Event;
use Luzrain\TelegramBotBundle\Attribute\OnEvent;
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
use Luzrain\TelegramBotBundle\Test\Helper\ControllerTestHelper;

final class AnyUpdateController extends TelegramCommand
{
#[OnEvent(event: Event\Update::class, priority: 10)]
public function __invoke(): void
{
ControllerTestHelper::$isUpdate = true;
}
}
20 changes: 20 additions & 0 deletions tests/Controller/CallbackCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Controller;

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotBundle\Attribute\OnCallback;
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
use Luzrain\TelegramBotBundle\Test\Helper\ControllerTestHelper;

final class CallbackCommandController extends TelegramCommand
{
#[OnCallback('test_callback_1')]
public function callback1(): Method
{
ControllerTestHelper::$isCallback1 = true;
return $this->reply('Callback1 answer');
}
}
22 changes: 22 additions & 0 deletions tests/Controller/MessageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Controller;

use Luzrain\TelegramBotApi\Event;
use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;
use Luzrain\TelegramBotBundle\Attribute\OnEvent;
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
use Luzrain\TelegramBotBundle\Test\Helper\ControllerTestHelper;

final class MessageController extends TelegramCommand
{
#[OnEvent(event: Event\Message::class)]
public function __invoke(Type\Message $message): Method
{
ControllerTestHelper::$isMessage = true;
return $this->reply('You wrote: ' . $message->text);
}
}
20 changes: 20 additions & 0 deletions tests/Controller/StartCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Controller;

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotBundle\Attribute\OnCommand;
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
use Luzrain\TelegramBotBundle\Test\Helper\ControllerTestHelper;

final class StartCommandController extends TelegramCommand
{
#[OnCommand('/start')]
public function __invoke(): Method
{
ControllerTestHelper::$isStartCommand = true;
return $this->reply('Start answer');
}
}
34 changes: 34 additions & 0 deletions tests/Controller/TestCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Controller;

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotBundle\Attribute\OnCommand;
use Luzrain\TelegramBotBundle\TelegramBot\TelegramCommand;
use Luzrain\TelegramBotBundle\Test\Helper\ControllerTestHelper;

final class TestCommandController extends TelegramCommand
{
#[OnCommand(command: '/test1', description: 'test1 command description', publish: true)]
public function test1(): Method
{
ControllerTestHelper::$isTest1CommandCommand = true;
return $this->reply('Test1 answer');
}

#[OnCommand(command: '/test2', description: 'test2 command description', publish: true)]
public function test2(): Method
{
ControllerTestHelper::$isTest2CommandCommand = true;
return $this->reply('Test2 answer');
}

#[OnCommand(command: '/test3', description: 'test3 command description', publish: false)]
public function test3(): Method
{
ControllerTestHelper::$isTest3CommandCommand = true;
return $this->reply('Test3 answer');
}
}
27 changes: 27 additions & 0 deletions tests/Helper/ControllerTestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test\Helper;

final class ControllerTestHelper
{
public static bool $isStartCommand = false;
public static bool $isTest1CommandCommand = false;
public static bool $isTest2CommandCommand = false;
public static bool $isTest3CommandCommand = false;
public static bool $isMessage = false;
public static bool $isCallback1 = false;
public static bool $isUpdate = false;

public function __construct()
{
self::$isStartCommand = false;
self::$isTest1CommandCommand = false;
self::$isTest2CommandCommand = false;
self::$isTest3CommandCommand = false;
self::$isMessage = false;
self::$isCallback1 = false;
self::$isUpdate = false;
}
}
52 changes: 52 additions & 0 deletions tests/ServicesAutowiringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\Test;

use Luzrain\TelegramBotApi\BotApi;
use Luzrain\TelegramBotApi\ClientApi;
use Luzrain\TelegramBotBundle\TelegramBot\Command\ButtonDeleteCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\ButtonSetCommandsCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\DeleteWebhookCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\PolllingStartCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\SetWebhookCommand;
use Luzrain\TelegramBotBundle\TelegramBot\Command\WebhookInfoCommand;
use Luzrain\TelegramBotBundle\TelegramBot\CommandMetadataProvider;
use Luzrain\TelegramBotBundle\TelegramBot\Controller\WebHookController;
use Luzrain\TelegramBotBundle\TelegramBot\LongPollingService;
use Luzrain\TelegramBotBundle\TelegramBot\UpdateHandler;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class ServicesAutowiringTest extends KernelTestCase
{
private static ContainerInterface $container;

public function setUp(): void
{
self::$container = self::getContainer();
self::$container->set('httpClient', $this->createMock(ClientInterface::class));
self::$container->set('requestFactory', $this->createMock(RequestFactoryInterface::class));
self::$container->set('streamFactory', $this->createMock(StreamFactoryInterface::class));
}

public function testServiceAutowiring(): void
{
$this->assertInstanceOf(BotApi::class, self::$container->get(BotApi::class));
$this->assertInstanceOf(ClientApi::class, self::$container->get('telegram_bot.client_api'));
$this->assertInstanceOf(WebHookController::class, self::$container->get('telegram_bot.webhook_controller'));
$this->assertInstanceOf(LongPollingService::class, self::$container->get('telegram_bot.long_polling_service'));
$this->assertInstanceOf(SetWebhookCommand::class, self::$container->get('telegram_bot.set_webhook_command'));
$this->assertInstanceOf(WebhookInfoCommand::class, self::$container->get('telegram_bot.get_webhook_command'));
$this->assertInstanceOf(DeleteWebhookCommand::class, self::$container->get('telegram_bot.delete_webhook_command'));
$this->assertInstanceOf(PolllingStartCommand::class, self::$container->get('telegram_bot.polling_command'));
$this->assertInstanceOf(ButtonSetCommandsCommand::class, self::$container->get('telegram_bot.menu_button_set_commands'));
$this->assertInstanceOf(ButtonDeleteCommand::class, self::$container->get('telegram_bot.menu_button_delete'));
$this->assertInstanceOf(UpdateHandler::class, self::$container->get('telegram_bot.update_handler'));
$this->assertInstanceOf(CommandMetadataProvider::class, self::$container->get('telegram_bot.command_metadata_provider'));
}
}
Loading

0 comments on commit 6671954

Please sign in to comment.