Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RelatedProductsTest.php
Go to the documentation of this file.
1 <?php
7 
8 class RelatedProductsTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
19 
20  protected function setUp()
21  {
22  $this->relatedProductTypes = ['type1', 'type2', 'type3'];
23  $this->model = new \Magento\Quote\Model\Quote\Item\RelatedProducts($this->relatedProductTypes);
24  }
25 
34  public function testGetRelatedProductIds($optionValue, $productId, $expectedResult)
35  {
36  $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
37  $itemOptionMock = $this->createPartialMock(
38  \Magento\Quote\Model\Quote\Item\Option::class,
39  ['getValue', 'getProductId', '__wakeup']
40  );
41 
42  $quoteItemMock->expects(
43  $this->once()
44  )->method(
45  'getOptionByCode'
46  )->with(
47  'product_type'
48  )->will(
49  $this->returnValue($itemOptionMock)
50  );
51 
52  $itemOptionMock->expects($this->once())->method('getValue')->will($this->returnValue($optionValue));
53 
54  $itemOptionMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
55 
56  $this->assertEquals($expectedResult, $this->model->getRelatedProductIds([$quoteItemMock]));
57  }
58 
59  /*
60  * Data provider for testGetRelatedProductIds
61  *
62  * @return array
63  */
68  {
69  return [
70  ['optionValue' => 'type1', 'productId' => 123, 'expectedResult' => [123]],
71  ['optionValue' => 'other_type', 'productId' => 123, 'expectedResult' => []],
72  ['optionValue' => 'type1', 'productId' => null, 'expectedResult' => []],
73  ['optionValue' => 'other_type', 'productId' => false, 'expectedResult' => []]
74  ];
75  }
76 
81  {
82  $quoteItemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
83 
84  $quoteItemMock->expects(
85  $this->once()
86  )->method(
87  'getOptionByCode'
88  )->with(
89  'product_type'
90  )->will(
91  $this->returnValue(new \stdClass())
92  );
93 
94  $this->assertEquals([], $this->model->getRelatedProductIds([$quoteItemMock]));
95  }
96 }