1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/tests/Feature/Scheduler/SchedulerTest.php

158 lines
4.2 KiB
PHP
Raw Normal View History

2022-05-24 02:35:10 +02:00
<?php
namespace Tests\Feature\Scheduler;
use App\Export\CSV\ClientExport;
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;
2022-05-27 09:01:15 +02:00
use Illuminate\Validation\ValidationException;
2022-05-24 02:35:10 +02:00
class SchedulerTest extends TestCase
{
use MakesHash;
use MockUnitData;
use WithoutEvents;
2022-05-29 08:43:07 +02:00
// use RefreshDatabase;
2022-05-24 02:35:10 +02:00
public function setUp(): void
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
2022-05-27 09:01:15 +02:00
2022-05-30 20:45:13 +02:00
// $this->withoutExceptionHandling();
2022-05-27 09:01:15 +02:00
2022-05-24 02:35:10 +02:00
}
public function testSchedulerCantBeCreatedWithWrongData()
{
$data = [
'repeat_every' => Scheduler::DAILY,
2022-05-30 20:45:13 +02:00
'job' => Scheduler::CREATE_CLIENT_REPORT,
2022-05-24 02:35:10 +02:00
'date_key' => '123',
'report_keys' => ['test'],
2022-05-27 09:01:15 +02:00
'date_range' => 'all',
// 'start_from' => '2022-01-01'
2022-05-24 02:35:10 +02:00
];
2022-05-27 09:01:15 +02:00
$response = false;
2022-05-24 02:35:10 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/task_scheduler/', $data);
2022-05-27 09:01:15 +02:00
2022-05-24 02:35:10 +02:00
$response->assertSessionHasErrors();
}
public function testSchedulerCanBeUpdated()
{
2022-05-27 09:01:15 +02:00
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
2022-05-24 02:35:10 +02:00
2022-05-27 09:01:15 +02:00
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
2022-05-24 02:35:10 +02:00
$updateData = [
'start_from' => 1655934741
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->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()
{
2022-05-27 09:01:15 +02:00
$response = $this->createScheduler();
2022-05-24 02:35:10 +02:00
2022-05-27 09:01:15 +02:00
$arr = $response->json();
$id = $arr['data']['id'];
2022-05-24 02:35:10 +02:00
2022-05-27 09:01:15 +02:00
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
2022-05-24 02:35:10 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id));
2022-05-24 02:35:10 +02:00
$arr = $response->json();
2022-05-30 20:45:13 +02:00
$this->assertEquals('create_client_report', $arr['data']['action_name']);
2022-05-24 02:35:10 +02:00
}
2022-05-30 20:45:13 +02:00
2022-05-24 02:35:10 +02:00
public function testSchedulerJobCanBeUpdated()
{
2022-05-27 09:01:15 +02:00
$response = $this->createScheduler();
$arr = $response->json();
$id = $arr['data']['id'];
$scheduler = Scheduler::find($this->decodePrimaryKey($id));
2022-05-24 02:35:10 +02:00
2022-05-30 20:45:13 +02:00
$this->assertSame('create_client_report', $scheduler->action_name);
2022-05-24 02:35:10 +02:00
$updateData = [
2022-05-30 20:45:13 +02:00
'job' => Scheduler::CREATE_CREDIT_REPORT,
2022-05-24 02:35:10 +02:00
'date_range' => 'all',
'report_keys' => ['test1']
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2022-05-30 20:45:13 +02:00
])->put('/api/v1/task_scheduler/' . $this->encodePrimaryKey($scheduler->id), $updateData);
2022-05-24 02:35:10 +02:00
2022-05-30 20:45:13 +02:00
$updatedSchedulerJob = Scheduler::first()->action_name;
2022-05-26 04:30:07 +02:00
$arr = $response->json();
2022-05-27 09:01:15 +02:00
2022-05-30 20:45:13 +02:00
$this->assertSame('create_credit_report', $arr['data']['action_name']);
2022-05-24 02:35:10 +02:00
}
public function createScheduler()
{
$data = [
'repeat_every' => Scheduler::DAILY,
2022-05-30 20:45:13 +02:00
'job' => Scheduler::CREATE_CLIENT_REPORT,
2022-05-24 02:35:10 +02:00
'date_key' => '123',
'report_keys' => ['test'],
'date_range' => 'all',
2022-05-27 09:01:15 +02:00
'start_from' => '2022-01-01'
2022-05-24 02:35:10 +02:00
];
return $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/task_scheduler/', $data);
}
}