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,26 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Override;
use Tests\TestCase;
use App\Repository\DrinkTypeRepository;
class DatabaseTest extends TestCase
{
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
}
public function testDatabase(): void
{
/** @var DrinkTypeRepository $drinkRepository */
$drinkRepository = $this->container->get(DrinkTypeRepository::class);
$this->assertCount(0, $drinkRepository->findAll());
}
}

View file

@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Repository\DrinkTypeRepository;
class DrinkTypeRepositoryTest extends TestCase
{
private DrinkTypeRepository $repository;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(DrinkTypeRepository::class);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some drink types
$drinkType1 = new DrinkType('Cola', 'Refreshing cola drink', 10);
$drinkType2 = new DrinkType('Fanta', 'Orange soda', 5);
// Save them to the repository
$this->repository->save($drinkType1);
$this->repository->save($drinkType2);
// Now findAll should return both drink types
$drinkTypes = $this->repository->findAll();
$this->assertCount(2, $drinkTypes);
$this->assertContainsOnlyInstancesOf(DrinkType::class, $drinkTypes);
}
public function testFind(): void
{
// Create a drink type
$drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->repository->save($drinkType);
// Get the ID
$id = $drinkType->getId();
$this->assertNotNull($id);
// Find by ID
$foundDrinkType = $this->repository->find($id);
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
$this->assertEquals('Refreshing cola drink', $foundDrinkType->getDescription());
$this->assertEquals(10, $foundDrinkType->getDesiredStock());
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindOneBy(): void
{
// Create a drink type
$drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->repository->save($drinkType);
// Find by name
$foundDrinkType = $this->repository->findOneBy(['name' => 'Cola']);
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
// Try to find a non-existent name
$this->assertNull($this->repository->findOneBy(['name' => 'NonExistent']));
}
public function testSave(): void
{
// Create a drink type
$drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
// Save it
$this->repository->save($drinkType);
// Check that it was saved
$id = $drinkType->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundDrinkType = $this->repository->find($id);
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
// Update it
$drinkType->setName('Pepsi');
$this->repository->save($drinkType);
// Find it again to confirm the update
$foundDrinkType = $this->repository->find($id);
$this->assertEquals('Pepsi', $foundDrinkType->getName());
}
public function testRemove(): void
{
// Create a drink type
$drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->repository->save($drinkType);
// Get the ID
$id = $drinkType->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($drinkType);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}

View file

@ -0,0 +1,197 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\InventoryRecord;
use App\Repository\DrinkTypeRepository;
use App\Repository\InventoryRecordRepository;
use DateTimeImmutable;
class InventoryRecordRepositoryTest extends TestCase
{
private InventoryRecordRepository $repository;
private DrinkTypeRepository $drinkTypeRepository;
private DrinkType $drinkType;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(InventoryRecordRepository::class);
$this->drinkTypeRepository = $this->container->get(DrinkTypeRepository::class);
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some inventory records
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
// Save them to the repository
$this->repository->save($record1);
$this->repository->save($record2);
// Now findAll should return both records
$records = $this->repository->findAll();
$this->assertCount(2, $records);
$this->assertContainsOnlyInstancesOf(InventoryRecord::class, $records);
}
public function testFind(): void
{
// Create an inventory record
$record = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$this->repository->save($record);
// Get the ID
$id = $record->getId();
$this->assertNotNull($id);
// Find by ID
$foundRecord = $this->repository->find($id);
$this->assertInstanceOf(InventoryRecord::class, $foundRecord);
$this->assertEquals(5, $foundRecord->getQuantity());
$this->assertEquals('2023-01-01', $foundRecord->getTimestamp()->format('Y-m-d'));
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindByDrinkType(): void
{
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Create inventory records for both drink types
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($anotherDrinkType, 15, new DateTimeImmutable('2023-01-03'));
// Save them to the repository
$this->repository->save($record1);
$this->repository->save($record2);
$this->repository->save($record3);
// Find by drink type
$records = $this->repository->findByDrinkType($this->drinkType);
$this->assertCount(2, $records);
$this->assertContainsOnlyInstancesOf(InventoryRecord::class, $records);
// Check that the records are for the correct drink type
foreach ($records as $record) {
$this->assertEquals($this->drinkType->getId(), $record->getDrinkType()->getId());
}
// Find by the other drink type
$otherRecords = $this->repository->findByDrinkType($anotherDrinkType);
$this->assertCount(1, $otherRecords);
$this->assertEquals($anotherDrinkType->getId(), $otherRecords[0]->getDrinkType()->getId());
}
public function testFindLatestByDrinkType(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-03'));
// Save them to the repository
$this->repository->save($record1);
$this->repository->save($record2);
$this->repository->save($record3);
// Find the latest record
$latestRecord = $this->repository->findLatestByDrinkType($this->drinkType);
$this->assertInstanceOf(InventoryRecord::class, $latestRecord);
$this->assertEquals(15, $latestRecord->getQuantity());
$this->assertEquals('2023-01-03', $latestRecord->getTimestamp()->format('Y-m-d'));
}
public function testFindByTimestampRange(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-03'));
$record4 = new InventoryRecord($this->drinkType, 20, new DateTimeImmutable('2023-01-04'));
$record5 = new InventoryRecord($this->drinkType, 25, new DateTimeImmutable('2023-01-05'));
// Save them to the repository
$this->repository->save($record1);
$this->repository->save($record2);
$this->repository->save($record3);
$this->repository->save($record4);
$this->repository->save($record5);
// Find records in a specific range
$start = new DateTimeImmutable('2023-01-02');
$end = new DateTimeImmutable('2023-01-04');
$records = $this->repository->findByTimestampRange($start, $end);
$this->assertCount(3, $records);
// Check that the records are within the range
foreach ($records as $record) {
$timestamp = $record->getTimestamp();
$this->assertTrue($timestamp >= $start && $timestamp <= $end);
}
}
public function testSave(): void
{
// Create an inventory record
$record = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
// Save it
$this->repository->save($record);
// Check that it was saved
$id = $record->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundRecord = $this->repository->find($id);
$this->assertInstanceOf(InventoryRecord::class, $foundRecord);
$this->assertEquals(5, $foundRecord->getQuantity());
// Update it
$record->setQuantity(10);
$this->repository->save($record);
// Find it again to confirm the update
$foundRecord = $this->repository->find($id);
$this->assertEquals(10, $foundRecord->getQuantity());
}
public function testRemove(): void
{
// Create an inventory record
$record = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$this->repository->save($record);
// Get the ID
$id = $record->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($record);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}

View file

@ -0,0 +1,226 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\Order;
use App\Entity\OrderItem;
use App\Repository\DrinkTypeRepository;
use App\Repository\OrderRepository;
use App\Repository\OrderItemRepository;
use DateTimeImmutable;
class OrderItemRepositoryTest extends TestCase
{
private OrderItemRepository $repository;
private DrinkTypeRepository $drinkTypeRepository;
private OrderRepository $orderRepository;
private DrinkType $drinkType;
private Order $order;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(OrderItemRepository::class);
$this->drinkTypeRepository = $this->container->get(DrinkTypeRepository::class);
$this->orderRepository = $this->container->get(OrderRepository::class);
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
// Create an order for testing
$this->order = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$this->orderRepository->save($this->order);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some order items
$orderItem1 = new OrderItem($this->drinkType, 5, $this->order);
$orderItem2 = new OrderItem($this->drinkType, 10, $this->order);
// Save them to the repository
$this->repository->save($orderItem1);
$this->repository->save($orderItem2);
// Now findAll should return both order items
$orderItems = $this->repository->findAll();
$this->assertCount(2, $orderItems);
$this->assertContainsOnlyInstancesOf(OrderItem::class, $orderItems);
}
public function testFind(): void
{
// Create an order item
$orderItem = new OrderItem($this->drinkType, 5, $this->order);
$this->repository->save($orderItem);
// Get the ID
$id = $orderItem->getId();
$this->assertNotNull($id);
// Find by ID
$foundOrderItem = $this->repository->find($id);
$this->assertInstanceOf(OrderItem::class, $foundOrderItem);
$this->assertEquals(5, $foundOrderItem->getQuantity());
$this->assertEquals($this->drinkType->getId(), $foundOrderItem->getDrinkType()->getId());
$this->assertEquals($this->order->getId(), $foundOrderItem->getOrder()->getId());
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindByOrder(): void
{
// Create another order
$anotherOrder = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-02'));
$this->orderRepository->save($anotherOrder);
// Create order items for both orders
$orderItem1 = new OrderItem($this->drinkType, 5, $this->order);
$orderItem2 = new OrderItem($this->drinkType, 10, $this->order);
$orderItem3 = new OrderItem($this->drinkType, 15, $anotherOrder);
// Save them to the repository
$this->repository->save($orderItem1);
$this->repository->save($orderItem2);
$this->repository->save($orderItem3);
// Find by order
$orderItems = $this->repository->findByOrder($this->order);
$this->assertCount(2, $orderItems);
$this->assertContainsOnlyInstancesOf(OrderItem::class, $orderItems);
// Check that the order items are for the correct order
foreach ($orderItems as $orderItem) {
$this->assertEquals($this->order->getId(), $orderItem->getOrder()->getId());
}
// Find by the other order
$otherOrderItems = $this->repository->findByOrder($anotherOrder);
$this->assertCount(1, $otherOrderItems);
$this->assertEquals($anotherOrder->getId(), $otherOrderItems[0]->getOrder()->getId());
}
public function testFindByDrinkType(): void
{
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Create order items for both drink types
$orderItem1 = new OrderItem($this->drinkType, 5, $this->order);
$orderItem2 = new OrderItem($this->drinkType, 10, $this->order);
$orderItem3 = new OrderItem($anotherDrinkType, 15, $this->order);
// Save them to the repository
$this->repository->save($orderItem1);
$this->repository->save($orderItem2);
$this->repository->save($orderItem3);
// Find by drink type
$orderItems = $this->repository->findByDrinkType($this->drinkType);
$this->assertCount(2, $orderItems);
$this->assertContainsOnlyInstancesOf(OrderItem::class, $orderItems);
// Check that the order items are for the correct drink type
foreach ($orderItems as $orderItem) {
$this->assertEquals($this->drinkType->getId(), $orderItem->getDrinkType()->getId());
}
// Find by the other drink type
$otherOrderItems = $this->repository->findByDrinkType($anotherDrinkType);
$this->assertCount(1, $otherOrderItems);
$this->assertEquals($anotherDrinkType->getId(), $otherOrderItems[0]->getDrinkType()->getId());
}
public function testFindByOrderAndDrinkType(): void
{
// Create another drink type and order
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
$anotherOrder = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-02'));
$this->orderRepository->save($anotherOrder);
// Create order items with different combinations
$orderItem1 = new OrderItem($this->drinkType, 5, $this->order);
$orderItem2 = new OrderItem($anotherDrinkType, 10, $this->order);
$orderItem3 = new OrderItem($this->drinkType, 15, $anotherOrder);
$orderItem4 = new OrderItem($anotherDrinkType, 20, $anotherOrder);
// Save them to the repository
$this->repository->save($orderItem1);
$this->repository->save($orderItem2);
$this->repository->save($orderItem3);
$this->repository->save($orderItem4);
// Find by order and drink type
$orderItem = $this->repository->findByOrderAndDrinkType($this->order, $this->drinkType);
$this->assertInstanceOf(OrderItem::class, $orderItem);
$this->assertEquals(5, $orderItem->getQuantity());
$this->assertEquals($this->order->getId(), $orderItem->getOrder()->getId());
$this->assertEquals($this->drinkType->getId(), $orderItem->getDrinkType()->getId());
// Find by another combination
$anotherOrderItem = $this->repository->findByOrderAndDrinkType($anotherOrder, $anotherDrinkType);
$this->assertInstanceOf(OrderItem::class, $anotherOrderItem);
$this->assertEquals(20, $anotherOrderItem->getQuantity());
$this->assertEquals($anotherOrder->getId(), $anotherOrderItem->getOrder()->getId());
$this->assertEquals($anotherDrinkType->getId(), $anotherOrderItem->getDrinkType()->getId());
}
public function testSave(): void
{
// Create an order item
$orderItem = new OrderItem($this->drinkType, 5, $this->order);
// Save it
$this->repository->save($orderItem);
// Check that it was saved
$id = $orderItem->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundOrderItem = $this->repository->find($id);
$this->assertInstanceOf(OrderItem::class, $foundOrderItem);
$this->assertEquals(5, $foundOrderItem->getQuantity());
// Update it
$orderItem->setQuantity(10);
$this->repository->save($orderItem);
// Find it again to confirm the update
$foundOrderItem = $this->repository->find($id);
$this->assertEquals(10, $foundOrderItem->getQuantity());
}
public function testRemove(): void
{
// Create an order item
$orderItem = new OrderItem($this->drinkType, 5, $this->order);
$this->repository->save($orderItem);
// Get the ID
$id = $orderItem->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($orderItem);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}

View file

@ -0,0 +1,223 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\Order;
use App\Entity\OrderItem;
use App\Repository\DrinkTypeRepository;
use App\Repository\OrderRepository;
use App\Repository\OrderItemRepository;
use DateTimeImmutable;
class OrderRepositoryTest extends TestCase
{
private OrderRepository $repository;
private DrinkTypeRepository $drinkTypeRepository;
private OrderItemRepository $orderItemRepository;
private DrinkType $drinkType;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(OrderRepository::class);
$this->drinkTypeRepository = $this->container->get(DrinkTypeRepository::class);
$this->orderItemRepository = $this->container->get(OrderItemRepository::class);
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some orders
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-02'));
// Save them to the repository
$this->repository->save($order1);
$this->repository->save($order2);
// Now findAll should return both orders
$orders = $this->repository->findAll();
$this->assertCount(2, $orders);
$this->assertContainsOnlyInstancesOf(Order::class, $orders);
}
public function testFind(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$this->repository->save($order);
// Get the ID
$id = $order->getId();
$this->assertNotNull($id);
// Find by ID
$foundOrder = $this->repository->find($id);
$this->assertInstanceOf(Order::class, $foundOrder);
$this->assertEquals(Order::STATUS_NEW, $foundOrder->getStatus());
$this->assertEquals('2023-01-01', $foundOrder->getCreatedAt()->format('Y-m-d'));
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindByStatus(): void
{
// Create orders with different statuses
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_IN_WORK, new DateTimeImmutable('2023-01-02'));
$order3 = new Order(Order::STATUS_ORDERED, new DateTimeImmutable('2023-01-03'));
$order4 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-04'));
$order5 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-05'));
// Save them to the repository
$this->repository->save($order1);
$this->repository->save($order2);
$this->repository->save($order3);
$this->repository->save($order4);
$this->repository->save($order5);
// Find by status
$newOrders = $this->repository->findByStatus(Order::STATUS_NEW);
$this->assertCount(2, $newOrders);
$this->assertContainsOnlyInstancesOf(Order::class, $newOrders);
// Check that the orders have the correct status
foreach ($newOrders as $order) {
$this->assertEquals(Order::STATUS_NEW, $order->getStatus());
}
// Find by another status
$fulfilledOrders = $this->repository->findByStatus(Order::STATUS_FULFILLED);
$this->assertCount(1, $fulfilledOrders);
$this->assertEquals(Order::STATUS_FULFILLED, $fulfilledOrders[0]->getStatus());
}
public function testFindByDateRange(): void
{
// Create orders with different dates
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-02'));
$order3 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-03'));
$order4 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-04'));
$order5 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-05'));
// Save them to the repository
$this->repository->save($order1);
$this->repository->save($order2);
$this->repository->save($order3);
$this->repository->save($order4);
$this->repository->save($order5);
// Find orders in a specific date range
$start = new DateTimeImmutable('2023-01-02');
$end = new DateTimeImmutable('2023-01-04');
$orders = $this->repository->findByDateRange($start, $end);
$this->assertCount(3, $orders);
// Check that the orders are within the date range
foreach ($orders as $order) {
$createdAt = $order->getCreatedAt();
$this->assertTrue($createdAt >= $start && $createdAt <= $end);
}
}
public function testFindLastOrdersForDrinkType(): 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'));
// Save them to the repository
$this->repository->save($order1);
$this->repository->save($order2);
$this->repository->save($order3);
$this->repository->save($order4);
$this->repository->save($order5);
// Create order items for the drink type
$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);
// Save them to the repository
$this->orderItemRepository->save($orderItem1);
$this->orderItemRepository->save($orderItem2);
$this->orderItemRepository->save($orderItem3);
$this->orderItemRepository->save($orderItem4);
$this->orderItemRepository->save($orderItem5);
// Find the last 3 orders for the drink type
$orders = $this->repository->findLastOrdersForDrinkType($this->drinkType, 3);
$this->assertCount(3, $orders);
// Check that the orders are the most recent ones
$this->assertEquals('2023-01-05', $orders[0]->getCreatedAt()->format('Y-m-d'));
$this->assertEquals('2023-01-04', $orders[1]->getCreatedAt()->format('Y-m-d'));
$this->assertEquals('2023-01-03', $orders[2]->getCreatedAt()->format('Y-m-d'));
}
public function testSave(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
// Save it
$this->repository->save($order);
// Check that it was saved
$id = $order->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundOrder = $this->repository->find($id);
$this->assertInstanceOf(Order::class, $foundOrder);
$this->assertEquals(Order::STATUS_NEW, $foundOrder->getStatus());
// Update it
$order->setStatus(Order::STATUS_IN_WORK);
$this->repository->save($order);
// Find it again to confirm the update
$foundOrder = $this->repository->find($id);
$this->assertEquals(Order::STATUS_IN_WORK, $foundOrder->getStatus());
}
public function testRemove(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$this->repository->save($order);
// Get the ID
$id = $order->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($order);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}

View file

@ -0,0 +1,167 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\SystemConfig;
use App\Repository\SystemConfigRepository;
class SystemConfigRepositoryTest extends TestCase
{
private SystemConfigRepository $repository;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(SystemConfigRepository::class);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some config entries
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
// Save them to the repository
$this->repository->save($config1);
$this->repository->save($config2);
// Now findAll should return both configs
$configs = $this->repository->findAll();
$this->assertCount(2, $configs);
$this->assertContainsOnlyInstancesOf(SystemConfig::class, $configs);
}
public function testFind(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get the ID
$id = $config->getId();
$this->assertNotNull($id);
// Find by ID
$foundConfig = $this->repository->find($id);
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindByKey(): void
{
// Create some configs
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
// Save them to the repository
$this->repository->save($config1);
$this->repository->save($config2);
// Find by key
$foundConfig = $this->repository->findByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Try to find a non-existent key
$this->assertNull($this->repository->findByKey('nonexistent'));
}
public function testGetValue(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get value by key
$value = $this->repository->getValue('key1');
$this->assertEquals('value1', $value);
// Get value for a non-existent key (should return default)
$value = $this->repository->getValue('nonexistent', 'default');
$this->assertEquals('default', $value);
}
public function testSetValue(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Set a value for a new key
$this->repository->setValue('key1', 'value1');
// Check that a new config was created
$configs = $this->repository->findAll();
$this->assertCount(1, $configs);
$config = $configs[0];
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('value1', $config->getValue());
// Set a value for an existing key
$this->repository->setValue('key1', 'updated');
// Check that the value was updated
$config = $this->repository->findByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $config);
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('updated', $config->getValue());
}
public function testSave(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
// Save it
$this->repository->save($config);
// Check that it was saved
$id = $config->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundConfig = $this->repository->find($id);
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Update it
$config->setValue('updated');
$this->repository->save($config);
// Find it again to confirm the update
$foundConfig = $this->repository->find($id);
$this->assertEquals('updated', $foundConfig->getValue());
}
public function testRemove(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get the ID
$id = $config->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($config);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}

View file

@ -0,0 +1,201 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Service;
use Override;
use Tests\TestCase;
use App\Entity\SystemConfig;
use App\Service\ConfigurationService;
use App\Repository\SystemConfigRepository;
use App\Enum\SystemSettingKey;
use InvalidArgumentException;
class ConfigurationServiceTest extends TestCase
{
private ConfigurationService $service;
private SystemConfigRepository $repository;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(SystemConfigRepository::class);
$this->service = $this->container->get(ConfigurationService::class);
}
public function testGetAllConfigs(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllConfigs());
// Create some config entries
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
$this->repository->save($config1);
$this->repository->save($config2);
// Now getAllConfigs should return both configs
$configs = $this->service->getAllConfigs();
$this->assertCount(2, $configs);
$this->assertContainsOnlyInstancesOf(SystemConfig::class, $configs);
}
public function testGetConfigValue(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get value by key
$value = $this->service->getConfigValue('key1');
$this->assertEquals('value1', $value);
// Get value for a non-existent key (should return default)
$value = $this->service->getConfigValue('nonexistent', 'default');
$this->assertEquals('default', $value);
}
public function testSetConfigValue(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllConfigs());
// Set a value for a new key
$this->service->setConfigValue('key1', 'value1');
// Check that a new config was created
$configs = $this->service->getAllConfigs();
$this->assertCount(1, $configs);
$config = $configs[0];
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('value1', $config->getValue());
// Set a value for an existing key
$this->service->setConfigValue('key1', 'updated');
// Check that the value was updated
$config = $this->service->getConfigByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $config);
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('updated', $config->getValue());
}
public function testGetConfigByKey(): void
{
// Create some configs
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
$this->repository->save($config1);
$this->repository->save($config2);
// Find by key
$foundConfig = $this->service->getConfigByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Try to find a non-existent key
$this->assertNull($this->service->getConfigByKey('nonexistent'));
}
public function testCreateConfig(): void
{
// Create a new config
$config = $this->service->createConfig('key1', 'value1');
// Check that it was created correctly
$this->assertInstanceOf(SystemConfig::class, $config);
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('value1', $config->getValue());
// Check that it's in the database
$foundConfig = $this->service->getConfigByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Try to create a config with the same key (should throw an exception)
$this->expectException(InvalidArgumentException::class);
$this->service->createConfig('key1', 'value2');
}
public function testUpdateConfig(): void
{
// Create a config
$config = $this->service->createConfig('key1', 'value1');
// Update the value
$updatedConfig = $this->service->updateConfig($config, null, 'updated');
$this->assertEquals('key1', $updatedConfig->getKey());
$this->assertEquals('updated', $updatedConfig->getValue());
// Check that it was updated in the database
$foundConfig = $this->service->getConfigByKey('key1');
$this->assertEquals('updated', $foundConfig->getValue());
// Update the key
$updatedConfig = $this->service->updateConfig($config, 'newKey', null);
$this->assertEquals('newKey', $updatedConfig->getKey());
$this->assertEquals('updated', $updatedConfig->getValue());
// Check that it was updated in the database
$foundConfig = $this->service->getConfigByKey('newKey');
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('newKey', $foundConfig->getKey());
$this->assertEquals('updated', $foundConfig->getValue());
// Create another config
$config2 = $this->service->createConfig('key2', 'value2');
// Try to update the key to an existing key (should throw an exception)
$this->expectException(InvalidArgumentException::class);
$this->service->updateConfig($config2, 'newKey', null);
}
public function testDeleteConfig(): void
{
// Create a config
$config = $this->service->createConfig('key1', 'value1');
// Check that it exists
$this->assertInstanceOf(SystemConfig::class, $this->service->getConfigByKey('key1'));
// Delete it
$this->service->deleteConfig($config);
// Check that it's gone
$this->assertNull($this->service->getConfigByKey('key1'));
}
public function testInitializeDefaultConfigs(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllConfigs());
// Initialize default configs
$this->service->initializeDefaultConfigs();
// Check that the default configs were created
$configs = $this->service->getAllConfigs();
$this->assertGreaterThanOrEqual(3, count($configs)); // At least 3 default configs
// Check specific default configs
$lookbackOrders = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK_ORDERS->value);
$this->assertEquals('5', $lookbackOrders);
$magnitude = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_MAGNITUDE->value);
$this->assertEquals('0.2', $magnitude);
$threshold = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_THRESHOLD->value);
$this->assertEquals('0.1', $threshold);
// Initialize again (should not create duplicates)
$this->service->initializeDefaultConfigs();
// Count should be the same
$this->assertCount(count($configs), $this->service->getAllConfigs());
}
}

View file

@ -0,0 +1,169 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Service;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Service\DrinkTypeService;
use App\Repository\DrinkTypeRepository;
use InvalidArgumentException;
class DrinkTypeServiceTest extends TestCase
{
private DrinkTypeService $service;
private DrinkTypeRepository $repository;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(DrinkTypeRepository::class);
$this->service = $this->container->get(DrinkTypeService::class);
}
public function testGetAllDrinkTypes(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllDrinkTypes());
// Create some drink types
$drinkType1 = new DrinkType('Cola', 'Refreshing cola drink', 10);
$drinkType2 = new DrinkType('Fanta', 'Orange soda', 5);
$this->repository->save($drinkType1);
$this->repository->save($drinkType2);
// Now getAllDrinkTypes should return both drink types
$drinkTypes = $this->service->getAllDrinkTypes();
$this->assertCount(2, $drinkTypes);
$this->assertContainsOnlyInstancesOf(DrinkType::class, $drinkTypes);
}
public function testGetDrinkTypeById(): void
{
// Create a drink type
$drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->repository->save($drinkType);
// Get the ID
$id = $drinkType->getId();
$this->assertNotNull($id);
// Find by ID
$foundDrinkType = $this->service->getDrinkTypeById($id);
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
$this->assertEquals('Refreshing cola drink', $foundDrinkType->getDescription());
$this->assertEquals(10, $foundDrinkType->getDesiredStock());
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->service->getDrinkTypeById($nonExistentId));
}
public function testGetDrinkTypeByName(): void
{
// Create some drink types
$drinkType1 = new DrinkType('Cola', 'Refreshing cola drink', 10);
$drinkType2 = new DrinkType('Fanta', 'Orange soda', 5);
$this->repository->save($drinkType1);
$this->repository->save($drinkType2);
// Find by name
$foundDrinkType = $this->service->getDrinkTypeByName('Cola');
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
$this->assertEquals('Refreshing cola drink', $foundDrinkType->getDescription());
$this->assertEquals(10, $foundDrinkType->getDesiredStock());
// Try to find a non-existent name
$this->assertNull($this->service->getDrinkTypeByName('nonexistent'));
}
public function testCreateDrinkType(): void
{
// Create a new drink type
$drinkType = $this->service->createDrinkType('Cola', 'Refreshing cola drink', 10);
// Check that it was created correctly
$this->assertInstanceOf(DrinkType::class, $drinkType);
$this->assertEquals('Cola', $drinkType->getName());
$this->assertEquals('Refreshing cola drink', $drinkType->getDescription());
$this->assertEquals(10, $drinkType->getDesiredStock());
// Check that it's in the database
$foundDrinkType = $this->service->getDrinkTypeByName('Cola');
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Cola', $foundDrinkType->getName());
$this->assertEquals('Refreshing cola drink', $foundDrinkType->getDescription());
$this->assertEquals(10, $foundDrinkType->getDesiredStock());
// Try to create a drink type with the same name (should throw an exception)
$this->expectException(InvalidArgumentException::class);
$this->service->createDrinkType('Cola', 'Another cola drink', 5);
}
public function testUpdateDrinkType(): void
{
// Create a drink type
$drinkType = $this->service->createDrinkType('Cola', 'Refreshing cola drink', 10);
// Update the description
$updatedDrinkType = $this->service->updateDrinkType($drinkType, null, 'Updated description', null);
$this->assertEquals('Cola', $updatedDrinkType->getName());
$this->assertEquals('Updated description', $updatedDrinkType->getDescription());
$this->assertEquals(10, $updatedDrinkType->getDesiredStock());
// Check that it was updated in the database
$foundDrinkType = $this->service->getDrinkTypeByName('Cola');
$this->assertEquals('Updated description', $foundDrinkType->getDescription());
// Update the desired stock
$updatedDrinkType = $this->service->updateDrinkType($drinkType, null, null, 20);
$this->assertEquals('Cola', $updatedDrinkType->getName());
$this->assertEquals('Updated description', $updatedDrinkType->getDescription());
$this->assertEquals(20, $updatedDrinkType->getDesiredStock());
// Check that it was updated in the database
$foundDrinkType = $this->service->getDrinkTypeByName('Cola');
$this->assertEquals(20, $foundDrinkType->getDesiredStock());
// Update the name
$updatedDrinkType = $this->service->updateDrinkType($drinkType, 'Pepsi', null, null);
$this->assertEquals('Pepsi', $updatedDrinkType->getName());
$this->assertEquals('Updated description', $updatedDrinkType->getDescription());
$this->assertEquals(20, $updatedDrinkType->getDesiredStock());
// Check that it was updated in the database
$foundDrinkType = $this->service->getDrinkTypeByName('Pepsi');
$this->assertInstanceOf(DrinkType::class, $foundDrinkType);
$this->assertEquals('Pepsi', $foundDrinkType->getName());
$this->assertEquals('Updated description', $foundDrinkType->getDescription());
$this->assertEquals(20, $foundDrinkType->getDesiredStock());
// Create another drink type
$drinkType2 = $this->service->createDrinkType('Fanta', 'Orange soda', 5);
// Try to update the name to an existing name (should throw an exception)
$this->expectException(InvalidArgumentException::class);
$this->service->updateDrinkType($drinkType2, 'Pepsi', null, null);
}
public function testDeleteDrinkType(): void
{
// Create a drink type
$drinkType = $this->service->createDrinkType('Cola', 'Refreshing cola drink', 10);
// Check that it exists
$this->assertInstanceOf(DrinkType::class, $this->service->getDrinkTypeByName('Cola'));
// Delete it
$this->service->deleteDrinkType($drinkType);
// Check that it's gone
$this->assertNull($this->service->getDrinkTypeByName('Cola'));
}
}

View file

@ -0,0 +1,251 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Service;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\InventoryRecord;
use App\Service\InventoryService;
use App\Service\ConfigurationService;
use App\Enum\SystemSettingKey;
use App\Repository\DrinkTypeRepository;
use App\Repository\InventoryRecordRepository;
use DateTimeImmutable;
class InventoryServiceTest extends TestCase
{
private InventoryService $service;
private DrinkTypeRepository $drinkTypeRepository;
private InventoryRecordRepository $inventoryRecordRepository;
private ConfigurationService $configService;
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->configService = $this->container->get(ConfigurationService::class);
$this->service = $this->container->get(InventoryService::class);
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
}
public function testGetAllInventoryRecords(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllInventoryRecords());
// Create some inventory records
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
// Now getAllInventoryRecords should return both records
$records = $this->service->getAllInventoryRecords();
$this->assertCount(2, $records);
$this->assertContainsOnlyInstancesOf(InventoryRecord::class, $records);
}
public function testGetInventoryRecordsByDrinkType(): void
{
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Create inventory records for both drink types
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($anotherDrinkType, 15, new DateTimeImmutable('2023-01-03'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
// Get records for the first drink type
$records = $this->service->getInventoryRecordsByDrinkType($this->drinkType);
$this->assertCount(2, $records);
$this->assertContainsOnlyInstancesOf(InventoryRecord::class, $records);
// Check that the records are for the correct drink type
foreach ($records as $record) {
$this->assertEquals($this->drinkType->getId(), $record->getDrinkType()->getId());
}
// Get records for the second drink type
$records = $this->service->getInventoryRecordsByDrinkType($anotherDrinkType);
$this->assertCount(1, $records);
$this->assertEquals($anotherDrinkType->getId(), $records[0]->getDrinkType()->getId());
}
public function testGetLatestInventoryRecord(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-03'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
// Get the latest record
$latestRecord = $this->service->getLatestInventoryRecord($this->drinkType);
$this->assertInstanceOf(InventoryRecord::class, $latestRecord);
$this->assertEquals(15, $latestRecord->getQuantity());
$this->assertEquals('2023-01-03', $latestRecord->getTimestamp()->format('Y-m-d'));
}
public function testGetInventoryRecordsByTimeRange(): void
{
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-03'));
$record4 = new InventoryRecord($this->drinkType, 20, new DateTimeImmutable('2023-01-04'));
$record5 = new InventoryRecord($this->drinkType, 25, new DateTimeImmutable('2023-01-05'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
$this->inventoryRecordRepository->save($record4);
$this->inventoryRecordRepository->save($record5);
// Get records in a specific range
$start = new DateTimeImmutable('2023-01-02');
$end = new DateTimeImmutable('2023-01-04');
$records = $this->service->getInventoryRecordsByTimeRange($start, $end);
$this->assertCount(3, $records);
// Check that the records are within the range
foreach ($records as $record) {
$timestamp = $record->getTimestamp();
$this->assertTrue($timestamp >= $start && $timestamp <= $end);
}
}
public function testGetCurrentStockLevel(): void
{
// Initially there should be no stock
$this->assertEquals(0, $this->service->getCurrentStockLevel($this->drinkType));
// Create inventory records with different timestamps
$record1 = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($this->drinkType, 10, new DateTimeImmutable('2023-01-02'));
$record3 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-03'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
$this->inventoryRecordRepository->save($record3);
// The current stock level should be from the latest record
$this->assertEquals(15, $this->service->getCurrentStockLevel($this->drinkType));
}
public function testGetStockDifference(): void
{
// Create inventory records
$record = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-01'));
$this->inventoryRecordRepository->save($record);
// The drink type has a desired stock of 10, and a current stock of 15
// So the difference should be 5 (excess)
$this->assertEquals(5, $this->service->getStockDifference($this->drinkType));
// Create a new record with less stock
$record = new InventoryRecord($this->drinkType, 5, new DateTimeImmutable('2023-01-02'));
$this->inventoryRecordRepository->save($record);
// Now the current stock is 5, and the desired stock is 10
// So the difference should be -5 (shortage)
$this->assertEquals(-5, $this->service->getStockDifference($this->drinkType));
}
public function testUpdateStockLevel(): void
{
// Update the stock level with a specific timestamp
$timestamp1 = new DateTimeImmutable('2023-01-01 10:00:00');
$record = $this->service->updateStockLevel($this->drinkType, 15, $timestamp1);
// Check that the record was created correctly
$this->assertInstanceOf(InventoryRecord::class, $record);
$this->assertEquals($this->drinkType->getId(), $record->getDrinkType()->getId());
$this->assertEquals(15, $record->getQuantity());
$this->assertEquals($timestamp1, $record->getTimestamp());
// Check that the current stock level is updated
$this->assertEquals(15, $this->service->getCurrentStockLevel($this->drinkType));
// Update the stock level again with a later timestamp
$timestamp2 = new DateTimeImmutable('2023-01-02 10:00:00');
$record = $this->service->updateStockLevel($this->drinkType, 20, $timestamp2);
// Check that the record was created correctly
$this->assertEquals(20, $record->getQuantity());
$this->assertEquals($timestamp2, $record->getTimestamp());
// Check that the current stock level is updated to the latest record's quantity
$this->assertEquals(20, $this->service->getCurrentStockLevel($this->drinkType));
}
public function testGetAllDrinkTypesWithStockLevels(): void
{
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Create inventory records
$record1 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($anotherDrinkType, 3, new DateTimeImmutable('2023-01-01'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
// Get all drink types with stock levels
$result = $this->service->getAllDrinkTypesWithStockLevels();
$this->assertCount(2, $result);
// Check the first drink type
$this->assertEquals($this->drinkType->getId(), $result[0]['drinkType']->getId());
$this->assertEquals(15, $result[0]['currentStock']);
$this->assertEquals(10, $result[0]['desiredStock']);
$this->assertEquals(5, $result[0]['difference']);
// Check the second drink type
$this->assertEquals($anotherDrinkType->getId(), $result[1]['drinkType']->getId());
$this->assertEquals(3, $result[1]['currentStock']);
$this->assertEquals(5, $result[1]['desiredStock']);
$this->assertEquals(-2, $result[1]['difference']);
}
public function testGetLowStockDrinkTypes(): void
{
// Set the low stock threshold to 70% so that 3/5 (60%) is considered low stock
$this->configService->setConfigValue(SystemSettingKey::LOW_STOCK_THRESHOLD->value, '70');
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Create inventory records
$record1 = new InventoryRecord($this->drinkType, 15, new DateTimeImmutable('2023-01-01'));
$record2 = new InventoryRecord($anotherDrinkType, 3, new DateTimeImmutable('2023-01-01'));
$this->inventoryRecordRepository->save($record1);
$this->inventoryRecordRepository->save($record2);
// Get low stock drink types
$result = $this->service->getLowStockDrinkTypes();
// Only the second drink type has low stock
$this->assertCount(1, $result);
$this->assertEquals($anotherDrinkType->getId(), $result[0]['drinkType']->getId());
$this->assertEquals(3, $result[0]['currentStock']);
$this->assertEquals(5, $result[0]['desiredStock']);
$this->assertEquals(2, $result[0]['shortage']);
}
}

View file

@ -0,0 +1,374 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Service;
use Override;
use Tests\TestCase;
use App\Entity\DrinkType;
use App\Entity\Order;
use App\Entity\OrderItem;
use App\Service\OrderService;
use App\Repository\DrinkTypeRepository;
use App\Repository\OrderRepository;
use App\Repository\OrderItemRepository;
use App\Service\InventoryService;
use App\Service\ConfigurationService;
use App\Enum\SystemSettingKey;
use DateTimeImmutable;
use InvalidArgumentException;
class OrderServiceTest extends TestCase
{
private OrderService $service;
private OrderRepository $orderRepository;
private OrderItemRepository $orderItemRepository;
private DrinkTypeRepository $drinkTypeRepository;
private InventoryService $inventoryService;
private ConfigurationService $configService;
private DrinkType $drinkType;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->orderRepository = $this->container->get(OrderRepository::class);
$this->orderItemRepository = $this->container->get(OrderItemRepository::class);
$this->drinkTypeRepository = $this->container->get(DrinkTypeRepository::class);
$this->inventoryService = $this->container->get(InventoryService::class);
$this->configService = $this->container->get(ConfigurationService::class);
$this->service = $this->container->get(OrderService::class);
// Create a drink type for testing
$this->drinkType = new DrinkType('Cola', 'Refreshing cola drink', 10);
$this->drinkTypeRepository->save($this->drinkType);
}
public function testGetAllOrders(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->service->getAllOrders());
// Create some orders
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-02'));
$this->orderRepository->save($order1);
$this->orderRepository->save($order2);
// Now getAllOrders should return both orders
$orders = $this->service->getAllOrders();
$this->assertCount(2, $orders);
$this->assertContainsOnlyInstancesOf(Order::class, $orders);
}
public function testGetOrderById(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$this->orderRepository->save($order);
// Get the ID
$id = $order->getId();
$this->assertNotNull($id);
// Find by ID
$foundOrder = $this->service->getOrderById($id);
$this->assertInstanceOf(Order::class, $foundOrder);
$this->assertEquals(Order::STATUS_NEW, $foundOrder->getStatus());
$this->assertEquals('2023-01-01', $foundOrder->getCreatedAt()->format('Y-m-d'));
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->service->getOrderById($nonExistentId));
}
public function testGetOrdersByStatus(): void
{
// Create orders with different statuses
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_IN_WORK, new DateTimeImmutable('2023-01-02'));
$order3 = new Order(Order::STATUS_ORDERED, new DateTimeImmutable('2023-01-03'));
$order4 = new Order(Order::STATUS_FULFILLED, new DateTimeImmutable('2023-01-04'));
$order5 = new Order(Order::STATUS_NEW, 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);
// Get orders by status
$newOrders = $this->service->getOrdersByStatus(Order::STATUS_NEW);
$this->assertCount(2, $newOrders);
$this->assertContainsOnlyInstancesOf(Order::class, $newOrders);
// Check that the orders have the correct status
foreach ($newOrders as $order) {
$this->assertEquals(Order::STATUS_NEW, $order->getStatus());
}
// Get orders by another status
$fulfilledOrders = $this->service->getOrdersByStatus(Order::STATUS_FULFILLED);
$this->assertCount(1, $fulfilledOrders);
$this->assertEquals(Order::STATUS_FULFILLED, $fulfilledOrders[0]->getStatus());
}
public function testGetOrdersByDateRange(): void
{
// Create orders with different dates
$order1 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-01'));
$order2 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-02'));
$order3 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-03'));
$order4 = new Order(Order::STATUS_NEW, new DateTimeImmutable('2023-01-04'));
$order5 = new Order(Order::STATUS_NEW, 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);
// Get orders in a specific date range
$start = new DateTimeImmutable('2023-01-02');
$end = new DateTimeImmutable('2023-01-04');
$orders = $this->service->getOrdersByDateRange($start, $end);
$this->assertCount(3, $orders);
// Check that the orders are within the date range
foreach ($orders as $order) {
$createdAt = $order->getCreatedAt();
$this->assertTrue($createdAt >= $start && $createdAt <= $end);
}
}
public function testCreateOrder(): void
{
// Create an order with items
$items = [
['drinkTypeId' => $this->drinkType->getId(), 'quantity' => 5],
];
$order = $this->service->createOrder($items);
// Check that the order was created correctly
$this->assertInstanceOf(Order::class, $order);
$this->assertEquals(Order::STATUS_NEW, $order->getStatus());
// Refresh the order from the database
$refreshedOrder = $this->service->getOrderById($order->getId());
$this->assertInstanceOf(Order::class, $refreshedOrder);
// Check that the order items were created correctly
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(1, $orderItems);
$orderItem = $orderItems->first();
$this->assertEquals($this->drinkType->getId(), $orderItem->getDrinkType()->getId());
$this->assertEquals(5, $orderItem->getQuantity());
// Try to create an order with an invalid drink type ID
$this->expectException(InvalidArgumentException::class);
$this->service->createOrder([['drinkTypeId' => 9999, 'quantity' => 5]]);
}
public function testCreateOrderFromStockLevels(): void
{
// Set the low stock threshold to 70% so that 3/5 (60%) is considered low stock
$this->configService->setConfigValue(SystemSettingKey::LOW_STOCK_THRESHOLD->value, '70');
// Create a drink type with low stock
$drinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($drinkType);
// Create inventory records
$this->inventoryService->updateStockLevel($this->drinkType, 15); // Excess stock
$this->inventoryService->updateStockLevel($drinkType, 3); // Low stock
// Create an order from stock levels
$order = $this->service->createOrderFromStockLevels();
// Check that the order was created correctly
$this->assertInstanceOf(Order::class, $order);
$this->assertEquals(Order::STATUS_NEW, $order->getStatus());
// Refresh the order from the database
$refreshedOrder = $this->service->getOrderById($order->getId());
$this->assertInstanceOf(Order::class, $refreshedOrder);
// Check that the order items were created correctly
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(1, $orderItems); // Only one drink type has low stock
$orderItem = $orderItems->first();
$this->assertEquals($drinkType->getId(), $orderItem->getDrinkType()->getId());
$this->assertEquals(2, $orderItem->getQuantity()); // Shortage is 2
}
public function testUpdateOrderStatus(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW);
$this->orderRepository->save($order);
// Update the status
$updatedOrder = $this->service->updateOrderStatus($order, Order::STATUS_IN_WORK);
$this->assertEquals(Order::STATUS_IN_WORK, $updatedOrder->getStatus());
// Check that it was updated in the database
$foundOrder = $this->service->getOrderById($order->getId());
$this->assertEquals(Order::STATUS_IN_WORK, $foundOrder->getStatus());
// Update to fulfilled status
$orderItem = new OrderItem($this->drinkType, 5, $foundOrder);
$this->orderItemRepository->save($orderItem);
// Refresh the order from the database
$refreshedOrder = $this->service->getOrderById($foundOrder->getId());
$this->assertInstanceOf(Order::class, $refreshedOrder);
$this->assertCount(1, $refreshedOrder->getOrderItems());
// Create an initial inventory record with a specific timestamp
$timestamp1 = new DateTimeImmutable('2023-01-01 10:00:00');
$this->inventoryService->updateStockLevel($this->drinkType, 10, $timestamp1);
// Update the status to fulfilled
$updatedOrder = $this->service->updateOrderStatus($refreshedOrder, Order::STATUS_FULFILLED);
$this->assertEquals(Order::STATUS_FULFILLED, $updatedOrder->getStatus());
// Check that the inventory was updated with a new record that has a later timestamp
$currentStock = $this->inventoryService->getCurrentStockLevel($this->drinkType);
$this->assertEquals(15, $currentStock); // 10 + 5 = 15
// Try to update to an invalid status
$this->expectException(InvalidArgumentException::class);
$this->service->updateOrderStatus($updatedOrder, 'invalid_status');
}
public function testAddOrderItem(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW);
$this->orderRepository->save($order);
// Add an item
$orderItem = $this->service->addOrderItem($order, $this->drinkType, 5);
// Check that the item was added correctly
$this->assertInstanceOf(OrderItem::class, $orderItem);
$this->assertEquals($this->drinkType->getId(), $orderItem->getDrinkType()->getId());
$this->assertEquals(5, $orderItem->getQuantity());
$this->assertEquals($order->getId(), $orderItem->getOrder()->getId());
// Refresh the order from the database
$refreshedOrder = $this->service->getOrderById($order->getId());
$this->assertInstanceOf(Order::class, $refreshedOrder);
// Check that the item is in the order
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(1, $orderItems);
$this->assertEquals($orderItem->getId(), $orderItems->first()->getId());
// Add another item for the same drink type
$orderItem2 = $this->service->addOrderItem($refreshedOrder, $this->drinkType, 3);
// Check that the quantity was updated
$this->assertEquals(8, $orderItem2->getQuantity()); // 5 + 3 = 8
// Refresh the order again
$refreshedOrder = $this->service->getOrderById($refreshedOrder->getId());
// Check that there's still only one item in the order
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(1, $orderItems);
// Create another drink type
$anotherDrinkType = new DrinkType('Fanta', 'Orange soda', 5);
$this->drinkTypeRepository->save($anotherDrinkType);
// Add an item for the new drink type
$orderItem3 = $this->service->addOrderItem($refreshedOrder, $anotherDrinkType, 2);
// Check that the item was added correctly
$this->assertInstanceOf(OrderItem::class, $orderItem3);
$this->assertEquals($anotherDrinkType->getId(), $orderItem3->getDrinkType()->getId());
$this->assertEquals(2, $orderItem3->getQuantity());
// Refresh the order again
$refreshedOrder = $this->service->getOrderById($refreshedOrder->getId());
// Check that there are now two items in the order
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(2, $orderItems);
// Try to add an item to an order that's not in 'new' or 'in_work' status
$refreshedOrder->setStatus(Order::STATUS_ORDERED);
$this->orderRepository->save($refreshedOrder);
$this->expectException(InvalidArgumentException::class);
$this->service->addOrderItem($refreshedOrder, $this->drinkType, 1);
}
public function testRemoveOrderItem(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW);
$this->orderRepository->save($order);
// Add an item
$orderItem = $this->service->addOrderItem($order, $this->drinkType, 5);
// Refresh the order from the database
$refreshedOrder = $this->service->getOrderById($order->getId());
$this->assertInstanceOf(Order::class, $refreshedOrder);
// Check that the item is in the order
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(1, $orderItems);
$this->assertEquals($orderItem->getId(), $orderItems->first()->getId());
// Remove the item
$this->service->removeOrderItem($refreshedOrder, $orderItem);
// Refresh the order again
$refreshedOrder = $this->service->getOrderById($refreshedOrder->getId());
// Check that the item is gone
$orderItems = $refreshedOrder->getOrderItems();
$this->assertCount(0, $orderItems);
// Try to remove an item from an order that's not in 'new' or 'in_work' status
$refreshedOrder->setStatus(Order::STATUS_ORDERED);
$this->orderRepository->save($refreshedOrder);
$orderItem = new OrderItem($this->drinkType, 5, $refreshedOrder);
$this->orderItemRepository->save($orderItem);
$this->expectException(InvalidArgumentException::class);
$this->service->removeOrderItem($refreshedOrder, $orderItem);
}
public function testDeleteOrder(): void
{
// Create an order
$order = new Order(Order::STATUS_NEW);
$this->orderRepository->save($order);
// Get the ID
$id = $order->getId();
$this->assertNotNull($id);
// Check that it exists
$this->assertInstanceOf(Order::class, $this->service->getOrderById($id));
// Delete it
$this->service->deleteOrder($order);
// Check that it's gone
$this->assertNull($this->service->getOrderById($id));
// Try to delete an order that's not in 'new' status
$order = new Order(Order::STATUS_IN_WORK);
$this->orderRepository->save($order);
$this->expectException(InvalidArgumentException::class);
$this->service->deleteOrder($order);
}
}

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']);
}
}

7
tests/Pest.php Normal file
View file

@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
use Tests\TestCase;
pest()->extend(TestCase::class)->in('Feature');

35
tests/TestCase.php Normal file
View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Tests;
use App\Settings;
use DI\Bridge\Slim\Bridge;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Psr\Container\ContainerInterface;
use Slim\App;
abstract class TestCase extends BaseTestCase
{
public ContainerInterface $container;
public App $app;
public function setUp(): void
{
$this->container = (require __DIR__ . '/../config/container.php')(new Settings(true));
$this->app = Bridge::create($this->container);
}
public function setUpDB(): void
{
$entityManager = $this->container->get(EntityManagerInterface::class);
// Create schema from entities
$metadata = $entityManager->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($entityManager);
$tool->dropSchema($metadata);
$tool->createSchema($metadata);
}
}