mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 12:24:35 -04:00
Tests for scheduler
This commit is contained in:
parent
9f3e4b881f
commit
2bcccb2215
@ -26,7 +26,9 @@ class SchedulerFactory
|
|||||||
$scheduler->is_paused = false;
|
$scheduler->is_paused = false;
|
||||||
$scheduler->is_deleted = false;
|
$scheduler->is_deleted = false;
|
||||||
$scheduler->template = '';
|
$scheduler->template = '';
|
||||||
|
$scheduler->next_run = now()->format('Y-m-d');
|
||||||
|
$scheduler->next_run_client = now()->format('Y-m-d');
|
||||||
|
|
||||||
return $scheduler;
|
return $scheduler;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,6 @@ class SchedulerRepository extends BaseRepository
|
|||||||
public function save(array $data, Scheduler $scheduler): Scheduler
|
public function save(array $data, Scheduler $scheduler): Scheduler
|
||||||
{
|
{
|
||||||
|
|
||||||
nlog($data);
|
|
||||||
|
|
||||||
$scheduler->fill($data);
|
$scheduler->fill($data);
|
||||||
|
|
||||||
$scheduler->save();
|
$scheduler->save();
|
||||||
|
@ -53,9 +53,10 @@ class SchedulerService
|
|||||||
->each(function ($_client){
|
->each(function ($_client){
|
||||||
|
|
||||||
$this->client = $_client;
|
$this->client = $_client;
|
||||||
$statement_properties = $this->calculateStatementProperties();
|
|
||||||
|
|
||||||
//work out the date range
|
//work out the date range
|
||||||
|
$statement_properties = $this->calculateStatementProperties();
|
||||||
|
|
||||||
$pdf = $_client->service()->statement($statement_properties,true);
|
$pdf = $_client->service()->statement($statement_properties,true);
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -65,7 +66,12 @@ class SchedulerService
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateStatementProperties()
|
/**
|
||||||
|
* Hydrates the array needed to generate the statement
|
||||||
|
*
|
||||||
|
* @return array The statement options array
|
||||||
|
*/
|
||||||
|
private function calculateStatementProperties(): array
|
||||||
{
|
{
|
||||||
$start_end = $this->calculateStartAndEndDates();
|
$start_end = $this->calculateStartAndEndDates();
|
||||||
|
|
||||||
@ -79,7 +85,12 @@ class SchedulerService
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculateStartAndEndDates()
|
/**
|
||||||
|
* Start and end date of the statement
|
||||||
|
*
|
||||||
|
* @return array [$start_date, $end_date];
|
||||||
|
*/
|
||||||
|
private function calculateStartAndEndDates(): array
|
||||||
{
|
{
|
||||||
return match ($this->scheduler->parameters['date_range']) {
|
return match ($this->scheduler->parameters['date_range']) {
|
||||||
'this_month' => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')],
|
'this_month' => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')],
|
||||||
@ -94,7 +105,11 @@ class SchedulerService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function calculateNextRun() :?Carbon
|
/**
|
||||||
|
* Sets the next run date of the scheduled task
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private function calculateNextRun()
|
||||||
{
|
{
|
||||||
if (! $this->scheduler->next_run) {
|
if (! $this->scheduler->next_run) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -31,6 +31,7 @@ class SchedulerFactory extends Factory
|
|||||||
'parameters' => [],
|
'parameters' => [],
|
||||||
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
||||||
'next_run' => now()->addSeconds(rand(86400,8640000)),
|
'next_run' => now()->addSeconds(rand(86400,8640000)),
|
||||||
|
'next_run_client' => now()->addSeconds(rand(86400,8640000)),
|
||||||
'template' => 'statement_task',
|
'template' => 'statement_task',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,10 @@
|
|||||||
namespace Tests\Feature\Scheduler;
|
namespace Tests\Feature\Scheduler;
|
||||||
|
|
||||||
use App\Export\CSV\ClientExport;
|
use App\Export\CSV\ClientExport;
|
||||||
|
use App\Factory\SchedulerFactory;
|
||||||
use App\Models\RecurringInvoice;
|
use App\Models\RecurringInvoice;
|
||||||
use App\Models\Scheduler;
|
use App\Models\Scheduler;
|
||||||
|
use App\Services\Scheduler\SchedulerService;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@ -51,6 +53,88 @@ class SchedulerTest extends TestCase
|
|||||||
$this->withoutExceptionHandling();
|
$this->withoutExceptionHandling();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCalculateStartAndEndDates()
|
||||||
|
{
|
||||||
|
|
||||||
|
$scheduler = SchedulerFactory::create($this->company->id, $this->user->id);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'A test statement scheduler',
|
||||||
|
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
||||||
|
'next_run' => "2023-01-01",
|
||||||
|
'template' => 'client_statement',
|
||||||
|
'parameters' => [
|
||||||
|
'date_range' => 'previous_month',
|
||||||
|
'show_payments_table' => true,
|
||||||
|
'show_aging_table' => true,
|
||||||
|
'status' => 'paid',
|
||||||
|
'clients' => [],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$scheduler->fill($data);
|
||||||
|
$scheduler->save();
|
||||||
|
|
||||||
|
$service_object = new SchedulerService($scheduler);
|
||||||
|
|
||||||
|
// $reflection = new \ReflectionClass(get_class($service_object));
|
||||||
|
// $method = $reflection->getMethod('calculateStatementProperties');
|
||||||
|
// $method->setAccessible(true);
|
||||||
|
// $method->invokeArgs($service_object, []);
|
||||||
|
|
||||||
|
$reflectionMethod = new \ReflectionMethod(SchedulerService::class, 'calculateStartAndEndDates');
|
||||||
|
$reflectionMethod->setAccessible(true);
|
||||||
|
$method = $reflectionMethod->invoke(new SchedulerService($scheduler)); // 'baz'
|
||||||
|
|
||||||
|
$this->assertIsArray($method);
|
||||||
|
|
||||||
|
$this->assertEquals('previous_month', $scheduler->parameters['date_range']);
|
||||||
|
|
||||||
|
$this->assertEqualsCanonicalizing(['2022-12-01','2022-12-31'], $method);
|
||||||
|
|
||||||
|
|
||||||
|
// $this->assertEquals('paid', $method['status']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCalculateStatementProperties()
|
||||||
|
{
|
||||||
|
|
||||||
|
$scheduler = SchedulerFactory::create($this->company->id, $this->user->id);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'A test statement scheduler',
|
||||||
|
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
||||||
|
'next_run' => now()->format('Y-m-d'),
|
||||||
|
'template' => 'client_statement',
|
||||||
|
'parameters' => [
|
||||||
|
'date_range' => 'previous_month',
|
||||||
|
'show_payments_table' => true,
|
||||||
|
'show_aging_table' => true,
|
||||||
|
'status' => 'paid',
|
||||||
|
'clients' => [],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$scheduler->fill($data);
|
||||||
|
$scheduler->save();
|
||||||
|
|
||||||
|
$service_object = new SchedulerService($scheduler);
|
||||||
|
|
||||||
|
// $reflection = new \ReflectionClass(get_class($service_object));
|
||||||
|
// $method = $reflection->getMethod('calculateStatementProperties');
|
||||||
|
// $method->setAccessible(true);
|
||||||
|
// $method->invokeArgs($service_object, []);
|
||||||
|
|
||||||
|
$reflectionMethod = new \ReflectionMethod(SchedulerService::class, 'calculateStatementProperties');
|
||||||
|
$reflectionMethod->setAccessible(true);
|
||||||
|
$method = $reflectionMethod->invoke(new SchedulerService($scheduler)); // 'baz'
|
||||||
|
|
||||||
|
$this->assertIsArray($method);
|
||||||
|
|
||||||
|
$this->assertEquals('paid', $method['status']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetThisMonthRange()
|
public function testGetThisMonthRange()
|
||||||
{
|
{
|
||||||
@ -82,24 +166,15 @@ class SchedulerTest extends TestCase
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)],
|
|
||||||
'is_paused' => 'bail|sometimes|boolean',
|
|
||||||
'frequency_id' => 'bail|required|integer|digits_between:1,12',
|
|
||||||
'next_run' => 'bail|required|date:Y-m-d',
|
|
||||||
'template' => 'bail|required|string',
|
|
||||||
'parameters' => 'bail|array',
|
|
||||||
*/
|
|
||||||
|
|
||||||
public function testClientStatementGeneration()
|
public function testClientStatementGeneration()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
'name' => 'A test statement scheduler',
|
'name' => 'A test statement scheduler',
|
||||||
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
|
||||||
'next_run' => '2023-01-14',
|
'next_run' => now()->format('Y-m-d'),
|
||||||
'template' => 'client_statement',
|
'template' => 'client_statement',
|
||||||
'parameters' => [
|
'parameters' => [
|
||||||
'date_range' => 'last_month',
|
'date_range' => 'previous_month',
|
||||||
'show_payments_table' => true,
|
'show_payments_table' => true,
|
||||||
'show_aging_table' => true,
|
'show_aging_table' => true,
|
||||||
'status' => 'paid',
|
'status' => 'paid',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user