RefreshSpecialPricesTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Cron;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class RefreshSpecialPricesTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  16. */
  17. protected $_objectManager;
  18. /**
  19. * @var \Magento\Catalog\Cron\RefreshSpecialPrices
  20. */
  21. protected $_model;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_storeManagerMock;
  26. /**
  27. * @var Resource|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_resourceMock;
  30. /**
  31. * @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $_dateTimeMock;
  34. /**
  35. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $_localeDateMock;
  38. /**
  39. * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $_eavConfigMock;
  42. /**
  43. * @var \Magento\Catalog\Model\Indexer\Product\Price\Processor|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $_priceProcessorMock;
  46. /**
  47. * @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $metadataPool;
  50. /**
  51. * @var \Magento\Framework\EntityManager\EntityMetadata|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $metadataMock;
  54. protected function setUp()
  55. {
  56. $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  57. $this->_storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  58. $this->_resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  59. $this->_dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
  60. $this->_localeDateMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  61. $this->_eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
  62. $this->_priceProcessorMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Price\Processor::class);
  63. $this->metadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadata::class);
  64. $this->_model = $this->_objectManager->getObject(
  65. \Magento\Catalog\Cron\RefreshSpecialPrices::class,
  66. [
  67. 'storeManager' => $this->_storeManagerMock,
  68. 'resource' => $this->_resourceMock,
  69. 'dateTime' => $this->_dateTimeMock,
  70. 'localeDate' => $this->_localeDateMock,
  71. 'eavConfig' => $this->_eavConfigMock,
  72. 'processor' => $this->_priceProcessorMock
  73. ]
  74. );
  75. $this->metadataPool = $this->createMock(MetadataPool::class);
  76. $reflection = new \ReflectionClass(get_class($this->_model));
  77. $reflectionProperty = $reflection->getProperty('metadataPool');
  78. $reflectionProperty->setAccessible(true);
  79. $reflectionProperty->setValue($this->_model, $this->metadataPool);
  80. }
  81. public function testRefreshSpecialPrices()
  82. {
  83. $idsToProcess = [1, 2, 3];
  84. $this->metadataPool->expects($this->atLeastOnce())
  85. ->method('getMetadata')
  86. ->willReturn($this->metadataMock);
  87. $this->metadataMock->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');
  88. $this->metadataMock->expects($this->atLeastOnce())->method('getIdentifierField')->willReturn('entity_id');
  89. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  90. $selectMock->expects($this->any())->method('from')->will($this->returnSelf());
  91. $selectMock->expects($this->any())->method('joinLeft')->will($this->returnSelf());
  92. $selectMock->expects($this->any())->method('where')->will($this->returnSelf());
  93. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  94. $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
  95. $connectionMock->expects(
  96. $this->any()
  97. )->method(
  98. 'fetchCol'
  99. )->will(
  100. $this->returnValue($idsToProcess)
  101. );
  102. $this->_resourceMock->expects(
  103. $this->once()
  104. )->method(
  105. 'getConnection'
  106. )->will(
  107. $this->returnValue($connectionMock)
  108. );
  109. $this->_resourceMock->expects(
  110. $this->any()
  111. )->method(
  112. 'getTableName'
  113. )->will(
  114. $this->returnValue('category')
  115. );
  116. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  117. $storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
  118. $this->_storeManagerMock->expects(
  119. $this->once()
  120. )->method(
  121. 'getStores'
  122. )->with(
  123. true
  124. )->will(
  125. $this->returnValue([$storeMock])
  126. );
  127. $this->_localeDateMock->expects(
  128. $this->once()
  129. )->method(
  130. 'scopeTimeStamp'
  131. )->with(
  132. $storeMock
  133. )->will(
  134. $this->returnValue(32000)
  135. );
  136. $indexerMock = $this->createMock(\Magento\Indexer\Model\Indexer::class);
  137. $indexerMock->expects($this->exactly(2))->method('reindexList');
  138. $this->_priceProcessorMock->expects(
  139. $this->exactly(2)
  140. )->method(
  141. 'getIndexer'
  142. )->will(
  143. $this->returnValue($indexerMock)
  144. );
  145. $attributeMock = $this->getMockForAbstractClass(
  146. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
  147. [],
  148. '',
  149. false,
  150. true,
  151. true,
  152. ['__wakeup', 'getAttributeId']
  153. );
  154. $attributeMock->expects($this->any())->method('getAttributeId')->will($this->returnValue(1));
  155. $this->_eavConfigMock->expects($this->any())->method('getAttribute')->will($this->returnValue($attributeMock));
  156. $this->_model->execute();
  157. }
  158. }