Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
ListAssociatedProductsTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ListAssociatedProductsTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $contextMock;
14 
18  protected $registryMock;
19 
23  protected $productMock;
24 
28  protected $typeInstanceMock;
29 
33  protected $storeManagerMock;
34 
38  protected $storeMock;
39 
43  protected $block;
44 
48  protected $priceCurrency;
49 
50  protected function setUp()
51  {
52  $this->contextMock = $this->createMock(\Magento\Backend\Block\Template\Context::class);
53  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
54  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
55  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
56  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
57  $this->typeInstanceMock = $this->createMock(\Magento\GroupedProduct\Model\Product\Type\Grouped::class);
58 
59  $this->contextMock->expects(
60  $this->any()
61  )->method(
62  'getStoreManager'
63  )->will(
64  $this->returnValue($this->storeManagerMock)
65  );
66 
67  $this->priceCurrency = $this->getMockBuilder(
68  \Magento\Framework\Pricing\PriceCurrencyInterface::class
69  )->getMock();
70 
71  $this->block = new \Magento\GroupedProduct\Block\Product\Grouped\AssociatedProducts\ListAssociatedProducts(
72  $this->contextMock,
73  $this->registryMock,
74  $this->priceCurrency
75  );
76  }
77 
82  public function testGetAssociatedProducts()
83  {
84  $this->priceCurrency->expects(
85  $this->any()
86  )->method(
87  'format'
88  )->with(
89  '1.00',
90  false
91  )->will(
92  $this->returnValue('1')
93  );
94 
95  $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($this->storeMock));
96 
97  $this->productMock->expects(
98  $this->once()
99  )->method(
100  'getTypeInstance'
101  )->will(
102  $this->returnValue($this->typeInstanceMock)
103  );
104 
105  $this->registryMock->expects(
106  $this->once()
107  )->method(
108  'registry'
109  )->with(
110  'current_product'
111  )->will(
112  $this->returnValue($this->productMock)
113  );
114 
115  $this->typeInstanceMock->expects(
116  $this->once()
117  )->method(
118  'getAssociatedProducts'
119  )->with(
120  $this->productMock
121  )->will(
122  $this->returnValue([$this->generateAssociatedProduct(1), $this->generateAssociatedProduct(2)])
123  );
124 
125  $expectedResult = [
126  '0' => [
127  'id' => 'id1',
128  'sku' => 'sku1',
129  'name' => 'name1',
130  'qty' => 1,
131  'position' => 1,
132  'price' => '1',
133  ],
134  '1' => [
135  'id' => 'id2',
136  'sku' => 'sku2',
137  'name' => 'name2',
138  'qty' => 2,
139  'position' => 2,
140  'price' => '1',
141  ],
142  ];
143 
144  $this->assertEquals($expectedResult, $this->block->getAssociatedProducts());
145  }
146 
153  protected function generateAssociatedProduct($productKey = 0)
154  {
155  $associatedProduct = $this->createPartialMock(
156  \Magento\Framework\DataObject::class,
157  ['getQty', 'getPosition', 'getId', 'getSku', 'getName', 'getPrice']
158  );
159 
160  $associatedProduct->expects($this->once())->method('getId')->will($this->returnValue('id' . $productKey));
161  $associatedProduct->expects($this->once())->method('getSku')->will($this->returnValue('sku' . $productKey));
162  $associatedProduct->expects($this->once())->method('getName')->will($this->returnValue('name' . $productKey));
163  $associatedProduct->expects($this->once())->method('getQty')->will($this->returnValue($productKey));
164  $associatedProduct->expects($this->once())->method('getPosition')->will($this->returnValue($productKey));
165  $associatedProduct->expects($this->once())->method('getPrice')->will($this->returnValue('1.00'));
166 
167  return $associatedProduct;
168  }
169 }