124 lines
4.3 KiB
PHP
124 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Contracts\Directories\EmployeeDirectory;
|
|
use App\Contracts\Directories\OrderDirectory;
|
|
use App\Services\StatsService;
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\StatsCustomerType;
|
|
|
|
describe('StatsService', function () {
|
|
it('calculates membership rate correctly', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$employeeDirectory->shouldReceive('countActiveEmployeesWithin')->andReturn(100);
|
|
$employeeDirectory->shouldReceive('countActiveRegisteredEmployeesWithin')->andReturn(25);
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
|
|
// WHEN
|
|
$rate = $service->getMembershipRate($start, $end);
|
|
|
|
// THEN
|
|
expect($rate)->toBe(25.0);
|
|
});
|
|
|
|
it('returns 0 for membership rate when no active employees', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$employeeDirectory->shouldReceive('countActiveEmployeesWithin')->andReturn(0);
|
|
$employeeDirectory->shouldReceive('countActiveRegisteredEmployeesWithin')->andReturn(0);
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
|
|
// WHEN
|
|
$rate = $service->getMembershipRate($start, $end);
|
|
|
|
// THEN
|
|
expect($rate)->toBe(0.0);
|
|
});
|
|
|
|
it('calculates membership percentage correctly', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$employeeDirectory->shouldReceive('countActiveEmployees')->andReturn(200);
|
|
$employeeDirectory->shouldReceive('countActiveRegisteredEmployees')->andReturn(150);
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
// WHEN
|
|
$percentage = $service->getMembershipPercentage();
|
|
|
|
// THEN
|
|
expect($percentage)->toBe(75.0);
|
|
});
|
|
|
|
it('returns 0 for membership percentage when no active employees', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$employeeDirectory->shouldReceive('countActiveEmployees')->andReturn(0);
|
|
$employeeDirectory->shouldReceive('countActiveRegisteredEmployees')->andReturn(0);
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
// WHEN
|
|
$percentage = $service->getMembershipPercentage();
|
|
|
|
// THEN
|
|
expect($percentage)->toBe(0.0);
|
|
});
|
|
|
|
it('gets the previous period', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
|
|
// WHEN
|
|
$previousPeriod = $service->getPreviousPeriod($start, $end);
|
|
|
|
// THEN
|
|
expect($previousPeriod->getStartDate()->toDateString())->toBe($start->copy()->subDays(11)->toDateString());
|
|
expect($previousPeriod->getIncludedEndDate()->toDateString())->toBe($start->copy()->subDays(1)->toDateString());
|
|
});
|
|
|
|
it('calculates nbSalarie stat correctly', function () {
|
|
// GIVEN
|
|
$employeeDirectory = mock(EmployeeDirectory::class);
|
|
$orderDirectory = mock(OrderDirectory::class);
|
|
|
|
$employeeDirectory->shouldReceive('countActiveEmployeesWithin')->andReturnValues([50, 100]); // previous, current
|
|
|
|
$service = new StatsService($employeeDirectory, $orderDirectory);
|
|
|
|
$start = CarbonImmutable::now()->subDays(10);
|
|
$end = CarbonImmutable::now();
|
|
|
|
// WHEN
|
|
$stats = $service->getStats([StatsCustomerType::nbSalarie], $start, $end);
|
|
|
|
// THEN
|
|
expect($stats[StatsCustomerType::nbSalarie->value]['previous'])->toBe(50);
|
|
expect($stats[StatsCustomerType::nbSalarie->value]['current'])->toBe(100);
|
|
expect($stats[StatsCustomerType::nbSalarie->value]['diff'])->toBe(50);
|
|
});
|
|
});
|