Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel unit tests - changing a config value depending on test method

I have an application with a system to verify accounts (register -> get email with link to activate -> account verified). That verification flow is optional and can be switched off with a configuration value:

// config/auth.php
return [
  // ...
  'enable_verification' => true
];

I want to test the registration controller:

  • it should redirect to home page in both cases
  • when verification is ON, home page should show message 'email sent'
  • when verification is OFF, home page should show message 'account created'
  • etc.

My test methods:

public function test_UserProperlyCreated_WithVerificationDisabled()
{
    $this->app['config']->set('auth.verification.enabled', false);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('[email protected]', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.complete'));
}

public function test_UserProperlyCreated_WithVerificationEnabled()
{
    $this->app['config']->set('auth.verification.enabled', true);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('[email protected]', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.needs_verification'));
}

When debugging, I noticed that the configuration value when inside the controller method is always set to the value in the config file, no matter what I set with my $this->app['config']->set...

I have other tests on the user repository itself to check that it works both when validation is ON or OFF. And there the tests behave as expected.

Any idea why it fails for controllers and how to fix that?

like image 986
Vincent Mimoun-Prat Avatar asked Jun 16 '16 14:06

Vincent Mimoun-Prat


2 Answers

If I understand your question right - you looking for "how to set config param", then you can use Config::set($key, $value)

like image 147
Alex Slipknot Avatar answered Oct 20 '22 07:10

Alex Slipknot


Correct answer is above

Config::set($key, $value) 

Example

//Not forget to add namespace using class.
use Config;

//Example to set config. (configFile.key)
Config::set('auth.enable_verification', false); 
like image 8
Eduard Brokan Avatar answered Oct 20 '22 07:10

Eduard Brokan