mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.35k stars 510 forks source link

Tests not working #694

Open sukiyanen85 opened 4 years ago

sukiyanen85 commented 4 years ago

Can't run tests using localization

After reading your documentation I have added the following code to the /tests/TestCase.php file:

protected function refreshApplicationWithLocale($locale)
{
    self::tearDown();
    putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
    self::setUp();
}

protected function tearDown()
{
    putenv(LaravelLocalization::ENV_ROUTE_KEY);
    parent::tearDown();
}

When I run the test it returns:

Fatal error: Declaration of Tests\TestCase::tearDown() must be compatible with Illuminate\Foundation\Testing\TestCase::tearDown(): void

Expected behavior According to the documentation, we should be able to run tests with localization after adding this code

More info:

yamenarahman commented 4 years ago
protected function tearDown() : void
sukiyanen85 commented 4 years ago

I have added the :void to tearDown() function

    protected function tearDown():void
    {
        putenv(LaravelLocalization::ENV_ROUTE_KEY);
        parent::tearDown();
    }

I'm trying to test that an email is sent when we fill the contact form. However when I run this test:

class ContactsControllerTest extends TestCase
{
    public function test_contact_email_is_sent(){
        $this->refreshApplicationWithLocale('en');
        Mail::fake();

        $response = $this->post(route('contacts.store'), [
            'name'       => 'Test',
            'country_id' => 1,
            'phone'      => '123456789',
            'email'      => 'email@email.com',
            'message'    => 'This is the message, with 15 characters',
            '_token'     => Session::token()
        ]);

        $response->dump();
        $response->assertSessionHasErrors();

        Mail::assertQueued(ContactMail::class);
    }
}

The $response->dump() is showing:

Redirecting to http://localhost.

This is my store function:

    public function store(ContactAddRequest $contact){
        Mail::send(new ContactMail($contact));

        session()->flash('success', __('Thank you for your contact. We will reply you as soon as possible'));

        return redirect(route('contacts.index'));
    }

And my route: Route::post(LaravelLocalization::transRoute('routes.contacts'), 'ContactsController@store')->name('contacts.store');

kenmaclord commented 4 years ago

Same problem here, the Redirect Response Url is empty. Let's imagine this story : url: /order localized url : /fr/order redirect url (if unauthenticated) : /fr/login

In my tests, I expect a testing workflow like this : $this->get('/order') ->assertStatus(302) ->assertRedirect('/login');

But it doesn't work. The response location says '/fr'

I find this package wonderful but very difficult to test. Anyone has an idea to make that easier ?