2021-12-19 15:57:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\CRM\ApiV2;
|
|
|
|
|
|
|
|
|
|
use App\Enums\ApiError;
|
|
|
|
|
use App\Enums\ApiStatus;
|
2025-08-05 15:30:34 +02:00
|
|
|
use App\Models\Answer;
|
2021-12-19 15:57:24 +01:00
|
|
|
use App\Models\Question;
|
|
|
|
|
use App\Models\Service;
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
2025-08-05 15:30:34 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-12-19 15:57:24 +01:00
|
|
|
use Tests\CreatesApplication;
|
|
|
|
|
|
|
|
|
|
class StatsTest extends ApiBase
|
|
|
|
|
{
|
|
|
|
|
use CreatesApplication;
|
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test get stats by brand.
|
|
|
|
|
* route: apiV2.stats.brandStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByBrand(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->isoFormat('YYYY-MM-DD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
2021-12-19 15:57:24 +01:00
|
|
|
$json->assertStructure([
|
|
|
|
|
'data' => [
|
|
|
|
|
'stats' => [
|
|
|
|
|
'*' => [
|
|
|
|
|
'date',
|
|
|
|
|
'active',
|
|
|
|
|
'inactive',
|
|
|
|
|
'old_accounts',
|
|
|
|
|
'new_accounts',
|
|
|
|
|
'total_accounts',
|
|
|
|
|
'total_opt_in'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test get stats by brand should fail with date bigger than 3 month.
|
|
|
|
|
* route: apiV2.stats.brandStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByBrandShouldFailWithInvalidDate(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
2023-09-06 17:24:53 +02:00
|
|
|
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value());
|
2021-12-19 15:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test get stats by brand should fail with invalid date format.
|
|
|
|
|
* route: apiV2.stats.brandStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByBrandShouldFailWithInvalidDateFormat(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD HH:mmDD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
2023-09-06 17:24:53 +02:00
|
|
|
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value());
|
2021-12-19 15:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test stats by question.
|
|
|
|
|
* route: apiV2.stats.questionStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByQuestion(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
2025-08-05 15:30:34 +02:00
|
|
|
extract($this->getCustomer(true, false));
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
|
|
|
|
|
$answersIds = $question->answers()->pluck('id');
|
|
|
|
|
$start = Carbon::now()->subDays(3);
|
|
|
|
|
$end = Carbon::now();
|
|
|
|
|
|
|
|
|
|
// * Make sure a customer answer is set.
|
2025-08-05 15:30:34 +02:00
|
|
|
$answer = Answer::findOrFail($answersIds->first());
|
|
|
|
|
DB::table('model_answer')->insert([
|
|
|
|
|
'answer_id' => $answer->getKey(),
|
|
|
|
|
'model_id' => $customer->getKey(),
|
|
|
|
|
'model_type' => $customer->getMorphClass(),
|
|
|
|
|
]);
|
2021-12-19 15:57:24 +01:00
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => $start->isoFormat('YYYY-MM-DD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => $end->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$datesIndexes = [];
|
|
|
|
|
|
|
|
|
|
do {
|
2025-07-23 14:24:38 +02:00
|
|
|
$datesIndexes[$start->isoFormat('YYYY-MM-DD')] = $answersIds->keys()->all();
|
|
|
|
|
$start = $start->addDay();
|
2021-12-19 15:57:24 +01:00
|
|
|
} while ($start->lessThanOrEqualTo($end));
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
2021-12-19 15:57:24 +01:00
|
|
|
$json->assertStructure([
|
|
|
|
|
'data' => [
|
|
|
|
|
'answers' => [
|
|
|
|
|
'*' => [
|
|
|
|
|
'id',
|
|
|
|
|
'answer'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'stats' => $datesIndexes
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test stats by question when it does not belong to brand.
|
2023-09-06 17:24:53 +02:00
|
|
|
* route: apiv.stats.questionStats
|
|
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByQuestionShouldFailWithInvalidQuestion(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
$question = Question::where('brand_id', '!=', $service->brand->id)->firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
2023-09-06 17:24:53 +02:00
|
|
|
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value());
|
2021-12-19 15:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test stats by question should fail with date bigger than 3 month.
|
|
|
|
|
* route: apiV2.stats.questionStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByQuestionShouldFailWithInvalidDate(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
2023-09-06 17:24:53 +02:00
|
|
|
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value());
|
2021-12-19 15:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test stats by question should fail with invalid date format.
|
|
|
|
|
* route: apiV2.stats.questionStats
|
2023-09-06 17:24:53 +02:00
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-04-17 11:32:43 +02:00
|
|
|
public function testStatsByQuestionShouldFailWithInvalidDateFormat(): void
|
2021-12-19 15:57:24 +01:00
|
|
|
{
|
|
|
|
|
$service = Service::firstOrFail();
|
|
|
|
|
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
|
|
|
|
|
|
|
|
|
|
$response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [
|
2023-06-28 15:47:23 +02:00
|
|
|
'start' => Carbon::now()->subMonths(3)->isoFormat('YYYY-MM Ddh'),
|
2025-04-17 11:32:43 +02:00
|
|
|
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
|
2021-12-19 15:57:24 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$json = $response->decodeResponseJson();
|
2023-09-04 13:08:34 +02:00
|
|
|
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
2023-09-06 17:24:53 +02:00
|
|
|
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value());
|
2021-12-19 15:57:24 +01:00
|
|
|
}
|
|
|
|
|
}
|