1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/tests/Feature/ReminderTest.php

144 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature;
use App\Jobs\Util\ReminderJob;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Carbon;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Jobs\Util\ReminderJob
*/
class ReminderTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutExceptionHandling();
}
public function testReminderQueryCatchesDate()
{
$this->invoice->next_send_date = now()->format('Y-m-d');
$this->invoice->save();
$invoices = Invoice::where('next_send_date', Carbon::today())->get();
$this->assertEquals(1, $invoices->count());
}
public function testReminderHits()
{
$this->invoice->date = now()->format('Y-m-d');
$this->invoice->due_date = Carbon::now()->addDays(30)->format('Y-m-d');
$settings = $this->company->settings;
$settings->enable_reminder1 = true;
$settings->schedule_reminder1 = 'after_invoice_date';
$settings->num_days_reminder1 = 7;
$settings->enable_reminder2 = true;
$settings->schedule_reminder2 = 'before_due_date';
$settings->num_days_reminder2 = 1;
$settings->enable_reminder3 = true;
$settings->schedule_reminder3 = 'after_due_date';
$settings->num_days_reminder3 = 1;
$this->company->settings = $settings;
$this->invoice->service()->markSent();
2021-05-26 02:35:39 +02:00
$this->invoice->service()->setReminder($settings)->save();
2021-06-11 00:20:46 +02:00
$this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), Carbon::now()->addDays(7)->format('Y-m-d'));
2021-05-25 23:31:17 +02:00
// ReminderJob::dispatchNow();
}
public function testReminderNextSendRecalculation()
{
2021-05-26 01:43:23 +02:00
$this->invoice->date = now()->subDays(2)->format('Y-m-d');
2021-05-25 23:31:17 +02:00
$this->invoice->due_date = Carbon::now()->addDays(30)->format('Y-m-d');
2021-05-26 01:43:23 +02:00
$this->invoice->reminder1_sent = now()->subDays(1)->format('Y-m-d');
2021-05-25 23:31:17 +02:00
$settings = $this->company->settings;
$settings->enable_reminder1 = true;
$settings->schedule_reminder1 = 'after_invoice_date';
$settings->num_days_reminder1 = 1;
$settings->enable_reminder2 = true;
2021-05-26 01:43:23 +02:00
$settings->schedule_reminder2 = 'after_invoice_date';
$settings->num_days_reminder2 = 2;
2021-05-25 23:31:17 +02:00
$settings->enable_reminder3 = true;
2021-05-26 01:43:23 +02:00
$settings->schedule_reminder3 = 'after_invoice_date';
$settings->num_days_reminder3 = 3;
2021-05-25 23:31:17 +02:00
$this->company->settings = $settings;
$this->invoice->service()->markSent();
2021-05-26 02:35:39 +02:00
$this->invoice->service()->setReminder($settings)->save();
2021-05-26 01:43:23 +02:00
$this->invoice->fresh();
2021-06-11 00:20:46 +02:00
$this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->format('Y-m-d'));
2021-05-25 23:31:17 +02:00
}
2021-05-26 01:43:23 +02:00
public function testReminder3NextSendRecalculation()
{
$this->invoice->date = now()->subDays(3)->format('Y-m-d');
$this->invoice->due_date = Carbon::now()->addDays(30)->format('Y-m-d');
$this->invoice->reminder1_sent = now()->subDays(2)->format('Y-m-d');
$this->invoice->reminder2_sent = now()->subDays(1)->format('Y-m-d');
$settings = $this->company->settings;
$settings->enable_reminder1 = true;
$settings->schedule_reminder1 = 'after_invoice_date';
$settings->num_days_reminder1 = 1;
$settings->enable_reminder2 = true;
$settings->schedule_reminder2 = 'after_invoice_date';
$settings->num_days_reminder2 = 2;
$settings->enable_reminder3 = true;
$settings->schedule_reminder3 = 'after_invoice_date';
$settings->num_days_reminder3 = 3;
$this->company->settings = $settings;
$this->invoice->service()->markSent();
2021-05-26 02:35:39 +02:00
$this->invoice->service()->setReminder($settings)->save();
2021-05-26 01:43:23 +02:00
$this->invoice->fresh();
2021-06-11 00:20:46 +02:00
$this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->format('Y-m-d'));
2021-05-26 01:43:23 +02:00
}
}