Skip to content

Commit

Permalink
[1.x] Fixes sharing "Carbon" state shared between requests (#552)
Browse files Browse the repository at this point in the history
* Fixes shared Carbon Locale between requests

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
nunomaduro and StyleCIBot committed Jul 15, 2022
1 parent 950afc8 commit 6f482eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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());
}
}

0 comments on commit 6f482eb

Please sign in to comment.