Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running mode tests #482

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<directory>./src/Telegram/Properties</directory>
<directory>./src/Telegram/Endpoints</directory>
<directory>./src/Telegram/Types</directory>
<directory>./src/RunningMode</directory>
<directory>./src/Testing/Constraints</directory>
<file>./src/Support/StrUtils.php</file>
<file>./src/Telegram/CustomEndpoints.php</file>
Expand Down
76 changes: 76 additions & 0 deletions src/Handlers/FiresHandlers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace SergiX44\Nutgram\Handlers;

use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
use Throwable;

trait FiresHandlers
{
/**
* @param string $type
* @param array $parameters
* @return mixed
* @throws Throwable
*/
protected function fireHandlersBy(string $type, array $parameters = []): mixed
{
$handlers = [];
$this->addHandlersBy($handlers, $type);
return $this->fireHandlers($handlers, $parameters);
}

/**
* @param array $handlers
* @param array $parameters
* @return mixed
* @throws Throwable
*/
protected function fireHandlers(array $handlers, array $parameters = []): mixed
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fireHandlers has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.

{
$result = null;

/** @var Handler $handler */
foreach ($handlers as $handler) {
try {
$this->currentHandler = $handler;
$result = $handler->addParameters($parameters)->getHead()($this);
} catch (Throwable $e) {
if (!empty($this->handlers[self::EXCEPTION])) {
$this->fireExceptionHandlerBy(self::EXCEPTION, $e);
continue;
}

throw $e;
}
}
$this->currentHandler = null;

return $result;
}

/**
* @param string $type
* @param Throwable $e
* @return mixed
*/
protected function fireExceptionHandlerBy(string $type, Throwable $e): mixed
{
$handlers = [];

if ($e instanceof TelegramException) {
$this->addHandlersBy($handlers, $type, value: $e->getMessage());
} else {
$this->addHandlersBy($handlers, $type, $e::class);
}


if (empty($handlers)) {
$this->addHandlersBy($handlers, $type);
}

/** @var Handler $handler */
$handler = reset($handlers)->setParameters($e);
return $handler($this);
}
}
71 changes: 2 additions & 69 deletions src/Nutgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use SergiX44\Nutgram\Cache\GlobalCache;
use SergiX44\Nutgram\Cache\UserCache;
use SergiX44\Nutgram\Conversations\Conversation;
use SergiX44\Nutgram\Handlers\FiresHandlers;
use SergiX44\Nutgram\Handlers\Handler;
use SergiX44\Nutgram\Handlers\ResolveHandlers;
use SergiX44\Nutgram\Handlers\Type\Command;
Expand All @@ -29,15 +30,14 @@
use SergiX44\Nutgram\RunningMode\RunningMode;
use SergiX44\Nutgram\Support\BulkMessenger;
use SergiX44\Nutgram\Telegram\Client;
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
use SergiX44\Nutgram\Telegram\Types\Command\BotCommandScope;
use SergiX44\Nutgram\Telegram\Types\Common\Update;
use SergiX44\Nutgram\Testing\FakeNutgram;
use Throwable;

class Nutgram extends ResolveHandlers
{
use Client, UpdateDataProxy, GlobalCacheProxy, UserCacheProxy;
use Client, UpdateDataProxy, GlobalCacheProxy, UserCacheProxy, FiresHandlers;

/**
* @var string
Expand Down Expand Up @@ -233,73 +233,6 @@ public function processUpdate(Update $update): void
$this->fireHandlers($handlers);
}

/**
* @param string $type
* @param array $parameters
* @return mixed
* @throws Throwable
*/
protected function fireHandlersBy(string $type, array $parameters = []): mixed
{
$handlers = [];
$this->addHandlersBy($handlers, $type);
return $this->fireHandlers($handlers, $parameters);
}

/**
* @param array $handlers
* @param array $parameters
* @return mixed
* @throws Throwable
*/
protected function fireHandlers(array $handlers, array $parameters = []): mixed
{
$result = null;

/** @var Handler $handler */
foreach ($handlers as $handler) {
try {
$this->currentHandler = $handler;
$result = $handler->addParameters($parameters)->getHead()($this);
} catch (Throwable $e) {
if (!empty($this->handlers[self::EXCEPTION])) {
$this->fireExceptionHandlerBy(self::EXCEPTION, $e);
continue;
}

throw $e;
}
}
$this->currentHandler = null;

return $result;
}

/**
* @param string $type
* @param Throwable $e
* @return mixed
*/
protected function fireExceptionHandlerBy(string $type, Throwable $e): mixed
{
$handlers = [];

if ($e instanceof TelegramException) {
$this->addHandlersBy($handlers, $type, value: $e->getMessage());
} else {
$this->addHandlersBy($handlers, $type, $e::class);
}


if (empty($handlers)) {
$this->addHandlersBy($handlers, $type);
}

/** @var Handler $handler */
$handler = reset($handlers)->setParameters($e);
return $handler($this);
}

/**
* @param Conversations\Conversation|callable $callable
* @param int|null $userId
Expand Down
7 changes: 3 additions & 4 deletions src/RunningMode/Polling.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@

class Polling implements RunningMode
{
/**
* @param Nutgram $bot
*/
public static bool $FOREVER = true;

public function processUpdates(Nutgram $bot): void
{
$config = $bot->getConfig();
$offset = 1;

print("Listening...\n");
while (true) {
while (self::$FOREVER) {
$updates = $bot->getUpdates(
offset: $offset,
limit: $config->pollingLimit,
Expand Down
62 changes: 34 additions & 28 deletions src/Testing/Asserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
trait Asserts
{
/**
* @param callable $closure
* @param int $index
* @param callable $closure
* @param int $index
* @return $this
*/
public function assertRaw(callable $closure, int $index = 0): self
Expand All @@ -33,8 +33,8 @@ public function assertRaw(callable $closure, int $index = 0): self
}

/**
* @param string $method
* @param int $times
* @param string $method
* @param int $times
* @return $this
*/
public function assertCalled(string $method, int $times = 1): self
Expand All @@ -55,9 +55,9 @@ public function assertCalled(string $method, int $times = 1): self
}

/**
* @param string|string[] $method
* @param array|null $expected
* @param int $index
* @param string|string[] $method
* @param array|null $expected
* @param int $index
* @return $this
*/
public function assertReply(string|array $method, ?array $expected = null, int $index = 0): self
Expand Down Expand Up @@ -86,9 +86,9 @@ public function assertReply(string|array $method, ?array $expected = null, int $
}

/**
* @param array $expected
* @param int $index
* @param string|null $forceMethod
* @param array $expected
* @param int $index
* @param string|null $forceMethod
* @return $this
*/
public function assertReplyMessage(array $expected, int $index = 0, ?string $forceMethod = null): self
Expand All @@ -97,8 +97,8 @@ public function assertReplyMessage(array $expected, int $index = 0, ?string $for
}

/**
* @param string $expected
* @param int $index
* @param string $expected
* @param int $index
* @return $this
*/
public function assertReplyText(string $expected, int $index = 0): self
Expand All @@ -107,37 +107,27 @@ public function assertReplyText(string $expected, int $index = 0): self
}

/**
* @param int|null $userId
* @param int|null $chatId
* @param int|null $userId
* @param int|null $chatId
* @return $this
*/
public function assertActiveConversation(?int $userId = null, ?int $chatId = null): self
{
$userId = $this->storedUser?->id ?? $userId;
$chatId = $this->storedChat?->id ?? $chatId;

if ($userId === null || $chatId === null) {
throw new InvalidArgumentException('You cannot do this assert without userId and chatId.');
}
[$userId, $chatId] = $this->checkUserChatIds($userId, $chatId);

PHPUnit::assertNotNull($this->currentConversation($userId, $chatId), 'No active conversation found');

return $this;
}

/**
* @param int|null $userId
* @param int|null $chatId
* @param int|null $userId
* @param int|null $chatId
* @return $this
*/
public function assertNoConversation(?int $userId = null, ?int $chatId = null): self
{
$userId = $this->storedUser?->id ?? $userId;
$chatId = $this->storedChat?->id ?? $chatId;

if ($userId === null || $chatId === null) {
throw new InvalidArgumentException('You cannot do this assert without userId and chatId.');
}
[$userId, $chatId] = $this->checkUserChatIds($userId, $chatId);

PHPUnit::assertNull($this->currentConversation($userId, $chatId), 'Found an active conversation');

Expand All @@ -163,4 +153,20 @@ protected function assertArraySubset(
$constraint = new ArraySubset($subset, $checkForIdentity);
PHPUnit::assertThat($array, $constraint, $msg);
}

/**
* @param int|null $userId
* @param int|null $chatId
* @return array
*/
private function checkUserChatIds(?int $userId, ?int $chatId): array
{
$userId = $this->storedUser?->id ?? $userId;
$chatId = $this->storedChat?->id ?? $chatId;

if ($userId === null || $chatId === null) {
throw new InvalidArgumentException('You cannot do this assert without userId and chatId.');
}
return [$userId, $chatId];
}
}
Loading
Loading