Skip to content
Merged
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
42 changes: 42 additions & 0 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,46 @@ public function testCallableStringParametersNotExecuted(): void

$this->assertEquals('generated: generated-value', $result);
}

public function testCanInjectResourceAndParamWithSameName(): void
{
// Register a 'locale' resource returning a Locale instance whose
// `name` statically resolves to "en".
$this->container->set('locale', fn () => new Locale());

$route = new Route('GET', '/path');

$route
->param('locale', 'en-default', new Text(10), 'locale param', false)
->inject('locale')
->action(function (string $localeParam, Locale $localeResource) {
echo \json_encode([
'localeParam' => $localeParam,
'localeResource' => $localeResource->name,
]);
});

\ob_start();
$request = new UtopiaFPMRequestTest();
$request::_setParams(['locale' => 'es']);
$this->http->execute($route, $request, new Response());
$result = \ob_get_contents();
Comment thread
Meldiron marked this conversation as resolved.
\ob_end_clean();

$expected = \json_encode([
'localeParam' => 'es',
'localeResource' => 'en',
]);

$this->assertEquals($expected, $result);
}
}

/**
* Dummy Locale class used by testCanInjectResourceAndParamWithSameName to
* verify resource injection alongside a same-named request parameter.
*/
class Locale
{
public string $name = 'en';
}
Comment on lines +690 to 693
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Generic helper class name may collide

Locale is a very common class name. Defining it in the Utopia\Http namespace at file scope means any future addition of a real Locale class to this namespace (or a test file included alongside this one) would cause a fatal "Cannot redeclare class" error. Naming it something test-specific (e.g. TestLocale or LocaleStub) eliminates the risk at no cost.

Suggested change
class Locale
{
public string $name = 'en';
}
class TestLocale
{
public string $name = 'en';
}

You would also need to update the container registration and the action's type hint accordingly:

$this->container->set('locale', fn () => new TestLocale());
// ...
->action(function (string $localeParam, TestLocale $localeResource) {

Loading