saufen/tests/Feature/Repository/OrderItemRepositoryTest.php
2025-05-31 21:43:13 +02:00

226 lines
8.6 KiB
PHP

<?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));
}
}