This commit is contained in:
lubiana 2025-05-31 21:43:13 +02:00
parent e958163a4a
commit b8a5a1ff58
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
79 changed files with 15113 additions and 0 deletions

View file

@ -0,0 +1,203 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Service;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\InventoryRecord;
use App\Entity\Order;
use App\Entity\OrderItem;
use App\Service\StockAdjustmentService;
use App\Service\ConfigurationService;
use App\Service\InventoryService;
use App\Enum\SystemSettingKey;
use App\Repository\DrinkTypeRepository;
use App\Repository\InventoryRecordRepository;
use App\Repository\OrderRepository;
use App\Repository\OrderItemRepository;
use DateTimeImmutable;
class StockAdjustmentServiceTest extends TestCase
{
private StockAdjustmentService $service;
private DrinkTypeRepository $drinkTypeRepository;
private InventoryRecordRepository $inventoryRecordRepository;
private OrderRepository $orderRepository;
private OrderItemRepository $orderItemRepository;
private ConfigurationService $configService;
private InventoryService $inventoryService;
private DrinkType $drinkType;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->drinkTypeRepository = $this->container->get(DrinkTypeRepository::class);
$this->inventoryRecordRepository = $this->container->get(InventoryRecordRepository::class);
$this->orderRepository = $this->container->get(OrderRepository::class);
$this->orderItemRepository = $this->container->get(OrderItemRepository::class);
$this->configService = $this->container->get(ConfigurationService::class);
$this->inventoryService = $this->container->get(InventoryService::class);
$this->service = $this->container->get(StockAdjustmentService::class);
// Initialize default configs
$this->configService->initializeDefaultConfigs();
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
}
public function testAdjustStockLevels(): void
{
// Create orders with different dates
$order1 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-02'));
$order3 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-03'));
$order4 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-04'));
$order5 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-05'));
$this->orderRepository->save($order1);
$this->orderRepository->save($order2);
$this->orderRepository->save($order3);
$this->orderRepository->save($order4);
$this->orderRepository->save($order5);
// Create order items for the drink type with different quantities
$orderItem1 = new OrderItem($this->drinkType, 5, $order1);
$orderItem2 = new OrderItem($this->drinkType, 10, $order2);
$orderItem3 = new OrderItem($this->drinkType, 15, $order3);
$orderItem4 = new OrderItem($this->drinkType, 20, $order4);
$orderItem5 = new OrderItem($this->drinkType, 25, $order5);
$this->orderItemRepository->save($orderItem1);
$this->orderItemRepository->save($orderItem2);
$this->orderItemRepository->save($orderItem3);
$this->orderItemRepository->save($orderItem4);
$this->orderItemRepository->save($orderItem5);
// Set the desired stock to a value that will trigger an adjustment
$this->drinkType->setDesiredStock(5);
$this->drinkTypeRepository->save($this->drinkType);
// Adjust stock levels
$result = $this->service->adjustStockLevels();
// Check that the adjustment was made
$this->assertCount(1, $result);
$this->assertEquals($this->drinkType->getId(), $result[0]['drinkType']->getId());
$this->assertEquals(5, $result[0]['oldDesiredStock']);
// The new desired stock should be higher than the old one
$this->assertGreaterThan(5, $result[0]['newDesiredStock']);
// Check that the drink type was updated in the database
$updatedDrinkType = $this->drinkTypeRepository->find($this->drinkType->getId());
$this->assertEquals($result[0]['newDesiredStock'], $updatedDrinkType->getDesiredStock());
}
public function testAdjustStockLevelsWithNoOrders(): void
{
// No orders, so no adjustment should be made
$result = $this->service->adjustStockLevels();
// Check that no adjustment was made
$this->assertCount(0, $result);
// Check that the drink type was not updated
$updatedDrinkType = $this->drinkTypeRepository->find($this->drinkType->getId());
$this->assertEquals(10, $updatedDrinkType->getDesiredStock());
}
public function testAdjustStockLevelsWithSmallDifference(): void
{
// Create orders with different dates
$order1 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-01'));
$this->orderRepository->save($order1);
// Create order items for the drink type with a quantity close to the desired stock
$orderItem1 = new OrderItem($this->drinkType, 11, $order1);
$this->orderItemRepository->save($orderItem1);
// Set the threshold to a high value so that small differences don't trigger an adjustment
$this->configService->setConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_THRESHOLD->value, '0.5');
// Adjust stock levels
$result = $this->service->adjustStockLevels();
// Check that no adjustment was made because the difference is too small
$this->assertCount(0, $result);
// Check that the drink type was not updated
$updatedDrinkType = $this->drinkTypeRepository->find($this->drinkType->getId());
$this->assertEquals(10, $updatedDrinkType->getDesiredStock());
}
public function testGetConsumptionHistory(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 20, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-02')); // Consumption: 5
$record3 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-03')); // Consumption: 5
$record4 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-04')); // Consumption: 5
$record5 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-05')); // No consumption (increase)
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
$this->inventoryRecordRepository->save($record4);
$this->inventoryRecordRepository->save($record5);
// Get consumption history
$start = new DateTimeImmutable('2023-01-01');
$end = new DateTimeImmutable('2023-01-05');
$history = $this->service->getConsumptionHistory($this->drinkType, $start, $end);
// Check that the history is correct
$this->assertCount(3, $history); // 3 consumption events
// Check the first consumption event
$this->assertEquals('2023-01-02', $history[0]['date']->format('Y-m-d'));
$this->assertEquals(5, $history[0]['consumption']);
// Check the second consumption event
$this->assertEquals('2023-01-03', $history[1]['date']->format('Y-m-d'));
$this->assertEquals(5, $history[1]['consumption']);
// Check the third consumption event
$this->assertEquals('2023-01-04', $history[2]['date']->format('Y-m-d'));
$this->assertEquals(5, $history[2]['consumption']);
}
public function testGetConsumptionHistoryWithDateRange(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 20, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-02')); // Consumption: 5
$record3 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-03')); // Consumption: 5
$record4 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-04')); // Consumption: 5
$record5 = new InventoryRecord($this->drinkType, 0, new DateTimeImmutable('2023-01-05')); // Consumption: 5
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
$this->inventoryRecordRepository->save($record4);
$this->inventoryRecordRepository->save($record5);
// Get consumption history for a specific date range
$start = new DateTimeImmutable('2023-01-02');
$end = new DateTimeImmutable('2023-01-04');
$history = $this->service->getConsumptionHistory($this->drinkType, $start, $end);
// Check that the history is correct
$this->assertCount(2, $history); // 2 consumption events within the range
// Check the first consumption event
$this->assertEquals('2023-01-03', $history[0]['date']->format('Y-m-d'));
$this->assertEquals(5, $history[0]['consumption']);
// Check the second consumption event
$this->assertEquals('2023-01-04', $history[1]['date']->format('Y-m-d'));
$this->assertEquals(5, $history[1]['consumption']);
}
}