Skip to content

[9.x] Use get methods to access application locale - #44521

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
abenevaut:fix-access-config-translation-service-provider
Oct 9, 2022
Merged

[9.x] Use get methods to access application locale#44521
taylorotwell merged 2 commits into
laravel:9.xfrom
abenevaut:fix-access-config-translation-service-provider

Conversation

@abenevaut

@abenevaut abenevaut commented Oct 9, 2022

Copy link
Copy Markdown

This PR allows to easily mock access to config (app.locale & app.fallback_locale) in TranslationServiceProvider.

I got this error

Error: Cannot use object of type Mockery_0_stdClass as array

/vendor/laravel/framework/src/Illuminate/Translation/TranslationServiceProvider.php:25

When I apply similar mock as https://github.com/laravel/framework/blob/9.x/tests/Foundation/FoundationApplicationTest.php#L22

class CommandTest extends TestCase
{
    protected Application $app;

    protected function setUp(): void
    {
        $this->app = new Application();
        $this->app['config'] = m::mock(\stdClass::class);
        $this->app['env'] = 'testing';

        $this->app->register(ValidationServiceProvider::class);
        $this->app->register(TranslationServiceProvider::class);
        $this->app->register(FilesystemServiceProvider::class);

        $this->app->instance('path.config', __DIR__ . '/../config');
        $this->app->instance('path.storage', __DIR__ . '/../storage');
        $this->app->instance('path.lang', __DIR__ . '/../lang');

        $this->app->boot();

        $this
            ->app['config']
            ->shouldReceive('get')
            ->once()
            ->with('app.locale')
            ->andReturns('en');
        $this
            ->app['config']
            ->shouldReceive('get')
            ->once()
            ->with('app.fallback_locale')
            ->andReturns('en');

        Facade::setFacadeApplication($this->app);
    }

@abenevaut
abenevaut marked this pull request as ready for review October 9, 2022 09:42
@abenevaut abenevaut changed the title Use get method to access config for application locale Use get methods to access application locale Oct 9, 2022
@taylorotwell
taylorotwell merged commit 0fbbbf2 into laravel:9.x Oct 9, 2022
@abenevaut
abenevaut deleted the fix-access-config-translation-service-provider branch October 9, 2022 14:42
@GrahamCampbell GrahamCampbell changed the title Use get methods to access application locale [9.x] Use get methods to access application locale Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants