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

[1.x] Fixes sharing "Carbon" state shared between requests #552

Merged
merged 2 commits into from
Jul 15, 2022
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/Listeners/FlushLocaleState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laravel\Octane\Listeners;

use Carbon\Laravel\ServiceProvider as CarbonServiceProvider;

class FlushLocaleState
{
/**
Expand All @@ -18,5 +20,7 @@ public function handle($event): void
$translator->setLocale($config->get('app.locale'));
$translator->setFallback($config->get('app.fallback_locale'));
});

(new CarbonServiceProvider($event->app))->updateLocale();
}
}
24 changes: 24 additions & 0 deletions tests/LocaleStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Octane\Tests;

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

Expand Down Expand Up @@ -29,4 +30,27 @@ public function test_translator_state_is_reset_across_subsequent_requests()
$this->assertEquals('en', $client->responses[1]->getContent());
$this->assertEquals('ms', $client->responses[2]->getContent());
}

public function test_carbon_state_is_reset_across_subsequent_requests()
{
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/test-locale', 'GET'), // should be "en"
Request::create('/test-locale?locale=nl', 'GET'),
Request::create('/test-locale', 'GET'), // should be "en", and not "nl"...
]);

$app['router']->get('/test-locale', function (Application $app, Request $request) {
if ($request->has('locale')) {
Carbon::setLocale($request->query('locale'));
}

return now()->getLocale();
});

$worker->run();

$this->assertEquals('en', $client->responses[0]->getContent());
$this->assertEquals('nl', $client->responses[1]->getContent());
$this->assertEquals('en', $client->responses[2]->getContent());
}
}