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

Add tags + macroable to HandlerGroup class #519

Merged
merged 1 commit into from
Jul 4, 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
4 changes: 4 additions & 0 deletions src/Handlers/HandlerGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace SergiX44\Nutgram\Handlers;

use Closure;
use Illuminate\Support\Traits\Macroable;
use SergiX44\Nutgram\Support\Taggable;
use SergiX44\Nutgram\Telegram\Types\Command\BotCommandScope;

class HandlerGroup
{
use Taggable, Macroable;

protected array $middlewares = [];

protected array $scopes = [];
Expand Down
14 changes: 10 additions & 4 deletions src/Handlers/ResolveHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,29 @@ protected function resolveGroups(): void
* @param array $currentScopes
* @return void
*/
private function resolveNestedGroups(array $groups, array $currentMiddlewares = [], array $currentScopes = [])
{
private function resolveNestedGroups(
array $groups,
array $currentMiddlewares = [],
array $currentScopes = [],
array $currentTags = []
) {
foreach ($groups as $group) {
$middlewares = [...$group->getMiddlewares(), ...$currentMiddlewares,];
$scopes = [...$currentScopes, ...$group->getScopes()];
$tags = [...$currentTags, ...$group->getTags()];
$this->groupHandlers = [];
($group->groupCallable)($this);

// apply the middleware stack to the current registered group handlers
array_walk_recursive($this->groupHandlers, function ($leaf) use ($middlewares, $scopes) {
array_walk_recursive($this->groupHandlers, function ($leaf) use ($tags, $middlewares, $scopes) {
if ($leaf instanceof Handler) {
foreach ($middlewares as $middleware) {
$leaf->middleware($middleware);
}
if ($leaf instanceof Command && !empty($scopes)) {
$leaf->scope($scopes);
}
$leaf->tags([...$leaf->getTags(), ...$tags]);
}
});

Expand All @@ -275,7 +281,7 @@ private function resolveNestedGroups(array $groups, array $currentMiddlewares =
if (!empty($this->groups)) {
$groups = $this->groups;
$this->groups = [];
$this->resolveNestedGroups($groups, $middlewares, $scopes);
$this->resolveNestedGroups($groups, $middlewares, $scopes, $tags);
}
}
}
Expand Down
54 changes: 50 additions & 4 deletions tests/Feature/TaggableHandlersTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

use SergiX44\Nutgram\Handlers\Handler;
use SergiX44\Nutgram\Handlers\HandlerGroup;
use SergiX44\Nutgram\Handlers\Type\Command;
use SergiX44\Nutgram\Nutgram;

test('setMeta + getMeta + hasMeta', function () {
test('setTag + getTag + hasTag', function () {
$bot = Nutgram::fake();

$bot->middleware(function (Nutgram $bot, $next) {
Expand All @@ -22,7 +23,7 @@
$bot->hearText('/start')->reply();
});

test('setMetas + removeMeta', function () {
test('setTags + removeTag', function () {
$bot = Nutgram::fake();

$bot->middleware(function (Nutgram $bot, $next) {
Expand All @@ -42,7 +43,7 @@
$bot->hearText('/start')->reply();
});

test('clearMetas', function () {
test('clearTags', function () {
$bot = Nutgram::fake();

$bot->middleware(function (Nutgram $bot, $next) {
Expand Down Expand Up @@ -88,7 +89,7 @@ public function handle(Nutgram $bot): void
$bot->hearText('/start')->reply();
});

test('use meta + macroable', function () {
test('use tag + macroable', function () {
Handler::macro('emotions', function (int $happiness, int $sadness) {
return $this
->tag('happiness', $happiness)
Expand All @@ -111,3 +112,48 @@ public function handle(Nutgram $bot): void

$bot->hearText('/start')->reply();
});


test('set tags using group', function () {
$bot = Nutgram::fake();

$bot->middleware(function (Nutgram $bot, $next) {
expect($bot->currentHandler())
->getTag('tag1')->toBe('foo')
->getTag('tag2')->toBe('bar');

$next($bot);
});

$bot->group(function (Nutgram $bot) {
$bot->onCommand('start', function (Nutgram $bot) {
$bot->sendMessage('Hello!');
})->tag('tag2', 'bar');
})->tag('tag1', 'foo');

$bot->hearText('/start')->reply();
});

test('set tags + macroable using group', function () {
HandlerGroup::macro('emotions', function (int $happiness, int $sadness) {
return $this->tag('happiness', $happiness)->tag('sadness', $sadness);
});

$bot = Nutgram::fake();

$bot->middleware(function (Nutgram $bot, $next) {
expect($bot->currentHandler())
->getTag('happiness')->toBe(80)
->getTag('sadness')->toBe(20)
->getTag('tag1')->toBe('foo');

$next($bot);
});
$bot->group(function (Nutgram $bot) {
$bot->onCommand('start', function (Nutgram $bot) {
$bot->sendMessage('Hello');
})->tag('tag1', 'foo');
})->emotions(80, 20);

$bot->hearText('/start')->reply();
});