122 lines
4.0 KiB
PHP
122 lines
4.0 KiB
PHP
<?php
|
|
|
|
use App\Models\Rainfall;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Laravel\Sanctum\Sanctum;
|
|
|
|
beforeEach(function () {
|
|
$this->user = User::factory()->create();
|
|
Sanctum::actingAs($this->user);
|
|
});
|
|
|
|
test('an user can fetch his rainfalls', function () {
|
|
Rainfall::factory()->count(100)->create([
|
|
'date' => Carbon::today()->subDays(rand(0, 365)),
|
|
]);
|
|
|
|
expect($this->user->rainfalls)
|
|
->toHaveCount(100);
|
|
|
|
$response = $this->get('/api/rainfalls/graph?start=2023-01-01&end='.now()->format('Y-m-d').'&period=day');
|
|
|
|
$response->assertOk()
|
|
->assertJson([
|
|
[
|
|
'start' => '2023-01-01 00:00:00',
|
|
'days' => 1,
|
|
'label' => '01/01/2023',
|
|
'value' => $this->user->rainfalls->where('date', '2023-01-01')->sum('value'),
|
|
],
|
|
[
|
|
'start' => '2023-01-02 00:00:00',
|
|
'days' => 1,
|
|
'label' => '02/01/2023',
|
|
'value' => $this->user->rainfalls->where('date', '2023-01-02')->sum('value'),
|
|
],
|
|
]);
|
|
});
|
|
|
|
test('an user can fetch his rainfall by week', function () {
|
|
Rainfall::factory()->count(100)->create([
|
|
'date' => Carbon::today()->subDays(rand(0, 365)),
|
|
]);
|
|
|
|
$response = $this->get('/api/rainfalls/graph?start=2023-02-01&end='.now()->format('Y-m-d').'&period=week');
|
|
|
|
// $date1 = now();
|
|
// $date2 = now()->addDays(40);
|
|
//
|
|
// $difference = $date1->diff($date2);
|
|
// dd($difference->days);
|
|
$response->assertOk()
|
|
->assertJson([
|
|
[
|
|
'start' => '2023-02-01 00:00:00',
|
|
'days' => 5,
|
|
'label' => 'semaine 6',
|
|
'value' => $this->user->rainfalls->where('date', '2023-02-01')->sum('value'),
|
|
],
|
|
[
|
|
'start' => '2023-02-06 00:00:00',
|
|
'days' => 7,
|
|
'label' => 'semaine 7',
|
|
'value' => $this->user->rainfalls->where('date', '2023-02-01')->sum('value'),
|
|
],
|
|
]);
|
|
})->todo();
|
|
|
|
test('an user can fetch his rainfall by month', function () {
|
|
Rainfall::factory()->count(100)->create([
|
|
'date' => Carbon::today()->subDays(rand(0, 365)),
|
|
]);
|
|
|
|
$response = $this->get('/api/rainfalls/graph?start=2023-01-15&end='.now()->format('Y-m-d').'&period=month');
|
|
|
|
$response->assertOk()
|
|
->assertJson([
|
|
[
|
|
'start' => '2023-01-15 00:00:00',
|
|
'days' => 17,
|
|
'label' => 'Janvier 2023',
|
|
'value' => $this->user->rainfalls->whereBetween('date', ['2023-01-15', '2023-01-31'])->sum('value'),
|
|
],
|
|
[
|
|
'start' => '2023-02-01 00:00:00',
|
|
'days' => 28,
|
|
'label' => 'Février 2023',
|
|
'value' => $this->user->rainfalls->whereBetween('date', ['2023-02-01', '2023-02-28'])->sum('value'),
|
|
],
|
|
[
|
|
'start' => '2023-03-01 00:00:00',
|
|
'days' => 31,
|
|
'label' => 'Mars 2023',
|
|
'value' => $this->user->rainfalls->whereBetween('date', ['2023-03-01', '2023-03-31'])->sum('value'),
|
|
],
|
|
]);
|
|
});
|
|
|
|
test('an user can fetch his rainfall by year', function () {
|
|
Rainfall::factory()->count(100)->create([
|
|
'date' => Carbon::today()->subDays(rand(0, 365)),
|
|
]);
|
|
|
|
$response = $this->get('/api/rainfalls/graph?start=2022-11-15&end='.now()->format('Y-m-d').'&period=year');
|
|
|
|
$response->assertOk()
|
|
->assertJson([
|
|
[
|
|
'start' => '2022-11-15 00:00:00',
|
|
'days' => 47,
|
|
'label' => 2022,
|
|
'value' => $this->user->rainfalls->whereBetween('date', ['2022-11-15', '2022-12-31'])->sum('value'),
|
|
],
|
|
[
|
|
'start' => '2023-01-01 00:00:00',
|
|
'days' => 359,
|
|
'label' => 2023,
|
|
'value' => $this->user->rainfalls->whereBetween('date', ['2023-01-01', '2023-12-31'])->sum('value'),
|
|
],
|
|
]);
|
|
});
|