QueryTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Test\Unit;
  7. /**
  8. * Class QueryTest
  9. */
  10. class QueryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $selectMock;
  16. /**
  17. * @var \Magento\Framework\Api\CriteriaInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $criteriaMock;
  20. /**
  21. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $resourceMock;
  24. /**
  25. * @var \Zend_Db_Statement_Pdo|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $fetchStmtMock;
  28. /**
  29. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $loggerMock;
  32. /**
  33. * @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $fetchStrategyMock;
  36. /**
  37. * @var \Magento\Framework\DB\Query
  38. */
  39. protected $query;
  40. /**
  41. * Set up
  42. *
  43. * @return void
  44. */
  45. protected function setUp()
  46. {
  47. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  48. $this->selectMock =
  49. $this->createPartialMock(\Magento\Framework\DB\Select::class, ['reset', 'columns', 'getConnection']);
  50. $this->criteriaMock = $this->getMockForAbstractClass(
  51. \Magento\Framework\Api\CriteriaInterface::class,
  52. [],
  53. '',
  54. false,
  55. true,
  56. true,
  57. []
  58. );
  59. $this->resourceMock = $this->getMockForAbstractClass(
  60. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  61. [],
  62. '',
  63. false,
  64. true,
  65. true,
  66. ['getIdFieldName']
  67. );
  68. $this->fetchStmtMock = $this->createPartialMock(\Zend_Db_Statement_Pdo::class, ['fetch']);
  69. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  70. $this->fetchStrategyMock = $this->getMockForAbstractClass(
  71. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface::class,
  72. [],
  73. '',
  74. false,
  75. true,
  76. true,
  77. []
  78. );
  79. $this->query = $objectManager->getObject(
  80. \Magento\Framework\DB\Query::class,
  81. [
  82. 'select' => $this->selectMock,
  83. 'criteria' => $this->criteriaMock,
  84. 'resource' => $this->resourceMock,
  85. 'fetchStrategy' => $this->fetchStrategyMock
  86. ]
  87. );
  88. }
  89. /**
  90. * Run test getAllIds method
  91. *
  92. * @return void
  93. */
  94. public function testGetAllIds()
  95. {
  96. $adapterMock = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['fetchCol']);
  97. $this->resourceMock->expects($this->once())
  98. ->method('getIdFieldName')
  99. ->will($this->returnValue('return-value'));
  100. $this->selectMock->expects($this->once())
  101. ->method('getConnection')
  102. ->will($this->returnValue($adapterMock));
  103. $adapterMock->expects($this->once())
  104. ->method('fetchCol')
  105. ->will($this->returnValue('fetch-result'));
  106. $this->assertEquals('fetch-result', $this->query->getAllIds());
  107. }
  108. /**
  109. * Run test getSize method
  110. *
  111. * @return void
  112. */
  113. public function testGetSize()
  114. {
  115. $adapterMock = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['fetchOne']);
  116. $this->selectMock->expects($this->once())
  117. ->method('columns')
  118. ->with('COUNT(*)');
  119. $this->selectMock->expects($this->once())
  120. ->method('getConnection')
  121. ->will($this->returnValue($adapterMock));
  122. $adapterMock->expects($this->once())
  123. ->method('fetchOne')
  124. ->will($this->returnValue(10.689));
  125. $this->assertEquals(10, $this->query->getSize());
  126. }
  127. /**
  128. * Run test fetchAll method
  129. *
  130. * @return void
  131. */
  132. public function testFetchAll()
  133. {
  134. $this->fetchStrategyMock->expects($this->once())
  135. ->method('fetchAll')
  136. ->will($this->returnValue('return-value'));
  137. $this->assertEquals('return-value', $this->query->fetchAll());
  138. }
  139. /**
  140. * Run test fetchItem method
  141. *
  142. * @return void
  143. */
  144. public function testFetchItem()
  145. {
  146. $adapterMock = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, ['query']);
  147. $this->selectMock->expects($this->once())
  148. ->method('getConnection')
  149. ->will($this->returnValue($adapterMock));
  150. $adapterMock->expects($this->once())
  151. ->method('query')
  152. ->will($this->returnValue($this->fetchStmtMock));
  153. $this->fetchStmtMock->expects($this->once())
  154. ->method('fetch')
  155. ->will($this->returnValue(null));
  156. $this->assertEquals([], $this->query->fetchItem());
  157. }
  158. }