Tests for scheduler

This commit is contained in:
David Bomba 2023-01-17 20:48:10 +11:00
parent 9f3e4b881f
commit 2bcccb2215
5 changed files with 109 additions and 18 deletions

View File

@ -26,7 +26,9 @@ class SchedulerFactory
$scheduler->is_paused = false;
$scheduler->is_deleted = false;
$scheduler->template = '';
$scheduler->next_run = now()->format('Y-m-d');
$scheduler->next_run_client = now()->format('Y-m-d');
return $scheduler;
}
}

View File

@ -27,8 +27,6 @@ class SchedulerRepository extends BaseRepository
public function save(array $data, Scheduler $scheduler): Scheduler
{
nlog($data);
$scheduler->fill($data);
$scheduler->save();

View File

@ -53,9 +53,10 @@ class SchedulerService
->each(function ($_client){
$this->client = $_client;
$statement_properties = $this->calculateStatementProperties();
//work out the date range
$statement_properties = $this->calculateStatementProperties();
$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();
@ -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']) {
'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) {
return null;

View File

@ -31,6 +31,7 @@ class SchedulerFactory extends Factory
'parameters' => [],
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
'next_run' => now()->addSeconds(rand(86400,8640000)),
'next_run_client' => now()->addSeconds(rand(86400,8640000)),
'template' => 'statement_task',
];
}

View File

@ -12,8 +12,10 @@
namespace Tests\Feature\Scheduler;
use App\Export\CSV\ClientExport;
use App\Factory\SchedulerFactory;
use App\Models\RecurringInvoice;
use App\Models\Scheduler;
use App\Services\Scheduler\SchedulerService;
use App\Utils\Traits\MakesHash;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
@ -51,6 +53,88 @@ class SchedulerTest extends TestCase
$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()
{
@ -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()
{
$data = [
'name' => 'A test statement scheduler',
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
'next_run' => '2023-01-14',
'next_run' => now()->format('Y-m-d'),
'template' => 'client_statement',
'parameters' => [
'date_range' => 'last_month',
'date_range' => 'previous_month',
'show_payments_table' => true,
'show_aging_table' => true,
'status' => 'paid',