Skip to content

Commit

Permalink
Merge pull request #2291 from magento-engcom/MSI-2284-Rely-only-on-Sa…
Browse files Browse the repository at this point in the history
…les-Channels-in-Store-Pickup-APIs

MSI-2284-Rely-only-on-Sales-Channels-in-Store-Pickup-APIs.
  • Loading branch information
ishakhsuvarov authored Jun 5, 2019
2 parents b999025 + 24cb985 commit 7c2b24b
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryInStorePickupApi\Api\GetIsAnyPickupLocationAvailableInterface;
use Magento\InventoryInStorePickupApi\Api\GetPickupLocationsAssignedToStockOrderedByPriorityInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventoryInStorePickupApi\Api\GetPickupLocationsAssignedToSalesChannelInterface;
use Magento\InventorySalesApi\Api\GetStockBySalesChannelInterface;

/**
Expand All @@ -19,37 +18,27 @@
class GetIsAnyPickupLocationAvailable implements GetIsAnyPickupLocationAvailableInterface
{
/**
* @var GetPickupLocationsAssignedToStockOrderedByPriorityInterface
* @var GetPickupLocationsAssignedToSalesChannelInterface
*/
private $getPickupLocations;

/**
* @var GetStockBySalesChannelInterface
* @param GetPickupLocationsAssignedToSalesChannelInterface $getPickupLocations
*/
private $getStockBySalesChannel;

/**
* @param GetPickupLocationsAssignedToStockOrderedByPriorityInterface $getPickupLocations
* @param GetStockBySalesChannelInterface $getStockBySalesChannel
*/
public function __construct(
GetPickupLocationsAssignedToStockOrderedByPriorityInterface $getPickupLocations,
GetStockBySalesChannelInterface $getStockBySalesChannel
) {
public function __construct(GetPickupLocationsAssignedToSalesChannelInterface $getPickupLocations)
{
$this->getPickupLocations = $getPickupLocations;
$this->getStockBySalesChannel = $getStockBySalesChannel;
}

/**
* @inheritdoc
*/
public function execute(SalesChannelInterface $salesChannel): bool
public function execute(string $salesChannelType, string $salesChannelCode): bool
{
$result = false;

try {
$stock = $this->getStockBySalesChannel->execute($salesChannel);
$result = count($this->getPickupLocations->execute((int)$stock->getStockId())) > 0;
$result = count($this->getPickupLocations->execute($salesChannelType, $salesChannelCode)) > 0;
} catch (NoSuchEntityException $exception) {
return $result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
use Magento\InventoryInStorePickupApi\Api\GetNearbyPickupLocationsInterface;
use Magento\InventoryInStorePickupApi\Model\Mapper;
use Magento\InventorySalesApi\Api\StockResolverInterface;

/**
* @inheritdoc
Expand Down Expand Up @@ -60,6 +61,11 @@ class GetNearbyPickupLocations implements GetNearbyPickupLocationsInterface
*/
private $sourceRepository;

/**
* @var StockResolverInterface
*/
private $stockResolver;

/**
* @param Mapper $mapper
* @param AddressToSourceSelectionAddress $addressToSourceSelectionAddress
Expand All @@ -68,6 +74,7 @@ class GetNearbyPickupLocations implements GetNearbyPickupLocationsInterface
* @param GetStockSourceLinksInterface $getStockSourceLinks
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param SourceRepositoryInterface $sourceRepository
* @param StockResolverInterface $stockResolver
*/
public function __construct(
Mapper $mapper,
Expand All @@ -76,7 +83,8 @@ public function __construct(
GetDistanceOrderedSourceCodes $getDistanceOrderedSourceCodes,
GetStockSourceLinksInterface $getStockSourceLinks,
SearchCriteriaBuilder $searchCriteriaBuilder,
SourceRepositoryInterface $sourceRepository
SourceRepositoryInterface $sourceRepository,
StockResolverInterface $stockResolver
) {
$this->mapper = $mapper;
$this->addressToSourceSelectionAddress = $addressToSourceSelectionAddress;
Expand All @@ -85,20 +93,27 @@ public function __construct(
$this->getStockSourceLinks = $getStockSourceLinks;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sourceRepository = $sourceRepository;
$this->stockResolver = $stockResolver;
}

/**
* @inheritdoc
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(AddressInterface $address, int $radius, int $stockId): array
{
public function execute(
AddressInterface $address,
int $radius,
string $salesChannelType,
string $salesChannelCode
): array {
$sourceSelectionAddress = $this->addressToSourceSelectionAddress->execute($address);
$latLng = $this->getLatLngFromAddress->execute($sourceSelectionAddress);

$codes = $this->getDistanceOrderedSourceCodes->execute($latLng, $radius);

$stock = $this->stockResolver->execute($salesChannelType, $salesChannelCode);
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
->addFilter(StockSourceLinkInterface::STOCK_ID, $stock->getStockId())
->addFilter(StockSourceLinkInterface::SOURCE_CODE, $codes, 'in')
->create();
$searchResult = $this->getStockSourceLinks->execute($searchCriteria);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
namespace Magento\InventoryInStorePickup\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface;
use Magento\InventoryInStorePickupApi\Model\Mapper;
use Magento\InventoryInStorePickupApi\Api\GetPickupLocationsAssignedToStockOrderedByPriorityInterface;
use Magento\InventoryInStorePickupApi\Api\GetPickupLocationsAssignedToSalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;

/**
* @inheritdoc
*/
class GetPickupLocationsAssignedToStockOrderedByPriority implements GetPickupLocationsAssignedToStockOrderedByPriorityInterface
class GetPickupLocationsAssignedToSalesChannel implements GetPickupLocationsAssignedToSalesChannelInterface
{
/**
* @var GetSourcesAssignedToStockOrderedByPriorityInterface
Expand All @@ -27,25 +29,35 @@ class GetPickupLocationsAssignedToStockOrderedByPriority implements GetPickupLoc
*/
private $mapper;

/**
* @var StockResolverInterface
*/
private $stockResolver;

/**
* @param GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority
* @param StockResolverInterface $stockResolver
* @param Mapper $mapper
*/
public function __construct(
GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority,
StockResolverInterface $stockResolver,
Mapper $mapper
) {
$this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority;
$this->stockResolver = $stockResolver;
$this->mapper = $mapper;
}

/**
* @inheritdoc
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function execute(int $stockId): array
public function execute(string $salesChannelType, string $salesChannelCode): array
{
$sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stockId);
$stock = $this->stockResolver->execute($salesChannelType, $salesChannelCode);
$sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stock->getStockId());

$result = [];
foreach ($sources as $source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\InventoryInStorePickup\Test\Integration;

use Magento\InventoryInStorePickup\Model\GetIsAnyPickupLocationAvailable;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
Expand All @@ -23,15 +22,9 @@ class GetIsAnyPickupLocationAvailableTest extends TestCase
*/
private $getIsPickupLocationAvailable;

/**
* @var SalesChannelInterfaceFactory
*/
private $salesChannelFactory;

public function setUp()
{
$this->getIsPickupLocationAvailable = Bootstrap::getObjectManager()->get(GetIsAnyPickupLocationAvailable::class);
$this->salesChannelFactory = Bootstrap::getObjectManager()->get(SalesChannelInterfaceFactory::class);
}

/**
Expand All @@ -47,10 +40,7 @@ public function setUp()
*/
public function testExecuteWithAvailableLocations()
{
$salesChannel = $this->salesChannelFactory->create();
$salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE);
$salesChannel->setCode('eu_website');
$result = $this->getIsPickupLocationAvailable->execute($salesChannel);
$result = $this->getIsPickupLocationAvailable->execute(SalesChannelInterface::TYPE_WEBSITE, 'eu_website');
$this->assertTrue($result);
}

Expand All @@ -66,10 +56,7 @@ public function testExecuteWithAvailableLocations()
*/
public function testExecuteWithoutAvailableLocations()
{
$salesChannel = $this->salesChannelFactory->create();
$salesChannel->setType(SalesChannelInterface::TYPE_WEBSITE);
$salesChannel->setCode('eu_website');
$result = $this->getIsPickupLocationAvailable->execute($salesChannel);
$result = $this->getIsPickupLocationAvailable->execute(SalesChannelInterface::TYPE_WEBSITE, 'eu_website');
$this->assertFalse($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
use Magento\InventoryInStorePickup\Model\AddressFactory;
use Magento\InventoryInStorePickup\Model\GetNearbyPickupLocations;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Integration tests coverage for @see \Magento\InventoryInStorePickup\Model\GetNearbyPickupLocations.
*/
class GetNearbyPickupLocationsOfflineTest extends TestCase
{
/**
Expand All @@ -37,14 +41,18 @@ protected function setUp()
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/source_pickup_location_attributes.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/inventory_geoname.php
*
* @magentoConfigFixture default/cataloginventory/source_selection_distance_based/provider offline
*
* @param array $addressData
* @param int $radius
* @param int $stockId
* @param string[] $sortedSourceCodes
*
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @dataProvider executeDataProvider
* @magentoAppArea frontend
*
Expand All @@ -53,13 +61,18 @@ protected function setUp()
public function testExecute(
array $addressData,
int $radius,
int $stockId,
string $salesChannelCode,
array $sortedSourceCodes
) {
$address = $this->addressFactory->create($addressData);

/** @var PickupLocationInterface[] $sources */
$pickupLocations = $this->getNearbyPickupLocations->execute($address, $radius, $stockId);
$pickupLocations = $this->getNearbyPickupLocations->execute(
$address,
$radius,
SalesChannelInterface::TYPE_WEBSITE,
$salesChannelCode
);

$this->assertCount(count($sortedSourceCodes), $pickupLocations);
foreach ($sortedSourceCodes as $key => $code) {
Expand All @@ -76,7 +89,7 @@ public function testExecute(
* City
* ]
* Radius (in KM),
* Stock Id,
* Sales Channel Code,
* Expected Source Codes[]
* ]
*
Expand All @@ -91,7 +104,7 @@ public function executeDataProvider(): array
'postcode' => '81671'
],
500,
10,
'eu_website',
['eu-3']
],
[
Expand All @@ -100,7 +113,7 @@ public function executeDataProvider(): array
'region' => 'Bretagne'
],
1000,
10,
'eu_website',
['eu-1']
],
[
Expand All @@ -109,7 +122,7 @@ public function executeDataProvider(): array
'city' => 'Saint-Saturnin-lès-Apt'
],
1000,
30,
'global_website',
['eu-1', 'eu-3']
],
[
Expand All @@ -118,7 +131,7 @@ public function executeDataProvider(): array
'postcode' => '12022'
],
350,
10,
'eu_website',
[]
],
[
Expand All @@ -129,7 +142,7 @@ public function executeDataProvider(): array
'city' => 'Rasun Di Sotto'
],
350,
10,
'eu_website',
['eu-3']
],
[
Expand All @@ -138,7 +151,7 @@ public function executeDataProvider(): array
'postcode' => '86559',
],
750,
30,
'global_website',
['eu-3', 'eu-1']
],
[
Expand All @@ -147,7 +160,7 @@ public function executeDataProvider(): array
'region' => 'Kansas'
],
1000,
20,
'us_website',
['us-1']
]
];
Expand Down
Loading

0 comments on commit 7c2b24b

Please sign in to comment.