Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefreshSpecialPricesTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class RefreshSpecialPricesTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $_objectManager;
20 
24  protected $_model;
25 
29  protected $_storeManagerMock;
30 
34  protected $_resourceMock;
35 
39  protected $_dateTimeMock;
40 
44  protected $_localeDateMock;
45 
49  protected $_eavConfigMock;
50 
55 
59  protected $metadataPool;
60 
64  protected $metadataMock;
65 
66  protected function setUp()
67  {
68  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
69 
70  $this->_storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
71  $this->_resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
72  $this->_dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
73  $this->_localeDateMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
74  $this->_eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
75  $this->_priceProcessorMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Price\Processor::class);
76 
77  $this->metadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadata::class);
78 
79  $this->_model = $this->_objectManager->getObject(
80  \Magento\Catalog\Cron\RefreshSpecialPrices::class,
81  [
82  'storeManager' => $this->_storeManagerMock,
83  'resource' => $this->_resourceMock,
84  'dateTime' => $this->_dateTimeMock,
85  'localeDate' => $this->_localeDateMock,
86  'eavConfig' => $this->_eavConfigMock,
87  'processor' => $this->_priceProcessorMock
88  ]
89  );
90 
91  $this->metadataPool = $this->createMock(MetadataPool::class);
92 
93  $reflection = new \ReflectionClass(get_class($this->_model));
94  $reflectionProperty = $reflection->getProperty('metadataPool');
95  $reflectionProperty->setAccessible(true);
96  $reflectionProperty->setValue($this->_model, $this->metadataPool);
97  }
98 
99  public function testRefreshSpecialPrices()
100  {
101  $idsToProcess = [1, 2, 3];
102 
103  $this->metadataPool->expects($this->atLeastOnce())
104  ->method('getMetadata')
105  ->willReturn($this->metadataMock);
106 
107  $this->metadataMock->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');
108 
109  $this->metadataMock->expects($this->atLeastOnce())->method('getIdentifierField')->willReturn('entity_id');
110 
111  $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
112  $selectMock->expects($this->any())->method('from')->will($this->returnSelf());
113  $selectMock->expects($this->any())->method('joinLeft')->will($this->returnSelf());
114  $selectMock->expects($this->any())->method('where')->will($this->returnSelf());
115 
116  $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
117  $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock));
118  $connectionMock->expects(
119  $this->any()
120  )->method(
121  'fetchCol'
122  )->will(
123  $this->returnValue($idsToProcess)
124  );
125 
126  $this->_resourceMock->expects(
127  $this->once()
128  )->method(
129  'getConnection'
130  )->will(
131  $this->returnValue($connectionMock)
132  );
133 
134  $this->_resourceMock->expects(
135  $this->any()
136  )->method(
137  'getTableName'
138  )->will(
139  $this->returnValue('category')
140  );
141 
142  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
143  $storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
144 
145  $this->_storeManagerMock->expects(
146  $this->once()
147  )->method(
148  'getStores'
149  )->with(
150  true
151  )->will(
152  $this->returnValue([$storeMock])
153  );
154 
155  $this->_localeDateMock->expects(
156  $this->once()
157  )->method(
158  'scopeTimeStamp'
159  )->with(
160  $storeMock
161  )->will(
162  $this->returnValue(32000)
163  );
164 
165  $indexerMock = $this->createMock(\Magento\Indexer\Model\Indexer::class);
166  $indexerMock->expects($this->exactly(2))->method('reindexList');
167 
168  $this->_priceProcessorMock->expects(
169  $this->exactly(2)
170  )->method(
171  'getIndexer'
172  )->will(
173  $this->returnValue($indexerMock)
174  );
175 
176  $attributeMock = $this->getMockForAbstractClass(
177  \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
178  [],
179  '',
180  false,
181  true,
182  true,
183  ['__wakeup', 'getAttributeId']
184  );
185  $attributeMock->expects($this->any())->method('getAttributeId')->will($this->returnValue(1));
186 
187  $this->_eavConfigMock->expects($this->any())->method('getAttribute')->will($this->returnValue($attributeMock));
188 
189  $this->_model->execute();
190  }
191 }