Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default Stock in Data Install #113

Merged
merged 2 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}