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] Fix carbon locale when setting it via app locale setter #557

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
"laravel/framework": "^8.81|^9.0",
"laminas/laminas-diactoros": "^2.5",
"laravel/serializable-closure": "^1.0",
"nesbot/carbon": "^2.60",
"symfony/psr-http-message-bridge": "^2.0"
},
"repositories": [
{
"type": "github",
"url": "https://github.com/briannesbitt/Carbon"
}
],
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
"require-dev": {
"guzzlehttp/guzzle": "^7.2",
"mockery/mockery": "^1.4",
Expand Down
6 changes: 5 additions & 1 deletion src/Listeners/FlushLocaleState.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function handle($event): void
$translator->setFallback($config->get('app.fallback_locale'));
});

(new CarbonServiceProvider($event->app))->updateLocale();
$provider = tap(new CarbonServiceProvider($event->app))->updateLocale();

collect($event->sandbox->getProviders($provider))
->values()
->whenNotEmpty(fn ($providers) => $providers->first()->setAppGetter(fn () => $event->sandbox));
}
}
25 changes: 25 additions & 0 deletions tests/LocaleStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,29 @@ public function test_carbon_state_is_reset_across_subsequent_requests()
$this->assertEquals('nl', $client->responses[1]->getContent());
$this->assertEquals('en', $client->responses[2]->getContent());
}

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

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

return [app()->getLocale(), now()->getLocale()];
});

$worker->run();

$this->assertEquals(['en', 'en'], json_decode($client->responses[0]->getContent(), true));
$this->assertEquals(['nl', 'nl'], json_decode($client->responses[1]->getContent(), true));
$this->assertEquals(['en', 'en'], json_decode($client->responses[2]->getContent(), true));
$this->assertEquals(['pt', 'pt'], json_decode($client->responses[3]->getContent(), true));
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Octane\Tests;

use Carbon\Laravel\ServiceProvider as CarbonServiceProvider;
use Laravel\Octane\ApplicationFactory;
use Laravel\Octane\Contracts\Client;
use Laravel\Octane\Octane;
Expand All @@ -21,6 +22,7 @@ protected function createOctaneContext(array $requests)

$appFactory->shouldReceive('createApplication')->andReturn($app = $this->createApplication());

$app->register(new CarbonServiceProvider($app));
$app->register(new OctaneServiceProvider($app));

$worker = new FakeWorker($appFactory, $roadRunnerClient = new FakeClient($requests));
Expand Down