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