From bb24b37b2843f9a68379955900d8d912064c5a10 Mon Sep 17 00:00:00 2001 From: Alexander Veselov Date: Wed, 20 Mar 2024 11:27:22 +0200 Subject: [PATCH] FRW-1817 Adjusted Dynamic Multistore Test Coverage (#10800) FRW-1817 Adjusted Dynamic Multistore Test Coverage --- .../Zed/Oms/Business/Mail/MailHandlerTest.php | 115 ++++++++++++++ .../Zed/Oms/Business/Util/ReservationTest.php | 145 ++++++++++++++++++ .../Zed/Oms/_support/OmsBusinessTester.php | 31 +++- 3 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 tests/SprykerTest/Zed/Oms/Business/Mail/MailHandlerTest.php create mode 100644 tests/SprykerTest/Zed/Oms/Business/Util/ReservationTest.php diff --git a/tests/SprykerTest/Zed/Oms/Business/Mail/MailHandlerTest.php b/tests/SprykerTest/Zed/Oms/Business/Mail/MailHandlerTest.php new file mode 100644 index 00000000..534c86cb --- /dev/null +++ b/tests/SprykerTest/Zed/Oms/Business/Mail/MailHandlerTest.php @@ -0,0 +1,115 @@ +tester->configureTestStateMachine([$this->tester::DEFAULT_OMS_PROCESS_NAME]); + } + + /** + * @dataProvider getMailTypes + * + * @param string $mailType + * + * @return void + */ + public function testSendOrderMailExpandsMailTransferWithStore(string $mailType): void + { + // Arrange + $orderTransfer = $this->tester->haveOrder([], $this->tester::DEFAULT_OMS_PROCESS_NAME); + $orderTransfer = $this->tester->getOrderByIdSalesOrder($orderTransfer->getIdSalesOrder()); + $salesOrderEntity = (new SpySalesOrder()) + ->fromArray($orderTransfer->toArray()) + ->setBillingAddress((new SpySalesOrderAddress())->setAddress1($this->tester::FAKE_BILLING_ADDRESS)); + + // Assert + $mailFacadeMock = $this->createMailFacadeMock(); + $mailFacadeMock + ->expects($this->once()) + ->method('handleMail') + ->with($this->callback(function (MailTransfer $mailTransfer) use ($salesOrderEntity) { + return $mailTransfer->getStoreName() === $salesOrderEntity->getStore(); + })); + + // Act + $mailHandler = new MailHandler( + $this->createSalesFacadeMock($orderTransfer), + $mailFacadeMock, + [], + ); + $mailHandler->{$mailType}($salesOrderEntity); + } + + /** + * @return array + */ + protected function getMailTypes(): array + { + return [ + ['sendOrderShippedMail'], + ['sendOrderConfirmationMail'], + ]; + } + + /** + * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer + * + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Dependency\Facade\OmsToSalesInterface + */ + protected function createSalesFacadeMock(OrderTransfer $orderTransfer): OmsToSalesInterface + { + $salesFacadeMock = $this->createMock(OmsToSalesInterface::class); + $salesFacadeMock->expects($this->once()) + ->method('getOrderByIdSalesOrder') + ->willReturn($orderTransfer); + + return $salesFacadeMock; + } + + /** + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Dependency\Facade\OmsToMailInterface + */ + protected function createMailFacadeMock(): OmsToMailInterface + { + return $this->createMock(OmsToMailInterface::class); + } +} diff --git a/tests/SprykerTest/Zed/Oms/Business/Util/ReservationTest.php b/tests/SprykerTest/Zed/Oms/Business/Util/ReservationTest.php new file mode 100644 index 00000000..65c55862 --- /dev/null +++ b/tests/SprykerTest/Zed/Oms/Business/Util/ReservationTest.php @@ -0,0 +1,145 @@ +createReservationReaderMock(); + $storeTransfers = $this->tester->getLocator()->store()->facade()->getAllStores(); + + // Assert + $reservationReaderMock->expects($this->once()) + ->method('sumReservedProductQuantitiesForSku') + ->willReturn(new Decimal(1)); + + // Act + $reservation = new Reservation( + $reservationReaderMock, + [], + $this->createStoreFacadeMock($storeTransfers), + $this->createOmsRepositoryMock(count($storeTransfers)), + $this->createOmsEntityManagerMock(count($storeTransfers)), + [], + [], + ); + $reservation->updateReservationQuantity($this->tester::FAKE_SKU); + } + + /** + * @return void + */ + public function testUpdateReservationGetsAllStores(): void + { + // Arrange + $reservationRequestTransfer = new ReservationRequestTransfer(); + $reservationReaderMock = $this->createReservationReaderMock(); + $storeTransfers = $this->tester->getLocator()->store()->facade()->getAllStores(); + + // Assert + $reservationReaderMock->expects($this->exactly(count($storeTransfers))) + ->method('sumReservedProductQuantities') + ->willReturn(new Decimal(1)); + + // Act + $reservation = new Reservation( + $reservationReaderMock, + [], + $this->createStoreFacadeMock($storeTransfers), + $this->createOmsRepositoryMock(count($storeTransfers)), + $this->createOmsEntityManagerMock(count($storeTransfers)), + [], + [], + ); + $reservation->updateReservation($reservationRequestTransfer); + } + + /** + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Business\Reader\ReservationReaderInterface + */ + protected function createReservationReaderMock(): ReservationReaderInterface + { + return $this->createMock(ReservationReaderInterface::class); + } + + /** + * @param array<\Generated\Shared\Transfer\StoreTransfer> $storeTransfers + * + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Dependency\Facade\OmsToStoreFacadeInterface + */ + protected function createStoreFacadeMock(array $storeTransfers): OmsToStoreFacadeInterface + { + $storeFacadeMock = $this->createMock(OmsToStoreFacadeInterface::class); + $storeFacadeMock->expects($this->once()) + ->method('getAllStores') + ->willReturn($storeTransfers); + + return $storeFacadeMock; + } + + /** + * @param int $storesCount + * + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Persistence\OmsRepositoryInterface + */ + protected function createOmsRepositoryMock(int $storesCount): OmsRepositoryInterface + { + $omsRepositoryMock = $this->createMock(OmsRepositoryInterface::class); + $omsRepositoryMock->expects($this->exactly($storesCount)) + ->method('findProductReservation') + ->willReturn(new OmsProductReservationTransfer()); + + return $omsRepositoryMock; + } + + /** + * @param int $storesCount + * + * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\Oms\Persistence\OmsEntityManagerInterface + */ + protected function createOmsEntityManagerMock(int $storesCount): OmsEntityManagerInterface + { + $omsEntityManagerMock = $this->createMock(OmsEntityManagerInterface::class); + $omsEntityManagerMock->expects($this->exactly($storesCount)) + ->method('updateReservation'); + + return $omsEntityManagerMock; + } +} diff --git a/tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php b/tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php index 905632fa..cc2124e2 100644 --- a/tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php +++ b/tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php @@ -16,6 +16,7 @@ use Generated\Shared\DataBuilder\QuoteBuilder; use Generated\Shared\Transfer\CustomerTransfer; use Generated\Shared\Transfer\OmsEventTriggeredTransfer; +use Generated\Shared\Transfer\OrderFilterTransfer; use Generated\Shared\Transfer\OrderTransfer; use Generated\Shared\Transfer\QuoteTransfer; use Generated\Shared\Transfer\StoreTransfer; @@ -54,12 +55,27 @@ * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null) * @method \Spryker\Zed\Oms\Business\OmsFacadeInterface getFacade() * - * @SuppressWarnings(\SprykerTest\Zed\Oms\PHPMD) + * @SuppressWarnings(PHPMD) */ class OmsBusinessTester extends Actor { use _generated\OmsBusinessTesterActions; + /** + * @var string + */ + public const DEFAULT_OMS_PROCESS_NAME = 'Test01'; + + /** + * @var string + */ + public const FAKE_BILLING_ADDRESS = 'TestAddress 1'; + + /** + * @var string + */ + public const FAKE_SKU = 'FAKE_SKU'; + /** * @return void */ @@ -445,4 +461,17 @@ public function isApplicable(OmsEventTriggeredTransfer $omsEventTriggeredTransfe return $omsEventTriggeredListener; } + + /** + * @param int $idSalesOrder + * + * @return \Generated\Shared\Transfer\OrderTransfer + */ + public function getOrderByIdSalesOrder(int $idSalesOrder): OrderTransfer + { + return $this->getLocator()->sales()->facade() + ->getOrder( + (new OrderFilterTransfer())->setSalesOrderId($idSalesOrder), + ); + } }