OptionRepositoryTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Bundle\Model;
  8. use Magento\Bundle\Api\Data\LinkInterface;
  9. use Magento\Bundle\Api\Data\OptionInterface;
  10. use Magento\Bundle\Api\Data\LinkInterfaceFactory;
  11. use Magento\Bundle\Api\Data\OptionInterfaceFactory;
  12. use Magento\Catalog\Api\ProductRepositoryInterface;
  13. use Magento\CatalogInventory\Api\StockRegistryInterface;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. /**
  17. * Test option repository class
  18. */
  19. class OptionRepositoryTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * @var OptionRepository
  27. */
  28. private $optionRepository;
  29. /**
  30. * @var ProductRepositoryInterface
  31. */
  32. private $productRepository;
  33. protected function setUp()
  34. {
  35. $this->objectManager = Bootstrap::getObjectManager();
  36. $this->optionRepository = $this->objectManager->get(OptionRepository::class);
  37. $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  38. }
  39. /**
  40. * @magentoDataFixture Magento/Catalog/_files/products.php
  41. * @magentoDataFixture Magento/Bundle/_files/empty_bundle_product.php
  42. */
  43. public function testBundleProductIsSaleableAfterNewOptionSave()
  44. {
  45. $bundleProduct = $this->productRepository->get('bundle-product');
  46. /** @var OptionInterface $newOption */
  47. $newOption = $this->objectManager->create(OptionInterfaceFactory::class)->create();
  48. /** @var LinkInterface $productLink */
  49. $productLink = $this->objectManager->create(LinkInterfaceFactory::class)->create();
  50. $newOption->setTitle('new-option');
  51. $newOption->setRequired(true);
  52. $newOption->setType('select');
  53. $newOption->setSku($bundleProduct->getSku());
  54. $productLink->setSku('simple');
  55. $productLink->setQty(1);
  56. $productLink->setIsDefault(true);
  57. $productLink->setCanChangeQuantity(0);
  58. $newOption->setProductLinks([$productLink]);
  59. $optionId = $this->optionRepository->save($bundleProduct, $newOption);
  60. $bundleProduct = $this->productRepository->get($bundleProduct->getSku(), false, null, true);
  61. $this->assertNotNull($optionId, 'Bundle option was not saved correctly');
  62. $this->assertTrue($bundleProduct->isSaleable(), 'Bundle product should show as in stock once option is added');
  63. }
  64. }