-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from magento-engcom/default-stock-install-data
Add default Stock in Data Install
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
app/code/Magento/InventoryCatalog/Test/Api/GetDefaultStockTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |