2021-10-09 18:34:37 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Commands;
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Jobs\Banking\CreateTransaction;
|
2021-10-09 18:34:37 +03:00
|
|
|
use App\Jobs\Document\CreateDocument;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Models\Banking\Transaction;
|
2021-10-09 18:34:37 +03:00
|
|
|
use App\Models\Document\Document;
|
|
|
|
use App\Notifications\Sale\Invoice as InvoiceNotification;
|
|
|
|
use App\Utilities\Date;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
|
|
|
|
class RecurringCheckTest extends FeatureTestCase
|
|
|
|
{
|
|
|
|
public $recurring_count;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->recurring_count = 7;
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function testItShouldCreateCorrectNumberOfRecurringInvoicesByCount(): void
|
2021-10-09 18:34:37 +03:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->dispatch(new CreateDocument($this->getInvoiceRequest('count')));
|
2021-10-09 18:34:37 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
Date::setTestNow(Date::today());
|
2021-10-09 18:34:37 +03:00
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('documents', $this->recurring_count + 1);
|
|
|
|
|
|
|
|
Notification::assertSentToTimes($this->user, InvoiceNotification::class, $this->recurring_count);
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function testItShouldCreateCorrectNumberOfRecurringInvoicesByDate(): void
|
2021-10-09 18:34:37 +03:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->dispatch(new CreateDocument($this->getInvoiceRequest('date')));
|
2021-10-09 18:34:37 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
Date::setTestNow(Date::today());
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('documents', $this->recurring_count + 1);
|
|
|
|
|
|
|
|
Notification::assertSentToTimes($this->user, InvoiceNotification::class, $this->recurring_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldNotCreateAnyRecurringInvoiceByCount(): void
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
|
|
|
$this->dispatch(new CreateDocument($this->getInvoiceRequest('count')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today()->subDays($this->recurring_count));
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('documents', 1);
|
|
|
|
|
|
|
|
Notification::assertNotSentTo($this->user, InvoiceNotification::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldNotCreateAnyRecurringInvoiceByDate(): void
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
|
|
|
$this->dispatch(new CreateDocument($this->getInvoiceRequest('date')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today()->subDays($this->recurring_count));
|
2021-10-09 18:34:37 +03:00
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('documents', 1);
|
|
|
|
|
|
|
|
Notification::assertNotSentTo($this->user, InvoiceNotification::class);
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public function testItShouldCreateCorrectNumberOfRecurringExpensesByCount(): void
|
2021-10-09 18:34:37 +03:00
|
|
|
{
|
2022-06-01 10:15:55 +03:00
|
|
|
$this->dispatch(new CreateTransaction($this->getExpenseRequest('count')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today());
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('transactions', $this->recurring_count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldCreateCorrectNumberOfRecurringExpensesByDate(): void
|
|
|
|
{
|
|
|
|
$this->dispatch(new CreateTransaction($this->getExpenseRequest('date')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today());
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('transactions', $this->recurring_count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldNotCreateAnyRecurringExpenseByCount(): void
|
|
|
|
{
|
|
|
|
$this->dispatch(new CreateTransaction($this->getExpenseRequest('count')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today()->subDays($this->recurring_count));
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('transactions', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldNotCreateAnyRecurringExpenseByDate(): void
|
|
|
|
{
|
|
|
|
$this->dispatch(new CreateTransaction($this->getExpenseRequest('date')));
|
|
|
|
|
|
|
|
Date::setTestNow(Date::today()->subDays($this->recurring_count));
|
|
|
|
|
|
|
|
$this->artisan('recurring:check');
|
|
|
|
|
|
|
|
$this->assertDatabaseCount('transactions', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvoiceRequest(string $limit_by): array
|
|
|
|
{
|
|
|
|
$request = Document::factory()->invoice()->items()->sent()->recurring()->raw();
|
|
|
|
|
|
|
|
return array_merge($request, $this->getRecurringData($limit_by));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExpenseRequest(string $limit_by): array
|
|
|
|
{
|
|
|
|
$request = Transaction::factory()->expense()->recurring()->raw();
|
|
|
|
|
|
|
|
return array_merge($request, $this->getRecurringData($limit_by));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecurringData(string $limit_by): array
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'recurring_started_at' => Date::today()->subDays($this->recurring_count - 1),
|
|
|
|
'recurring_limit' => $limit_by,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($limit_by == 'count') {
|
|
|
|
$data['recurring_limit_count'] = 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit_by == 'date') {
|
|
|
|
$data['recurring_limit_date'] = Date::today()->addDays($this->recurring_count + 5)->toDateTimeString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2021-10-09 18:34:37 +03:00
|
|
|
}
|
|
|
|
}
|