Skip to content

Commit

Permalink
FRW-1817 Adjusted Dynamic Multistore Test Coverage (#10800)
Browse files Browse the repository at this point in the history
FRW-1817 Adjusted Dynamic Multistore Test Coverage
  • Loading branch information
vol4onok authored Mar 20, 2024
1 parent 8d0c3ba commit bb24b37
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 1 deletion.
115 changes: 115 additions & 0 deletions tests/SprykerTest/Zed/Oms/Business/Mail/MailHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerTest\Zed\Oms\Business\Mail;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\MailTransfer;
use Generated\Shared\Transfer\OrderTransfer;
use Orm\Zed\Sales\Persistence\SpySalesOrder;
use Orm\Zed\Sales\Persistence\SpySalesOrderAddress;
use Spryker\Zed\Oms\Business\Mail\MailHandler;
use Spryker\Zed\Oms\Dependency\Facade\OmsToMailInterface;
use Spryker\Zed\Oms\Dependency\Facade\OmsToSalesInterface;
use SprykerTest\Zed\Oms\OmsBusinessTester;

/**
* Auto-generated group annotations
*
* @group SprykerTest
* @group Zed
* @group Oms
* @group Business
* @group Mail
* @group MailHandlerTest
* Add your own group annotations below this line
*/
class MailHandlerTest extends Unit
{
/**
* @var \SprykerTest\Zed\Oms\OmsBusinessTester
*/
protected OmsBusinessTester $tester;

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
145 changes: 145 additions & 0 deletions tests/SprykerTest/Zed/Oms/Business/Util/ReservationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerTest\Zed\Oms\Business\Util;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\OmsProductReservationTransfer;
use Generated\Shared\Transfer\ReservationRequestTransfer;
use Spryker\DecimalObject\Decimal;
use Spryker\Zed\Oms\Business\Reader\ReservationReaderInterface;
use Spryker\Zed\Oms\Business\Util\Reservation;
use Spryker\Zed\Oms\Dependency\Facade\OmsToStoreFacadeInterface;
use Spryker\Zed\Oms\Persistence\OmsEntityManagerInterface;
use Spryker\Zed\Oms\Persistence\OmsRepositoryInterface;
use SprykerTest\Zed\Oms\OmsBusinessTester;

/**
* Auto-generated group annotations
*
* @group SprykerTest
* @group Zed
* @group Oms
* @group Business
* @group Util
* @group ReservationTest
* Add your own group annotations below this line
*/
class ReservationTest extends Unit
{
/**
* @var \SprykerTest\Zed\Oms\OmsBusinessTester
*/
protected OmsBusinessTester $tester;

/**
* @return void
*/
public function testUpdateReservationQuantityGetsAllStores(): void
{
// Arrange
$reservationReaderMock = $this->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;
}
}
31 changes: 30 additions & 1 deletion tests/SprykerTest/Zed/Oms/_support/OmsBusinessTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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),
);
}
}

0 comments on commit bb24b37

Please sign in to comment.