2022-05-24 02:35:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature\Scheduler;
|
|
|
|
|
|
|
|
use App\Export\CSV\ClientExport;
|
|
|
|
use App\Models\ScheduledJob;
|
|
|
|
use App\Models\Scheduler;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutEvents;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\MockUnitData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class SchedulerTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use MockUnitData;
|
|
|
|
use WithoutEvents;
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerCantBeCreatedWithWrongData()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'repeat_every' => Scheduler::DAILY,
|
|
|
|
'job' => ScheduledJob::CREATE_CLIENT_REPORT,
|
|
|
|
'date_key' => '123',
|
|
|
|
'report_keys' => ['test'],
|
|
|
|
// 'date_range' => 'all',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/task_scheduler/', $data);
|
|
|
|
|
|
|
|
$response->assertSessionHasErrors();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerCanBeUpdated()
|
|
|
|
{
|
|
|
|
$this->createScheduler();
|
|
|
|
|
|
|
|
|
|
|
|
$scheduler = Scheduler::first();
|
|
|
|
$updateData = [
|
|
|
|
'start_from' => 1655934741
|
|
|
|
];
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-05-25 23:26:12 +02:00
|
|
|
])->put('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id), $updateData);
|
2022-05-24 02:35:10 +02:00
|
|
|
|
|
|
|
$responseData = $response->json();
|
2022-05-26 04:30:07 +02:00
|
|
|
$this->assertEquals($updateData['start_from'], $responseData['data']['start_from']);
|
2022-05-24 02:35:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerCanBeSeen()
|
|
|
|
{
|
|
|
|
$this->createScheduler();
|
|
|
|
|
|
|
|
|
|
|
|
$scheduler = Scheduler::first();
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-05-25 23:26:12 +02:00
|
|
|
])->get('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id));
|
2022-05-24 02:35:10 +02:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2022-05-25 00:11:36 +02:00
|
|
|
$this->assertEquals('create_client_report', $arr['data']['job']['action_name']);
|
2022-05-24 02:35:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerCanBeDeleted()
|
|
|
|
{
|
|
|
|
$this->createScheduler();
|
|
|
|
|
|
|
|
$scheduler = Scheduler::first();
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-05-25 23:26:12 +02:00
|
|
|
])->delete('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id));
|
2022-05-24 02:35:10 +02:00
|
|
|
|
|
|
|
$this->assertEquals(0, Scheduler::count());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerJobCanBeUpdated()
|
|
|
|
{
|
|
|
|
$this->createScheduler();
|
|
|
|
|
|
|
|
$scheduler = Scheduler::first();
|
|
|
|
$this->assertSame('create_client_report', $scheduler->job->action_name);
|
|
|
|
|
|
|
|
$updateData = [
|
|
|
|
'job' => ScheduledJob::CREATE_CREDIT_REPORT,
|
|
|
|
'date_range' => 'all',
|
|
|
|
'report_keys' => ['test1']
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-05-25 23:26:12 +02:00
|
|
|
])->put('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id) . '/update_job', $updateData);
|
2022-05-24 02:35:10 +02:00
|
|
|
|
|
|
|
$updatedSchedulerJob = Scheduler::first()->job->action_name;
|
2022-05-26 04:30:07 +02:00
|
|
|
$arr = $response->json();
|
|
|
|
$this->assertSame('create_credit_report', $arr['data']['job']['action_name']);
|
2022-05-24 02:35:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchedulerCanBeCreated()
|
|
|
|
{
|
|
|
|
$response = $this->createScheduler();
|
|
|
|
|
|
|
|
$all_schedulers = Scheduler::count();
|
|
|
|
|
|
|
|
$this->assertSame(1, $all_schedulers);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createScheduler()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'repeat_every' => Scheduler::DAILY,
|
|
|
|
'job' => ScheduledJob::CREATE_CLIENT_REPORT,
|
|
|
|
'date_key' => '123',
|
|
|
|
'report_keys' => ['test'],
|
|
|
|
'date_range' => 'all',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/task_scheduler/', $data);
|
|
|
|
}
|
|
|
|
}
|