ldc-crm/tests/CRM/ApiV4/StatsTest.php
Alexis Gousseau 303834bbca
Crm V4
- Make all tests pass Client (V4),CRMV2,CRMV4
- Simplify client submodule path
- Move App_url to .env.testing
2023-10-06 16:37:29 +02:00

209 lines
7.1 KiB
PHP

<?php
namespace Tests\CRM\ApiV4;
use App\Enums\ApiError;
use App\Enums\ApiStatus;
use App\Models\Customer;
use App\Models\CustomerAnswer;
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;
/**
* Test get stats by brand.
* route: apiV4.stats.brandStats
*
* @return void
*/
public function testStatsByBrand()
{
$service = Service::firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.brandStats'), [
'start' => Carbon::now()->subMonths(3)->isoFormat('YYYY-MM-DD'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
$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: apiV4.stats.brandStats
*
* @return void
*/
public function testStatsByBrandShouldFailWithInvalidDate()
{
$service = Service::firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.brandStats'), [
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value);
}
/**
* Test get stats by brand should fail with invalid date format.
* route: apiV4.stats.brandStats
*
* @return void
*/
public function testStatsByBrandShouldFailWithInvalidDateFormat()
{
$service = Service::firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.brandStats'), [
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD HH:mmDD'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value);
}
/**
* Test stats by question.
* route: apiV4.stats.questionStats
*
* @return void
*/
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();
// * Make sure a customer answer is set.
/** @var \App\Models\CustomerAnswer $custAnswer */
$custAnswer = CustomerAnswer::whereHas('answer', function ($q) use ($answersIds) {
$q->whereId($answersIds->first());
})->firstOrNew();
$custAnswer->customer()->associate($custAnswer->customer ?? Customer::query()->firstOrFail());
$custAnswer->answer()->associate($answersIds->first());
$custAnswer->saveOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.questionStats', $question), [
'start' => $start->isoFormat('YYYY-MM-DD'),
'end' => $end->isoFormat('YYYY-MM-DD')
]);
$datesIndexes = [];
do {
$datesIndexes[$start->isoFormat('YYYY-MM-DD')] = $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
]
]);
}
/**
* Test stats by question when it does not belong to brand.
* route: apiv.stats.questionStats
*
* @return void
*/
public function testStatsByQuestionShouldFailWithInvalidQuestion()
{
$service = Service::firstOrFail();
$question = Question::where('brand_id', '!=', $service->brand->id)->firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.questionStats', $question), [
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value);
}
/**
* Test stats by question should fail with date bigger than 3 month.
* route: apiV4.stats.questionStats
*
* @return void
*/
public function testStatsByQuestionShouldFailWithInvalidDate()
{
$service = Service::firstOrFail();
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.questionStats', $question), [
'start' => Carbon::now()->subMonths(3)->subDay()->isoFormat('YYYY-MM-DD'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value);
}
/**
* Test stats by question should fail with invalid date format.
* route: apiV4.stats.questionStats
*
* @return void
*/
public function testStatsByQuestionShouldFailWithInvalidDateFormat()
{
$service = Service::firstOrFail();
$question = Question::whereBrandId($service->brand->id)->firstOrFail();
$response = $this->callApiWithToken($service, 'get', route('apiV4.stats.questionStats', $question), [
'start' => Carbon::now()->subMonths(3)->isoFormat('YYYY-MM Ddh'),
'end' => Carbon::now()->isoFormat('YYYY-MM-DD')
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
$this->assertEquals($json->json('data.code'), ApiError::validationFailed->value);
}
}