Skip to content

Commit

Permalink
Merge pull request #107 from magento-engcom/default-source-install-data
Browse files Browse the repository at this point in the history
Add default Source in Data Install.
  • Loading branch information
maghamed committed Oct 4, 2017
2 parents 48bd40b + feafb15 commit b5bac61
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
45 changes: 44 additions & 1 deletion app/code/Magento/InventoryCatalog/Setup/InstallData.php
Expand Up @@ -9,16 +9,29 @@
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\Framework\Api\DataObjectHelper;

/**
* Class InstallData
* Install Default Source, Stock and link them together
*/
class InstallData implements InstallDataInterface
{
/**
* @var SourceRepositoryInterface
*/
private $sourceRepository;

/**
* @var SourceInterfaceFactory
*/
private $sourceFactory;

/**
* @var StockRepositoryInterface
*/
Expand All @@ -35,15 +48,21 @@ class InstallData implements InstallDataInterface
private $dataObjectHelper;

/**
* @param SourceRepositoryInterface $sourceRepository
* @param SourceInterfaceFactory $sourceFactory
* @param StockRepositoryInterface $stockRepository
* @param StockInterfaceFactory $stockFactory
* @param DataObjectHelper $dataObjectHelper
*/
public function __construct(
SourceRepositoryInterface $sourceRepository,
SourceInterfaceFactory $sourceFactory,
StockRepositoryInterface $stockRepository,
StockInterfaceFactory $stockFactory,
DataObjectHelper $dataObjectHelper
) {
$this->sourceRepository = $sourceRepository;
$this->sourceFactory = $sourceFactory;
$this->stockRepository = $stockRepository;
$this->stockFactory = $stockFactory;
$this->dataObjectHelper = $dataObjectHelper;
Expand All @@ -55,9 +74,33 @@ public function __construct(
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->addDefaultSource();
$this->addDefaultStock();
}

/**
* Add default source
*
* @return void
*/
private function addDefaultSource()
{
$data = [
SourceInterface::SOURCE_ID => 1,
SourceInterface::NAME => 'Default Source',
SourceInterface::ENABLED => 1,
SourceInterface::DESCRIPTION => 'Default Source',
SourceInterface::LATITUDE => 0,
SourceInterface::LONGITUDE => 0,
SourceInterface::PRIORITY => 0,
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::POSTCODE => '00000'
];
$source = $this->sourceFactory->create();
$this->dataObjectHelper->populateWithArray($source, $data, SourceInterface::class);
$this->sourceRepository->save($source);
}

/**
* Add default stock
*
Expand Down
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventoryCatalog\Test\Api;

use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;
use Magento\Framework\Webapi\Rest\Request;

/**
* Class GetDefaultSourceTest
*/
class GetDefaultSourceTest extends WebapiAbstract
{
/**
* Test that default Source is present after installation
*/
public function testGetDefaultSource()
{
$defaultSourceId = 1;
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/inventory/source/' . $defaultSourceId,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => 'inventoryApiSourceRepositoryV1',
'operation' => 'inventoryApiSourceRepositoryV1Get',
],
];
if (self::ADAPTER_REST == TESTS_WEB_API_ADAPTER) {
$source = $this->_webApiCall($serviceInfo);
} else {
$source = $this->_webApiCall($serviceInfo, ['sourceId' => $defaultSourceId]);
}
$this->assertEquals($defaultSourceId, $source[SourceInterface::SOURCE_ID]);
}
}

0 comments on commit b5bac61

Please sign in to comment.