/** @test */ public function it_should_send_a_notification_email_to_user() { //set today $today = Carbon::now()->toDateString(); $tomorrow = Carbon::now()->addDay()->toDateString(); //user $user = factory(App\User::class)->create(['verification_token' => '123', 'verified' => 1]); //streets $streets = factory(App\Street::class, 20)->create(); //attach 2 streets to user $street1 = $streets->first(); $street2 = $streets->last(); $user->addStreet($street1->id); $user->addStreet($street2->id); //set streets maintenance info $street1->next_maintenance = $tomorrow; $street2->next_maintenance = $tomorrow; $street1->info = 'Extra information'; $street2->info = 'Extra information'; $userStreets = [$street1, $street2]; //send notificationmail $userMailer = new UserMailer(); $userMailer->sendNotificationEmail($user, $userStreets); $this->seeInDatabase('users', ['email' => $user->email, 'mail_count' => 1]); }
/** * Send notification emails to all verified users whose streets are maintained tomorrow * This is used in daily command * @return Array */ public static function sendUserNotificationsIfNeeded() { $users = static::where('verified', 1)->get(); $userMailer = new UserMailer(); $sentUsers = []; foreach ($users as $user) { $streets = $user->streets()->maintainedTomorrow()->get()->all(); if ($userMailer->sendNotificationEmail($user, $streets)) { $sentUsers[] = $user; } } return $sentUsers; }