Skip to content

Commit

Permalink
UrlGenerator Sandbox (#896)
Browse files Browse the repository at this point in the history
* UrlGenerator Sandbox

* Fix Styl
  • Loading branch information
dbpolito committed May 23, 2024
1 parent e8d9d78 commit 54a34e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Concerns/ProvidesDefaultConfigurationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function prepareApplicationForNextOperation(): array
{
return [
\Laravel\Octane\Listeners\CreateConfigurationSandbox::class,
\Laravel\Octane\Listeners\CreateUrlGeneratorSandbox::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToAuthorizationGate::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToBroadcastManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToDatabaseManager::class,
Expand Down
16 changes: 16 additions & 0 deletions src/Listeners/CreateUrlGeneratorSandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Laravel\Octane\Listeners;

class CreateUrlGeneratorSandbox
{
/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
$event->sandbox->instance('url', clone $event->sandbox['url']);
}
}
1 change: 1 addition & 0 deletions src/OctaneServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected function bindListeners()
{
$this->app->singleton(Listeners\CollectGarbage::class);
$this->app->singleton(Listeners\CreateConfigurationSandbox::class);
$this->app->singleton(Listeners\CreateUrlGeneratorSandbox::class);
$this->app->singleton(Listeners\DisconnectFromDatabases::class);
$this->app->singleton(Listeners\EnforceRequestScheme::class);
$this->app->singleton(Listeners\EnsureRequestServerPortMatchesScheme::class);
Expand Down
49 changes: 49 additions & 0 deletions tests/UrlGeneratorSandboxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Laravel\Octane\Tests;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;

class UrlGeneratorSandboxTest extends TestCase
{
public function test_url_is_reset_between_requests()
{
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/first', 'GET'),
Request::create('/second', 'GET'),
]);

$app['url']->defaults(['param' => 'original']);
$app['url']->forceRootUrl('http://original');

$app['router']->get('/first', function (Application $app) {
$app['url']->defaults(['param' => 'changed']);
$app['url']->forceRootUrl('http://changed');

return [
'default' => $app['url']->getDefaultParameters(),
'url' => $app['url']->to('/'),
];
});

$app['router']->get('/second', function (Application $app) {
return [
'default' => $app['url']->getDefaultParameters(),
'url' => $app['url']->to('/'),
];
});

$worker->run();

$this->assertEquals([
'default' => ['param' => 'changed'],
'url' => 'http://changed',
], $client->responses[0]->getData(true));

$this->assertEquals([
'default' => ['param' => 'original'],
'url' => 'http://original',
], $client->responses[1]->getData(true));
}
}

0 comments on commit 54a34e0

Please sign in to comment.