CollectionTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Test\Unit\Model\ResourceModel\Review;
  7. class CollectionTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Review\Model\ResourceModel\Review\Collection
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\DB\Select | \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $selectMock;
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $storeManagerMock;
  21. /**
  22. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb | \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $resourceMock;
  25. /**
  26. * @var \Magento\Framework\DB\Adapter\AdapterInterface | \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $readerAdapterMock;
  29. /**
  30. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  31. */
  32. protected $objectManager;
  33. protected function setUp()
  34. {
  35. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId']);
  36. $store->expects($this->any())->method('getId')->will($this->returnValue(1));
  37. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  38. $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store));
  39. $this->objectManager = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this));
  40. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['getConnection', 'getMainTable', 'getTable'])
  43. ->getMockForAbstractClass();
  44. $this->readerAdapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods(['select', 'prepareSqlCondition', 'quoteInto'])
  47. ->getMockForAbstractClass();
  48. $this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->readerAdapterMock->expects($this->any())
  52. ->method('select')
  53. ->willReturn($this->selectMock);
  54. $this->resourceMock->expects($this->any())
  55. ->method('getConnection')
  56. ->willReturn($this->readerAdapterMock);
  57. $this->resourceMock->expects($this->any())
  58. ->method('getMainTable')
  59. ->willReturn('maintable');
  60. $this->resourceMock->expects($this->any())
  61. ->method('getTable')
  62. ->willReturnCallback(function ($table) {
  63. return $table;
  64. });
  65. $this->model = $this->objectManager->getObject(
  66. \Magento\Review\Model\ResourceModel\Review\Collection::class,
  67. [
  68. 'storeManager' => $this->storeManagerMock,
  69. 'resource' => $this->resourceMock,
  70. ]
  71. );
  72. }
  73. public function testInitSelect()
  74. {
  75. $this->selectMock->expects($this->once())
  76. ->method('join')
  77. ->with(
  78. ['detail' => 'review_detail'],
  79. 'main_table.review_id = detail.review_id',
  80. ['detail_id', 'title', 'detail', 'nickname', 'customer_id']
  81. );
  82. $this->objectManager->getObject(
  83. \Magento\Review\Model\ResourceModel\Review\Collection::class,
  84. [
  85. 'storeManager' => $this->storeManagerMock,
  86. 'resource' => $this->resourceMock,
  87. ]
  88. );
  89. }
  90. public function testAddStoreFilter()
  91. {
  92. $this->readerAdapterMock->expects($this->once())
  93. ->method('prepareSqlCondition');
  94. $this->selectMock->expects($this->once())
  95. ->method('join')
  96. ->with(
  97. ['store' => 'review_store'],
  98. 'main_table.review_id=store.review_id',
  99. []
  100. );
  101. $this->model->addStoreFilter(1);
  102. }
  103. /**
  104. * @param int|string $entity
  105. * @param int $pkValue
  106. * @param string $quoteIntoArguments1
  107. * @param string $quoteIntoArguments2
  108. * @param string $quoteIntoReturn1
  109. * @param string $quoteIntoReturn2
  110. * @param int $callNum
  111. * @dataProvider addEntityFilterDataProvider
  112. */
  113. public function testAddEntityFilter(
  114. $entity,
  115. $pkValue,
  116. $quoteIntoArguments1,
  117. $quoteIntoArguments2,
  118. $quoteIntoReturn1,
  119. $quoteIntoReturn2,
  120. $callNum
  121. ) {
  122. $this->readerAdapterMock->expects($this->at(0))
  123. ->method('quoteInto')
  124. ->with($quoteIntoArguments1[0], $quoteIntoArguments1[1])
  125. ->willReturn($quoteIntoReturn1);
  126. $this->readerAdapterMock->expects($this->at(1))
  127. ->method('quoteInto')
  128. ->with($quoteIntoArguments2[0], $quoteIntoArguments2[1])
  129. ->willReturn($quoteIntoReturn2);
  130. $this->selectMock->expects($this->exactly($callNum))
  131. ->method('join')
  132. ->with(
  133. 'review_entity',
  134. 'main_table.entity_id=' . 'review_entity' . '.entity_id',
  135. ['entity_code']
  136. );
  137. $this->model->addEntityFilter($entity, $pkValue);
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function addEntityFilterDataProvider()
  143. {
  144. return [
  145. [
  146. 1,
  147. 2,
  148. ['main_table.entity_id=?', 1],
  149. ['main_table.entity_pk_value=?', 2],
  150. 'quoteIntoReturn1',
  151. 'quoteIntoReturn2',
  152. 0
  153. ],
  154. [
  155. 'entity',
  156. 2,
  157. ['review_entity.entity_code=?', 'entity'],
  158. ['main_table.entity_pk_value=?', 2],
  159. 'quoteIntoReturn1',
  160. 'quoteIntoReturn2',
  161. 1
  162. ]
  163. ];
  164. }
  165. public function testAddReviewsTotalCount()
  166. {
  167. $this->selectMock->expects($this->once())
  168. ->method('joinLeft')
  169. ->with(
  170. ['r' => 'review'],
  171. 'main_table.entity_pk_value = r.entity_pk_value',
  172. ['total_reviews' => new \Zend_Db_Expr('COUNT(r.review_id)')]
  173. )->willReturnSelf();
  174. $this->selectMock->expects($this->once())
  175. ->method('group');
  176. $this->model->addReviewsTotalCount();
  177. }
  178. }