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