entityManager = $this->createMock(EntityManagerInterface::class); // Create a PHPUnit mock for the Security component $this->security = $this->createMock(Security::class); // Initialize the OrderManager with the mocked dependencies $this->orderManager = new OrderManager($this->entityManager, $this->security); } public function testFindOrCreateOpenOrderItemCreatesNewOrderItem(): void { // Step 1: Mock the Inventory and its related Item $item = new Item(); $inventory = $this->createMock(Inventory::class); $inventory->method('getItem')->willReturn($item); // Step 2: Mock the OrderItem repository $orderItemRepository = $this->createMock(EntityRepository::class); $orderItemRepository->method('findOneBy')->willReturn(null); // Simulate no existing order item // Step 3: Mock getRepository() in EntityManager to return the mocked repository $this->entityManager ->method('getRepository') ->with(OrderItems::class) ->willReturn($orderItemRepository); // Step 4: Call the method under test $orderItem = $this->orderManager->findOrCreateOpenOrderItem($inventory); // Step 5: Assert the returned OrderItem has the correct properties $this->assertInstanceOf(OrderItems::class, $orderItem); $this->assertSame($item, $orderItem->getItem()); $this->assertTrue($orderItem->isIsOpenStock()); } public function testFindOrCreateOpenOrderItemFindsExistingOrderItem(): void { // Step 1: Mock the Inventory and its related Item $item = new Item(); $inventory = $this->createMock(Inventory::class); $inventory->method('getItem')->willReturn($item); // Step 2: Mock an existing OrderItem $existingOrderItem = new OrderItems(); $existingOrderItem->setItem($item); $existingOrderItem->setIsOpenStock(true); // Step 3: Mock the OrderItem repository to return the existing OrderItem $orderItemRepository = $this->createMock(EntityRepository::class); $orderItemRepository->method('findOneBy')->willReturn($existingOrderItem); // Step 4: Mock getRepository() in EntityManager to return the mocked repository $this->entityManager ->method('getRepository') ->with(OrderItems::class) ->willReturn($orderItemRepository); // Step 5: Call the method under test $orderItem = $this->orderManager->findOrCreateOpenOrderItem($inventory); // Step 6: Assert the returned OrderItem is the existing one $this->assertSame($existingOrderItem, $orderItem); } }