ldc-crm/tests/Feature/ApiV2/StatsTest.php
2021-11-05 14:46:53 +01:00

89 lines
2.6 KiB
PHP

<?php
namespace Tests\Feature\ApiV2;
use App\Enums\ApiStatus;
use App\Models\Question;
use App\Models\Service;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
use Tests\CreatesApplication;
class StatsTest extends ApiBase
{
use CreatesApplication;
use DatabaseTransactions;
/**
* route: apiV2.stats.brandStats
*/
public function testStatsByBrand()
{
$service = Service::firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [
'start' => Carbon::now()->subMonths(3)->format('Y-m-d'),
'end' => Carbon::now()->format('Y-m-d')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$json->assertStructure([
'data' => [
'stats' => [
'*' => [
'date',
'active',
'inactive',
'old_accounts',
'new_accounts',
'total_accounts',
'total_opt_in'
]
]
]
]);
}
/**
* route: apiV2.stats.brandStats
*/
public function testStatsByQuestion()
{
$service = Service::firstOrFail();
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
$answersIds = $question->answers()->pluck('id');
$start = Carbon::now()->subDays(3);
$end = Carbon::now();
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats'), [
'question' => $question->id,
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d')
]);
$datesIndexes = [];
do {
$datesIndexes[$start->format('Y-m-d')] = $answersIds
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
->keys()->all();
$start = $start->addDay();
} while ($start->lessThanOrEqualTo($end));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$json->assertStructure([
'data' => [
'answers' => [
'*' => [
'id',
'answer'
]
],
'stats' => $datesIndexes
]
]);
}
}