Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"php": ">=8.3",
"php-amqplib/php-amqplib": "^3.7",
"utopia-php/di": "0.3.*",
"utopia-php/servers": "0.3.*",
"utopia-php/servers": "dev-feat-param-aliases as 0.3.99",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Dev-branch dependency must be replaced before merging

utopia-php/servers is pinned to dev-feat-param-aliases as 0.3.99, a feature branch alias that is not a stable release. Any consumer running composer update or a fresh composer install will receive whatever is on that branch at that moment, making the build non-reproducible and potentially broken once the branch is rebased or deleted. This should be replaced with a concrete stable version (e.g. "0.3.*" or a specific tag) after the corresponding utopia-php/servers PR is merged and tagged.

"utopia-php/pools": "1.*",
"utopia-php/telemetry": "0.2.*",
"utopia-php/validators": "0.2.*"
Expand Down
194 changes: 100 additions & 94 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/Queue/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,18 @@ protected function getArguments(Container $container, Hook $hook, array $payload
{
$arguments = [];
foreach ($hook->getParams() as $key => $param) {
$payloadKey = $key;
if (!\array_key_exists($key, $payload) && !empty($param['aliases'])) {
foreach ($param['aliases'] as $alias) {
if (\array_key_exists($alias, $payload)) {
$payloadKey = $alias;
break;
}
}
}

// Get value from route or request object
$value = $payload[$key] ?? $param['default'];
$value = $payload[$payloadKey] ?? $param['default'];
$value =
$value === '' || $value === null ? $param['default'] : $value;

Expand Down
37 changes: 37 additions & 0 deletions tests/Queue/E2E/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,43 @@ public function testEnqueuePriority(): void
$this->assertTrue($result);
}

public function testParamAliases(): void
{
$publisher = $this->getPublisher();

// Resolves via canonical key
$this->assertTrue($publisher->enqueue($this->getQueue(), [
'type' => 'test_alias',
'aliasValue' => 'canonical',
'value' => 'canonical',
]));

// Resolves via first alias when canonical absent
$this->assertTrue($publisher->enqueue($this->getQueue(), [
'type' => 'test_alias',
'alias_value' => 'first-alias',
'value' => 'first-alias',
]));

// Falls through to later alias when earlier ones absent
$this->assertTrue($publisher->enqueue($this->getQueue(), [
'type' => 'test_alias',
'aliased' => 'second-alias',
'value' => 'second-alias',
]));

// Canonical key wins when both canonical and aliases are present
$this->assertTrue($publisher->enqueue($this->getQueue(), [
'type' => 'test_alias',
'aliasValue' => 'canonical-wins',
'alias_value' => 'should-lose',
'aliased' => 'should-lose',
'value' => 'canonical-wins',
]));

sleep(1);
}

/**
* @depends testEvents
*/
Expand Down
13 changes: 12 additions & 1 deletion tests/Queue/servers/AMQP/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
use Utopia\Queue\Broker\AMQP;
use Utopia\Queue\Adapter\Swoole;
use Utopia\Queue\Server;
use Utopia\Validator\Text;

$consumer = new AMQP(host: 'amqp', port: 5672, user: 'amqp', password: 'amqp');
$adapter = new Swoole($consumer, 12, 'amqp');
$server = new Server($adapter);

$server->job()->inject('message')->action(handleRequest(...));
$server->job()
->inject('message')
->param(
key: 'aliasValue',
default: '',
validator: new Text(length: 255, min: 0),
description: 'alias resolution test value',
optional: true,
aliases: ['alias_value', 'aliased'],
)
->action(handleRequest(...));

$server
->error()
Expand Down
13 changes: 12 additions & 1 deletion tests/Queue/servers/Swoole/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@
use Utopia\Queue\Adapter\Swoole;
use Utopia\Queue\Connection\Redis as RedisConnection;
use Utopia\Queue\Broker\Redis;
use Utopia\Validator\Text;

$consumer = new Redis(new RedisConnection('redis'));
$adapter = new Swoole($consumer, 12, 'swoole');
$server = new Server($adapter);

$server->job()->inject('message')->action(handleRequest(...));
$server->job()
->inject('message')
->param(
key: 'aliasValue',
default: '',
validator: new Text(length: 255, min: 0),
description: 'alias resolution test value',
optional: true,
aliases: ['alias_value', 'aliased'],
)
->action(handleRequest(...));

$server
->error()
Expand Down
13 changes: 12 additions & 1 deletion tests/Queue/servers/SwooleRedisCluster/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Utopia\Queue\Connection\RedisCluster;
use Utopia\Queue\Adapter\Swoole;
use Utopia\Queue\Server;
use Utopia\Validator\Text;

$consumer = new Redis(
new RedisCluster([
Expand All @@ -18,7 +19,17 @@
$adapter = new Swoole($consumer, 12, 'swoole-redis-cluster');
$server = new Server($adapter);

$server->job()->inject('message')->action(handleRequest(...));
$server->job()
->inject('message')
->param(
key: 'aliasValue',
default: '',
validator: new Text(length: 255, min: 0),
description: 'alias resolution test value',
optional: true,
aliases: ['alias_value', 'aliased'],
)
->action(handleRequest(...));

$server
->error()
Expand Down
13 changes: 12 additions & 1 deletion tests/Queue/servers/Workerman/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@
use Utopia\Queue\Adapter\Workerman;
use Utopia\Queue\Connection\Redis as RedisConnection;
use Utopia\Queue\Broker\Redis;
use Utopia\Validator\Text;

$consumer = new Redis(new RedisConnection('redis'));
$adapter = new Workerman($consumer, 12, 'wokerman');
$server = new Queue\Server($adapter);

$server->job()->inject('message')->action(handleRequest(...));
$server->job()
->inject('message')
->param(
key: 'aliasValue',
default: '',
validator: new Text(length: 255, min: 0),
description: 'alias resolution test value',
optional: true,
aliases: ['alias_value', 'aliased'],
)
->action(handleRequest(...));

$server
->error()
Expand Down
7 changes: 6 additions & 1 deletion tests/Queue/servers/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Utopia\Queue;

function handleRequest(Queue\Message $job): void
function handleRequest(Queue\Message $job, ?string $aliasValue = null): void
{
$type = $job->getPayload()['type'];
$value = $job->getPayload()['value'] ?? null;
Expand Down Expand Up @@ -38,6 +38,11 @@ function handleRequest(Queue\Message $job): void
assert($value['bool'] === true);
assert($value['null'] === null);

break;
case 'test_alias':
// payload's `value` field carries the expected resolved alias value
assert($aliasValue === $value);

break;
case 'test_exception':
assert(false);
Expand Down
Loading