E
E
EVOSandru62019-11-20 22:20:32
Laravel
EVOSandru6, 2019-11-20 22:20:32

How in laravel when testing registration to block sending notifications for mail and sms?

Good afternoon!
Testing registration.

public function testSuccess(): void
    {
        $this->withoutMiddleware();

        $user = factory(User::class, 'customer')->make(['phone' => '+7 (999) 000-0002']);

        $response = $this->post(route('register'), [
            'name' => $user->name,
            'email' => $user->email,
            'password' => 'secret1234',
            'password_confirmation' => 'secret1234',
            'department_id' => $voronezh = 40,
            'phone' => $user->phone
        ]);

           $newUser = User::where('email', $user->email);
           self::assertEquals($newUser->email, $user->email);

        $response
            ->assertStatus(302)
            ->assertRedirect(route('verify.phone'))
            ->assertSessionHas('success', __('messages', 'you_need_to_confirm_your_account_please_check_your_email_or_phone'));
    }

The service sends SMS and email notifications.
For SMS, I have a provider defined:
class SmsServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(SmsSender::class, function (Application $app) {
            $config = $app->make('config')->get('sms');

            switch ($config['driver']) {
                case 'smsc.ru':
                    $params = $config['drivers']['smsc.ru'];
                    return new SmscRu($params['login'], $params['password'], $params['url']);
                    break;
                case 'array':
                    return new SmsMock();
                    break;
                default:
                    throw new \InvalidArgumentException('Undefined SMS driver ' . $config['driver']);
            }
        });
    }
}

How can I override the implementation of the SmsSender interface from a test ?
And how can I lock the mail?
Here I have everything out of the box.
I did not create any providers.
Only in .env defined connection parameters.
Sending is carried out in services in the following way:
$this->mailer->to($user->email)->queue(new VerifyMail($user));

For some reason, the following addition to the file didn't work:
/phpunit.xml
<env name="MAIL_DRIVER" value="array" force="true"/>
<env name="SMS_DRIVER" value="array" force="true"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kylin, 2019-11-21
@EVOSandru6

For example, put a condition

if(config('app.env') === 'production'){
   //
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question