2019-12-14 06:49:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Feature;
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
use App\Mail\TemplateEmail;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
use App\Utils\Traits\InvoiceEmailBuilder;
|
2019-12-14 06:49:48 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-12-16 12:34:38 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2019-12-14 06:49:48 +01:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class InvoiceEmailTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
2019-12-16 12:34:38 +01:00
|
|
|
use GeneratesCounter;
|
2019-12-14 06:49:48 +01:00
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_initial_email_sends()
|
|
|
|
{
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
// \Log::error($this->invoice->makeValues());
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$this->invoice->date = now();
|
|
|
|
$this->invoice->due_date = now()->addDays(7);
|
|
|
|
$this->invoice->number = $this->getNextInvoiceNumber($this->client);
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$this->invoice->client = $this->client;
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$message_array = $this->invoice->getEmailData();
|
|
|
|
$message_array['title'] = &$message_array['subject'];
|
|
|
|
$message_array['footer'] = 'The Footer';
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2019-12-16 12:53:16 +01:00
|
|
|
// $template_style = $this->client->getSetting('email_style');
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$template_style = 'light';
|
|
|
|
//iterate through the senders list and send from here
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
$invitations = InvoiceInvitation::whereInvoiceId($this->invoice->id)->get();
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$invitations->each(function($invitation) use($message_array, $template_style) {
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-22 11:28:41 +01:00
|
|
|
$contact = $invitation->contact;
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
if($contact->send_invoice && $contact->email)
|
|
|
|
{
|
|
|
|
//there may be template variables left over for the specific contact? need to reparse here
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
//change the runtime config of the mail provider here:
|
|
|
|
|
|
|
|
//send message
|
|
|
|
Mail::to($contact->email)
|
2019-12-22 11:28:41 +01:00
|
|
|
->send(new TemplateEmail($message_array, $template_style, $this->user, $contact->client));
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
//fire any events
|
|
|
|
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2020-01-29 05:25:08 +01:00
|
|
|
//sleep(5);//here to cope with mailtrap time delays
|
2019-12-16 12:34:38 +01:00
|
|
|
|
|
|
|
}
|
2019-12-14 06:49:48 +01:00
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
});
|
|
|
|
|
2019-12-14 06:49:48 +01:00
|
|
|
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
}
|
2019-12-14 06:49:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|