Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OptionManagementTest.php
Go to the documentation of this file.
1 <?php
9 
10 use \Magento\Bundle\Model\OptionManagement;
11 
12 class OptionManagementTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
23 
28 
32  protected $optionMock;
33 
37  protected $productMock;
38 
39  protected function setUp()
40  {
41  $this->optionRepositoryMock =
42  $this->createMock(\Magento\Bundle\Api\ProductOptionRepositoryInterface::class);
43  $this->productRepositoryMock =
44  $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
45  $this->optionMock = $this->createMock(\Magento\Bundle\Api\Data\OptionInterface::class);
46  $this->productMock = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class);
47 
48  $this->model = new OptionManagement($this->optionRepositoryMock, $this->productRepositoryMock);
49  }
50 
51  public function testSave()
52  {
53  $this->optionMock->expects($this->once())->method('getSku')->willReturn('bundle_product_sku');
54  $this->productRepositoryMock->expects($this->once())
55  ->method('get')
56  ->with('bundle_product_sku')
57  ->willReturn($this->productMock);
58  $this->productMock->expects($this->once())
59  ->method('getTypeId')
60  ->willReturn(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE);
61  $this->optionRepositoryMock->expects($this->once())
62  ->method('save')
63  ->with($this->productMock, $this->optionMock);
64 
65  $this->model->save($this->optionMock);
66  }
67 
72  public function testSaveWithException()
73  {
74  $this->optionMock->expects($this->once())->method('getSku')->willReturn('bundle_product_sku');
75  $this->productRepositoryMock->expects($this->once())
76  ->method('get')
77  ->with('bundle_product_sku')
78  ->willReturn($this->productMock);
79  $this->productMock->expects($this->once())
80  ->method('getTypeId')
81  ->willReturn(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
82  $this->optionRepositoryMock->expects($this->never())->method('save');
83 
84  $this->model->save($this->optionMock);
85  }
86 }