laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

Tests not working

Open sukiyanen85 opened this issue 4 years ago • 3 comments

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:

  • Version of Laravel 6.12.0
  • Version of the Laravel-localization package: 1.4

sukiyanen85 avatar Jan 25 '20 22:01 sukiyanen85

protected function tearDown() : void

yamenarahman avatar Jan 26 '20 17:01 yamenarahman

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 protected]',
            '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');

sukiyanen85 avatar Jan 26 '20 17:01 sukiyanen85

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 ?

kenmaclord avatar Jan 31 '20 22:01 kenmaclord