mivita/tests/Feature/BusinessStoreOptimized/AssignMonthlyQualKpBonusPointsTest.php
2026-04-10 17:15:27 +02:00

117 lines
3.2 KiB
PHP

<?php
use App\Console\Commands\BusinessStoreOptimized;
use App\Models\UserLevel;
use App\Models\UserSalesVolume;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(Tests\TestCase::class, RefreshDatabase::class);
function makeCommand(): BusinessStoreOptimized
{
$command = new BusinessStoreOptimized;
$input = new \Symfony\Component\Console\Input\ArrayInput([]);
$output = new \Illuminate\Console\OutputStyle($input, new \Symfony\Component\Console\Output\NullOutput);
$command->setOutput($output);
return $command;
}
function callAssignMethod(BusinessStoreOptimized $command): void
{
$method = (new ReflectionClass($command))->getMethod('assignMonthlyQualKpBonusPoints');
$method->setAccessible(true);
$method->invoke($command);
}
function makeUserLevel(int $qualKp, int $pos = 7): UserLevel
{
return UserLevel::create([
'name' => 'Test Level '.$pos,
'qual_kp' => $qualKp,
'active' => 1,
'pos' => $pos,
'default' => 0,
]);
}
function makeUser(int $levelId, ?int $forceId = null): User
{
$attributes = [
'email' => fake()->unique()->safeEmail(),
'password' => bcrypt('secret'),
'lang' => 'de',
'm_level' => $levelId,
];
if ($forceId !== null) {
$attributes['id'] = $forceId;
}
return User::forceCreate($attributes);
}
it('assigns qual_kp points to user 486 when their level has qual_kp > 0', function () {
$level = makeUserLevel(690);
makeUser($level->id, 486);
callAssignMethod(makeCommand());
expect(UserSalesVolume::where('user_id', 486)
->where('month', date('m'))
->where('year', date('Y'))
->where('info', 'qual_kp_bonus')
->where('points', 690)
->exists()
)->toBeTrue();
});
it('does not assign bonus points to users not in the bonus list', function () {
$level = makeUserLevel(690);
$otherUser = makeUser($level->id);
callAssignMethod(makeCommand());
expect(UserSalesVolume::where('user_id', $otherUser->id)->where('info', 'qual_kp_bonus')->exists())
->toBeFalse();
});
it('skips user 486 when their level has qual_kp of zero', function () {
$level = makeUserLevel(0);
makeUser($level->id, 486);
callAssignMethod(makeCommand());
expect(UserSalesVolume::where('user_id', 486)->where('info', 'qual_kp_bonus')->exists())
->toBeFalse();
});
it('skips user 486 when they have no level assigned', function () {
User::forceCreate([
'id' => 486,
'email' => fake()->unique()->safeEmail(),
'password' => bcrypt('secret'),
'lang' => 'de',
'm_level' => null,
]);
callAssignMethod(makeCommand());
expect(UserSalesVolume::where('user_id', 486)->where('info', 'qual_kp_bonus')->exists())
->toBeFalse();
});
it('is idempotent and does not create duplicate entries for the same month', function () {
$level = makeUserLevel(690);
makeUser($level->id, 486);
$command = makeCommand();
callAssignMethod($command);
callAssignMethod($command);
expect(UserSalesVolume::where('user_id', 486)
->where('info', 'qual_kp_bonus')
->count()
)->toBe(1);
});