Skip to content

Commit

Permalink
Merge pull request #113 from magento-engcom/default-stock-install-data
Browse files Browse the repository at this point in the history
Add default Stock in Data Install
  • Loading branch information
maghamed authored Oct 4, 2017
2 parents 0a77e42 + 18d9872 commit 48bd40b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
76 changes: 76 additions & 0 deletions app/code/Magento/InventoryCatalog/Setup/InstallData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\InventoryCatalog\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventoryApi\Api\StockRepositoryInterface;
use Magento\Framework\Api\DataObjectHelper;

/**
* Class InstallData
*/
class InstallData implements InstallDataInterface
{
/**
* @var StockRepositoryInterface
*/
private $stockRepository;

/**
* @var StockInterfaceFactory
*/
private $stockFactory;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @param StockRepositoryInterface $stockRepository
* @param StockInterfaceFactory $stockFactory
* @param DataObjectHelper $dataObjectHelper
*/
public function __construct(
StockRepositoryInterface $stockRepository,
StockInterfaceFactory $stockFactory,
DataObjectHelper $dataObjectHelper
) {
$this->stockRepository = $stockRepository;
$this->stockFactory = $stockFactory;
$this->dataObjectHelper = $dataObjectHelper;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->addDefaultStock();
}

/**
* Add default stock
*
* @return void
*/
private function addDefaultStock()
{
$data = [
StockInterface::STOCK_ID => 1,
StockInterface::NAME => 'Default Stock'
];
$source = $this->stockFactory->create();
$this->dataObjectHelper->populateWithArray($source, $data, StockInterface::class);
$this->stockRepository->save($source);
}
}
41 changes: 41 additions & 0 deletions app/code/Magento/InventoryCatalog/Test/Api/GetDefaultStockTest.php
Original file line number Diff line number Diff line change
@@ -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\StockInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;
use Magento\Framework\Webapi\Rest\Request;

/**
* Class GetDefaultStockTest
*/
class GetDefaultStockTest extends WebapiAbstract
{
/**
* Test that default Stock is present after installation
*/
public function testGetDefaultSource()
{
$defaultStockId = 1;
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/inventory/stock/' . $defaultStockId,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => 'inventoryApiStockRepositoryV1',
'operation' => 'inventoryApiStockRepositoryV1Get',
],
];
if (self::ADAPTER_REST == TESTS_WEB_API_ADAPTER) {
$stock = $this->_webApiCall($serviceInfo);
} else {
$stock = $this->_webApiCall($serviceInfo, ['stockId' => $defaultStockId]);
}
$this->assertEquals($defaultStockId, $stock[StockInterface::STOCK_ID]);
}
}

0 comments on commit 48bd40b

Please sign in to comment.