Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteGeneratorTest.php
Go to the documentation of this file.
1 <?php
8 
14 class QuoteGeneratorTest extends \PHPUnit\Framework\TestCase
15 {
19  private $storeManager;
20 
24  private $productRepository;
25 
29  private $optionRepository;
30 
34  private $productCollectionFactory;
35 
39  private $linkManagement;
40 
44  private $serializer;
45 
49  private $config;
50 
54  private $fixtureModelMock;
55 
59  private $fixture;
60 
64  public function setUp()
65  {
66  $this->fixtureModelMock = $this->getMockBuilder(\Magento\Setup\Fixtures\FixtureModel::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
70  ->disableOriginalConstructor()
71  ->getMockForAbstractClass();
72  $this->productRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
73  ->disableOriginalConstructor()
74  ->getMockForAbstractClass();
75  $this->optionRepository = $this->getMockBuilder(
76  \Magento\ConfigurableProduct\Api\OptionRepositoryInterface::class
77  )
78  ->disableOriginalConstructor()
79  ->getMockForAbstractClass();
80  $this->productCollectionFactory = $this->getMockBuilder(
81  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class
82  )
83  ->disableOriginalConstructor()
84  ->setMethods(['create'])
85  ->getMock();
86  $this->linkManagement = $this->getMockBuilder(\Magento\ConfigurableProduct\Api\LinkManagementInterface::class)
87  ->disableOriginalConstructor()
88  ->getMockForAbstractClass();
89  $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\SerializerInterface::class)
90  ->disableOriginalConstructor()
91  ->getMockForAbstractClass();
92  $this->config = $this->getMockBuilder(\Magento\Setup\Fixtures\Quote\QuoteConfiguration::class)
93  ->disableOriginalConstructor()
94  ->setMethods(
95  [
96  'getSimpleCountTo',
97  'getConfigurableCountTo',
98  'getBigConfigurableCountTo',
99  'getRequiredQuoteQuantity',
100  'getFixtureDataFilename',
101  'getExistsQuoteQuantity',
102  ]
103  )
104  ->getMock();
105  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
106 
107  $this->fixture = $objectManager->getObject(
108  \Magento\Setup\Fixtures\Quote\QuoteGenerator::class,
109  [
110  'fixtureModel' => $this->fixtureModelMock,
111  'storeManager' => $this->storeManager,
112  'productRepository' => $this->productRepository,
113  'optionRepository' => $this->optionRepository,
114  'productCollectionFactory' => $this->productCollectionFactory,
115  'linkManagement' => $this->linkManagement,
116  'serializer' => $this->serializer,
117  'config' => $this->config,
118  ]
119  );
120  }
121 
127  public function testGenerateQuotes()
128  {
129  $storeId = 1;
130  $websiteId = 1;
131  $storeGroupId = 1;
132  $simpleProductIds = [1, 2];
133  $configurableProductId = [3];
134  $bigConfigurableProductId = [4];
135  $dir = str_replace('Test/Unit/', '', dirname(__DIR__));
136  $store = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
137  ->disableOriginalConstructor()
138  ->getMockForAbstractClass();
139  $website = $this->getMockBuilder(\Magento\Store\Api\Data\WebsiteInterface::class)
140  ->disableOriginalConstructor()
141  ->getMockForAbstractClass();
142  $storeGroup = $this->getMockBuilder(\Magento\Store\Api\Data\GroupInterface::class)
143  ->disableOriginalConstructor()
144  ->getMockForAbstractClass();
145  $productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
146  ->disableOriginalConstructor()
147  ->getMock();
148  $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
149  ->disableOriginalConstructor()
150  ->getMock();
151  $this->config->expects($this->atLeastOnce())->method('getSimpleCountTo')->willReturn(2);
152  $this->config->expects($this->atLeastOnce())->method('getConfigurableCountTo')->willReturn(1);
153  $this->config->expects($this->atLeastOnce())->method('getBigConfigurableCountTo')->willReturn(1);
154  $this->config->expects($this->atLeastOnce())->method('getRequiredQuoteQuantity')->willReturn(1);
155  $this->config->expects($this->atLeastOnce())->method('getExistsQuoteQuantity')->willReturn(0);
156  $this->config->expects($this->atLeastOnce())
157  ->method('getFixtureDataFilename')
158  ->willReturn($dir . DIRECTORY_SEPARATOR . "_files" . DIRECTORY_SEPARATOR . 'orders_fixture_data.json');
159  $this->storeManager->expects($this->atLeastOnce())->method('getStores')->willReturn([$store]);
160  $this->storeManager->expects($this->atLeastOnce())
161  ->method('getWebsite')->with($websiteId)->willReturn($website);
162  $this->storeManager->expects($this->atLeastOnce())
163  ->method('getGroup')->with($storeGroupId)->willReturn($storeGroup);
164  $store->expects($this->atLeastOnce())->method('getId')->willReturn($storeId);
165  $store->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId);
166  $store->expects($this->atLeastOnce())->method('getStoreGroupId')->willReturn($storeGroupId);
167  $website->expects($this->atLeastOnce())->method('getName')->willReturn('Default');
168  $store->expects($this->atLeastOnce())->method('getName')->willReturn('Default');
169  $storeGroup->expects($this->atLeastOnce())->method('getName')->willReturn('Default');
170  $this->storeManager->expects($this->atLeastOnce())->method('setCurrentStore')->with($storeId);
171  $this->productCollectionFactory->expects($this->atLeastOnce())
172  ->method('create')->willReturn($productCollection);
173  $productCollection->expects($this->atLeastOnce())->method('addStoreFilter')->with(1)->willReturnSelf();
174  $productCollection->expects($this->atLeastOnce())->method('addWebsiteFilter')->with(1)->willReturnSelf();
175  $productCollection->expects($this->atLeastOnce())->method('getSelect')->willReturn($select);
176  $select->expects($this->atLeastOnce())
177  ->method('where')
178  ->withConsecutive(
179  [' type_id = \'simple\' '],
180  [' sku NOT LIKE \'Big%\' '],
181  [' type_id = \'configurable\' '],
182  [' sku NOT LIKE \'Big%\' '],
183  [' type_id = \'configurable\' '],
184  [' sku LIKE \'Big%\' ']
185  )->willReturnSelf();
186  $productCollection->expects($this->atLeastOnce())
187  ->method('getAllIds')
188  ->withConsecutive([2], [1], [1])
189  ->willReturnOnConsecutiveCalls($simpleProductIds, $configurableProductId, $bigConfigurableProductId);
190  $this->prepareProducts();
191  $this->mockConnection();
192  $this->fixture->generateQuotes();
193  }
194 
200  private function prepareProducts()
201  {
202  $product = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
203  ->disableOriginalConstructor()
204  ->getMockForAbstractClass();
205  $configurableChild = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
206  ->disableOriginalConstructor()
207  ->getMockForAbstractClass();
208  $childProduct = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
209  ->disableOriginalConstructor()
210  ->getMockForAbstractClass();
211  $option = $this->getMockBuilder(\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute::class)
212  ->disableOriginalConstructor()
213  ->getMock();
214  $optionValue = $this->getMockBuilder(\Magento\ConfigurableProduct\Api\Data\OptionValueInterface::class)
215  ->disableOriginalConstructor()
216  ->getMockForAbstractClass();
217  $this->productRepository->expects($this->atLeastOnce())
218  ->method('getById')
219  ->withConsecutive([1], [2], [3], [4])
220  ->willReturn($product);
221  $product->expects($this->atLeastOnce())
222  ->method('getSku')->willReturnOnConsecutiveCalls('sku1', 'sku2', 'sku3', 'sku3', 'sku4', 'sku4');
223  $product->expects($this->atLeastOnce())
224  ->method('getName')->willReturnOnConsecutiveCalls('name1', 'name2', 'name3', 'name4');
225  $this->serializer->expects($this->atLeastOnce())
226  ->method('serialize')
227  ->willReturn('a:1:{i:10;i:1;}');
228  $this->optionRepository->expects($this->atLeastOnce())
229  ->method('getList')
230  ->withConsecutive(['sku3'], ['sku4'])
231  ->willReturn([$option]);
232  $this->linkManagement->expects($this->atLeastOnce())
233  ->method('getChildren')
234  ->withConsecutive(['sku3'], ['sku4'])
235  ->willReturn([$configurableChild]);
236  $configurableChild->expects($this->atLeastOnce())
237  ->method('getSku')
238  ->willReturnOnConsecutiveCalls('childSku3', 'childSku3', 'childSku4', 'childSku4');
239  $this->productRepository->expects($this->atLeastOnce())
240  ->method('get')
241  ->withConsecutive(['childSku3'], ['childSku4'])
242  ->willReturn($childProduct);
243  $childProduct->expects($this->atLeastOnce())->method('getId')->willReturnOnConsecutiveCalls(10, 11);
244  $option->expects($this->atLeastOnce())->method('getLabel')->willReturnOnConsecutiveCalls('label3', 'label4');
245  $option->expects($this->atLeastOnce())
246  ->method('getAttributeId')->willReturnOnConsecutiveCalls(10, 10, 20, 20);
247  $option->expects($this->atLeastOnce())->method('getValues')->willReturn([$optionValue]);
248  $optionValue->expects($this->atLeastOnce())->method('getValueIndex')->willReturn(1);
249  $configurableChild->expects($this->atLeastOnce())
250  ->method('getName')->willReturnOnConsecutiveCalls('childName3', 'childName4');
251  }
252 
258  private function mockConnection()
259  {
260  $objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
261  ->disableOriginalConstructor()
262  ->getMockForAbstractClass();
263  $resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
264  ->disableOriginalConstructor()
265  ->getMock();
266  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
267  ->disableOriginalConstructor()
268  ->getMockForAbstractClass();
269  $statement = $this->getMockBuilder(\Magento\Framework\DB\Statement\Pdo\Mysql::class)
270  ->disableOriginalConstructor()
271  ->getMock();
272  $this->fixtureModelMock->expects($this->atLeastOnce())->method('getObjectManager')->willReturn($objectManager);
273  $objectManager->expects($this->atLeastOnce())
274  ->method('get')
275  ->willReturn($resource);
276  $resource->expects($this->atLeastOnce())->method('getConnection')->willReturn($connection);
277  $connection->expects($this->atLeastOnce())
278  ->method('getTableName')
279  ->willReturn('table_name');
280  $resource->expects($this->atLeastOnce())
281  ->method('getTable')
282  ->willReturn('table_name');
283  $connection->expects($this->atLeastOnce())
284  ->method('query')
285  ->willReturn($statement);
286  $connection->expects($this->atLeastOnce())->method('getTransactionLevel')->willReturn(0);
287  $connection->expects($this->atLeastOnce())->method('beginTransaction')->willReturnSelf();
288  $statement->expects($this->atLeastOnce())->method('fetchColumn')->with(0)->willReturn(25);
289  }
290 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$resource
Definition: bulk.php:12
$connection
Definition: bulk.php:13