opnform/tests/Feature/Forms/FormStatTest.php

69 lines
2.5 KiB
PHP
Raw Normal View History

2022-09-20 19:59:52 +00:00
<?php
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
use Illuminate\Support\Facades\Artisan;
it('check formstat chart data', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, []);
$views = [];
$submissions = [];
// Create 10 views & submissions for past days
2024-02-23 10:54:12 +00:00
for ($i = 1; $i <= 10; $i++) {
2022-09-20 19:59:52 +00:00
$date = now()->subDays($i);
$dateString = $date->format('d-m-Y');
$submission = $form->submissions()->create();
$submission->created_at = $date;
$submission->save();
$view = $form->views()->create();
$view->created_at = $date;
$view->save();
$views[$dateString] = isset($views[$dateString]) ? ($views[$dateString] + 1) : 1;
$submissions[$dateString] = isset($submissions[$dateString]) ? ($submissions[$dateString] + 1) : 1;
}
// Run Command
Artisan::call('forms:database-cleanup');
// Create 5 views & submissions
2024-02-23 10:54:12 +00:00
for ($i = 1; $i <= 5; $i++) {
2022-09-20 19:59:52 +00:00
$form->views()->create();
$form->submissions()->create();
$dateString = now()->format('d-m-Y');
$views[$dateString] = isset($views[$dateString]) ? ($views[$dateString] + 1) : 1;
$submissions[$dateString] = isset($submissions[$dateString]) ? ($submissions[$dateString] + 1) : 1;
}
// Now check chart data
$this->getJson(route('open.workspaces.form.stats', [$workspace->id, $form->id]))
->assertSuccessful()
->assertJson(function (\Illuminate\Testing\Fluent\AssertableJson $json) use ($views, $submissions) {
return $json->whereType('views', 'array')
->whereType('submissions', 'array')
2024-02-23 10:54:12 +00:00
->where('views', function ($values) use ($views) {
foreach ($values as $date => $count) {
if ((isset($views[$date]) && $views[$date] != $count) || (! isset($views[$date]) && $count != 0)) {
2022-09-20 19:59:52 +00:00
return false;
}
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return true;
})
2024-02-23 10:54:12 +00:00
->where('submissions', function ($values) use ($submissions) {
foreach ($values as $date => $count) {
if ((isset($submissions[$date]) && $submissions[$date] != $count) || (! isset($submissions[$date]) && $count != 0)) {
2022-09-20 19:59:52 +00:00
return false;
}
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return true;
})
->etc();
});
});