ldc-crm/tests/Client/CrmClientStatsTest.php
2025-09-08 14:59:53 +02:00

191 lines
5.8 KiB
PHP

<?php
namespace Tests\Client;
use App\Models\Question;
use Illuminate\Support\Carbon;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Models\Question as ModelsQuestion;
use LDC\LDCCRM\Models\Stats;
class CrmClientStatsTest extends CrmClientTester
{
/**
* Get stats by brand.
*
* @return void
*/
public function testStatsByBrand(): void
{
$start = Carbon::now()->subMonths(3);
$end = Carbon::now();
$stats = Stats::getBrandStats(
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
$this->assertIsArray($stats, 'Results should be an array');
$i = 0;
$keys = [
'date', 'active', 'inactive', 'old_accounts', 'new_accounts',
'total_accounts', 'total_opt_in'
];
do {
$this->assertArrayHasKey($i, $stats);
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $stats[$i]);
}
$this->assertEquals($start->isoFormat('YYYY-MM-DD'), $stats[$i++]['date']);
$start = $start->addDay();
} while ($start->lessThanOrEqualTo($end));
}
/**
* Get stat end date is before start date.
*
* @return void
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException Because end date is before start date.
*/
public function testStatsByBrandShouldFailIfStartDateIsAfterEndDate(): void
{
$this->expectException(ApiErrorException::class);
$start = Carbon::now();
$end = Carbon::now()->subDays(6);
try {
Stats::getBrandStats(
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
} catch (ApiErrorException $e) {
$this->assertEquals(ApiError::validationFailed, $e->getApiError());
throw $e;
}
}
/**
* Test interval 3 months max.
*
* @return void
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException Because interval is bigger thatn 3 months.
*/
public function testStatsByBrandShouldFailIfIntervalIsMoreThanThreeMonths(): void
{
$this->expectException(ApiErrorException::class);
$start = Carbon::now()->subMonths(3)->subDay();
$end = Carbon::now();
try {
Stats::getBrandStats(
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
} catch (ApiErrorException $e) {
$this->assertEquals(ApiError::validationFailed, $e->getApiError());
throw $e;
}
}
/**
* Test get stat by question.
*
* @return void
*/
public function testStatsByQuestion(): void
{
/** @var \App\Models\Question $question */
$question = Question::where('brand_id', self::$brand->id)->first();
$answersIds = $question->answers()->pluck('id');
$start = Carbon::now()->subMonths(3);
$end = Carbon::now();
$stats = Stats::getQuestionStats(
new ModelsQuestion([
'id' => $question->id
]),
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
do {
$startD = $start->isoFormat('YYYY-MM-DD');
$this->assertArrayHasKey($startD, $stats['stats']);
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
foreach ($answersIds as $key => $id) {
$this->assertArrayHasKey($key, $stats['stats'][$startD]);
}
$start = $start->addDay();
} while ($start->lessThanOrEqualTo($end));
$this->assertIsArray($stats['stats'], 'Results should be an array');
}
/**
* Get stats by question when it does not belongs to brand.
*
* @return void
*/
public function testStatsByQuestionShouldFailIfQuestionDoesNotBelongsToBrand(): void
{
$this->expectException(ApiErrorException::class);
/** @var \App\Models\Question $question */
$question = Question::where('brand_id', '!=', self::$brand->id)->first();
$start = Carbon::now()->subMonths(3);
$end = Carbon::now();
Stats::getQuestionStats(
new ModelsQuestion([
'id' => $question->id
]),
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
}
/**
* Get stat by question when end date is before start date.
*
* @return void
*/
public function testStatsByQuestionShouldFailIfStartDateIsAfterEndDate(): void
{
$this->expectException(ApiErrorException::class);
/** @var \App\Models\Question $question */
$question = Question::where('brand_id', '!=', self::$brand->id)->first();
$start = Carbon::now();
$end = Carbon::now()->subDays(6);
Stats::getQuestionStats(
new ModelsQuestion([
'id' => $question->id
]),
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
}
/**
* Get stat by question when interval is more thatn 3 months.
*
* @return void
*/
public function testStatsByQuestionShouldFailIfIntervalIsMoreThanThreeMonths(): void
{
$this->expectException(ApiErrorException::class);
/** @var \App\Models\Question $question */
$question = Question::where('brand_id', '!=', self::$brand->id)->first();
$start = Carbon::now()->subMonths(3)->subDay();
$end = Carbon::now();
Stats::getQuestionStats(
new ModelsQuestion([
'id' => $question->id
]),
$start->isoFormat('YYYY-MM-DD'),
$end->isoFormat('YYYY-MM-DD')
);
}
}