commit 08-2025
This commit is contained in:
parent
9ae662f63e
commit
480fdc65ed
404 changed files with 65310 additions and 2600431 deletions
297
tests/Unit/BusinessPlan/BusinessUserItemOptimizedTest.php
Normal file
297
tests/Unit/BusinessPlan/BusinessUserItemOptimizedTest.php
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\BusinessPlan;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\User;
|
||||
use App\Models\UserBusiness;
|
||||
use App\Models\UserLevel;
|
||||
use App\Models\UserAccount;
|
||||
use App\Services\BusinessPlan\BusinessUserItemOptimized;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Carbon\Carbon;
|
||||
use stdClass;
|
||||
|
||||
class BusinessUserItemOptimizedTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private $dateObj;
|
||||
private $businessUserItem;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dateObj = new stdClass();
|
||||
$this->dateObj->month = 1;
|
||||
$this->dateObj->year = 2024;
|
||||
$this->dateObj->start_date = '2024-01-01 00:00:00';
|
||||
$this->dateObj->end_date = '2024-01-31 23:59:59';
|
||||
|
||||
$this->businessUserItem = new BusinessUserItemOptimized($this->dateObj);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function constructor_initializes_properties_correctly()
|
||||
{
|
||||
$businessUserItem = new BusinessUserItemOptimized($this->dateObj);
|
||||
|
||||
$this->assertEquals($this->dateObj, $businessUserItem->__get('date'));
|
||||
$this->assertEquals([], $businessUserItem->businessUserItems);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function makeUser_finds_existing_business_user()
|
||||
{
|
||||
// Create user and existing business data
|
||||
$user = User::factory()->create();
|
||||
$existingBusiness = UserBusiness::create([
|
||||
'user_id' => $user->id,
|
||||
'month' => 1,
|
||||
'year' => 2024,
|
||||
'sales_volume_KP_points' => 100
|
||||
]);
|
||||
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$this->assertEquals($existingBusiness->id, $this->businessUserItem->getBUser()->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function makeUser_handles_non_existent_user()
|
||||
{
|
||||
$this->businessUserItem->makeUser(999);
|
||||
|
||||
// Should not throw exception and should handle gracefully
|
||||
$this->assertNull($this->businessUserItem->getBUser());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function makeUserFromModel_throws_exception_for_invalid_user()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Invalid user model provided');
|
||||
|
||||
$invalidUser = new User(); // User without ID
|
||||
$this->businessUserItem->makeUserFromModel($invalidUser);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function makeUserFromModel_creates_business_user_from_valid_model()
|
||||
{
|
||||
// Create user with account and level
|
||||
$user = User::factory()->create([
|
||||
'm_level' => 1,
|
||||
'payment_account' => '2024-01-15',
|
||||
'active_date' => '2024-01-01'
|
||||
]);
|
||||
|
||||
$account = UserAccount::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'm_account' => 'TEST123'
|
||||
]);
|
||||
|
||||
$userLevel = UserLevel::factory()->create([
|
||||
'id' => 1,
|
||||
'name' => 'Bronze',
|
||||
'pos' => 1,
|
||||
'margin' => 10,
|
||||
'margin_shop' => 15,
|
||||
'qual_kp' => 100,
|
||||
'qual_pp' => 200
|
||||
]);
|
||||
|
||||
// Load user with relations
|
||||
$userWithRelations = User::with(['account', 'userLevel'])->find($user->id);
|
||||
|
||||
$this->businessUserItem->makeUserFromModel($userWithRelations);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$this->assertNotNull($bUser);
|
||||
$this->assertEquals($user->id, $bUser->user_id);
|
||||
$this->assertEquals('John', $bUser->first_name);
|
||||
$this->assertEquals('Doe', $bUser->last_name);
|
||||
$this->assertEquals('Bronze', $bUser->user_level_name);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function addBusinessLinePoints_handles_non_existent_line()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
// This should log a warning but not throw an exception
|
||||
$this->businessUserItem->addBusinessLinePoints(1, 100);
|
||||
|
||||
// Test passes if no exception is thrown
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function addBusinessLinePoints_adds_points_correctly()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
// First, add a business line
|
||||
$obj = new stdClass();
|
||||
$obj->points = 50;
|
||||
$this->businessUserItem->addBusinessLineToUser(1, $obj);
|
||||
|
||||
// Then add more points
|
||||
$this->businessUserItem->addBusinessLinePoints(1, 100);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$this->assertEquals(150, $bUser->business_lines[1]->points);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function addTotalTP_adds_points_with_type_safety()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$this->businessUserItem->addTotalTP('100.5'); // String should be converted to float
|
||||
$this->businessUserItem->addTotalTP(50);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$this->assertEquals(150.5, $bUser->total_pp);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function isQualKP_returns_correct_boolean()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$userLevel = UserLevel::factory()->create([
|
||||
'id' => 1,
|
||||
'qual_kp' => 100
|
||||
]);
|
||||
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
// Manually set values for testing
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$bUser->sales_volume_points_KP_sum = 150;
|
||||
$bUser->qual_kp = 100;
|
||||
|
||||
$this->assertTrue($this->businessUserItem->isQualKP());
|
||||
|
||||
$bUser->sales_volume_points_KP_sum = 50;
|
||||
$this->assertFalse($this->businessUserItem->isQualKP());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function getRestQualKP_returns_positive_value_or_zero()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$bUser->sales_volume_points_KP_sum = 150;
|
||||
$bUser->qual_kp = 100;
|
||||
|
||||
$this->assertEquals(50, $this->businessUserItem->getRestQualKP());
|
||||
|
||||
$bUser->sales_volume_points_KP_sum = 50;
|
||||
$this->assertEquals(0, $this->businessUserItem->getRestQualKP()); // Should not be negative
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function getCommissionTotal_calculates_correctly()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$bUser->commission_shop_sales = 10.50;
|
||||
$bUser->commission_pp_total = 25.75;
|
||||
$bUser->commission_growth_total = 15.25;
|
||||
|
||||
$total = $this->businessUserItem->getCommissionTotal();
|
||||
$this->assertEquals(51.50, $total);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function checkSponsor_handles_user_without_sponsor()
|
||||
{
|
||||
$user = User::factory()->create(['m_sponsor' => null]);
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$this->businessUserItem->checkSponsor($user);
|
||||
|
||||
$bUser = $this->businessUserItem->getBUser();
|
||||
$this->assertFalse($bUser->sponsor->is_sponsor);
|
||||
$this->assertEquals('Keinen Sponsor zugewiesen', $bUser->sponsor->full_name);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function isSave_returns_correct_boolean()
|
||||
{
|
||||
// New business user item should not be saved
|
||||
$this->assertFalse($this->businessUserItem->isSave());
|
||||
|
||||
// Create and save a business user
|
||||
$user = User::factory()->create();
|
||||
$business = UserBusiness::create([
|
||||
'user_id' => $user->id,
|
||||
'month' => 1,
|
||||
'year' => 2024
|
||||
]);
|
||||
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
$this->assertTrue($this->businessUserItem->isSave());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function magic_get_method_returns_properties()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->businessUserItem->makeUser($user->id);
|
||||
|
||||
$userId = $this->businessUserItem->__get('user_id');
|
||||
$this->assertEquals($user->id, $userId);
|
||||
|
||||
$nullValue = $this->businessUserItem->__get('non_existent_property');
|
||||
$this->assertNull($nullValue);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function readParentsBusinessUsers_loads_child_users()
|
||||
{
|
||||
// Create parent user
|
||||
$parentUser = User::factory()->create([
|
||||
'payment_account' => '2024-01-15',
|
||||
'active_date' => '2024-01-01',
|
||||
'm_level' => 1
|
||||
]);
|
||||
|
||||
// Create child users
|
||||
$childUser1 = User::factory()->create([
|
||||
'm_sponsor' => $parentUser->id,
|
||||
'payment_account' => '2024-01-15',
|
||||
'active_date' => '2024-01-01',
|
||||
'm_level' => 1
|
||||
]);
|
||||
|
||||
$childUser2 = User::factory()->create([
|
||||
'm_sponsor' => $parentUser->id,
|
||||
'payment_account' => '2024-01-15',
|
||||
'active_date' => '2024-01-01',
|
||||
'm_level' => 1
|
||||
]);
|
||||
|
||||
// Create accounts for users
|
||||
UserAccount::factory()->create(['user_id' => $parentUser->id]);
|
||||
UserAccount::factory()->create(['user_id' => $childUser1->id]);
|
||||
UserAccount::factory()->create(['user_id' => $childUser2->id]);
|
||||
|
||||
$this->businessUserItem->makeUser($parentUser->id);
|
||||
$this->businessUserItem->readParentsBusinessUsers();
|
||||
|
||||
// Should have loaded 2 child business users
|
||||
$this->assertCount(2, $this->businessUserItem->businessUserItems);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue