Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundlePriceAbstract.php
Go to the documentation of this file.
1 <?php
8 
13 abstract class BundlePriceAbstract extends \PHPUnit\Framework\TestCase
14 {
17 
20 
22  protected $objectManager;
23 
25  protected $productRepository;
26 
31 
32  protected function setUp()
33  {
35  $this->productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
36  $this->productCollectionFactory =
37  $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class);
38 
39  $scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
40  $scopeConfig->setValue(
41  \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
42  true,
44  );
45  }
46 
51  abstract public function getTestCases();
52 
62  protected function prepareFixture($strategyModifiers, $productSku)
63  {
64  $bundleProduct = $this->productRepository->get($productSku);
65 
66  foreach ($strategyModifiers as $modifier) {
67  if (method_exists($this, $modifier['modifierName'])) {
68  array_unshift($modifier['data'], $bundleProduct);
69  $bundleProduct = call_user_func_array([$this, $modifier['modifierName']], $modifier['data']);
70  } else {
71  throw new \Magento\Framework\Exception\InputException(
72  __('Modifier %s does not exists', $modifier['modifierName'])
73  );
74  }
75  }
76  $this->productRepository->save($bundleProduct);
77  }
78 
86  protected function addSimpleProduct(\Magento\Catalog\Model\Product $bundleProduct, array $optionsData)
87  {
88  $options = [];
89 
90  foreach ($optionsData as $optionData) {
91  $links = [];
92  $linksData = $optionData['links'];
93  unset($optionData['links']);
94 
95  $option = $this->objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
96  ->create(['data' => $optionData])
97  ->setSku($bundleProduct->getSku());
98 
99  foreach ($linksData as $linkData) {
100  $links[] = $this->objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
101  ->create(['data' => $linkData]);
102  }
103 
104  $option->setProductLinks($links);
105  $options[] = $option;
106  }
107 
108  $extension = $bundleProduct->getExtensionAttributes();
109  $extension->setBundleProductOptions($options);
110  $bundleProduct->setExtensionAttributes($extension);
111 
112  return $bundleProduct;
113  }
114 
120  protected function addCustomOption(\Magento\Catalog\Model\Product $bundleProduct, array $optionsData)
121  {
123  $customOptionFactory = $this->objectManager
124  ->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
125 
126  $options = [];
127  foreach ($optionsData as $optionData) {
129  [
130  'data' => $optionData
131  ]
132  );
133  $customOption->setProductSku($bundleProduct->getSku());
134  $customOption->setOptionId(null);
135 
137  }
138 
139  $bundleProduct->setOptions($options);
140  $bundleProduct->setCanSaveCustomOptions(true);
141 
142  return $bundleProduct;
143  }
144 }
$customOption
Definition: products.php:73
addSimpleProduct(\Magento\Catalog\Model\Product $bundleProduct, array $optionsData)
$optionData
addCustomOption($code, $value, $product=null)
Definition: Product.php:2040
__()
Definition: __.php:13
$customOptionFactory
$bundleProduct