GenericMapperTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 GenericMapperTest
  9. */
  10. class GenericMapperTest 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\DB\MapperFactory|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $mapperFactoryMock;
  20. /**
  21. * @var \Magento\Framework\DB\GenericMapper
  22. */
  23. protected $geneticMapper;
  24. /**
  25. * Set up
  26. *
  27. * @return void
  28. */
  29. protected function setUp()
  30. {
  31. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $this->selectMock = $this->createPartialMock(
  33. \Magento\Framework\DB\Select::class,
  34. ['orWhere', 'where', 'setPart', 'getPart']
  35. );
  36. $this->mapperFactoryMock = $this->createPartialMock(\Magento\Framework\DB\MapperFactory::class, ['create']);
  37. $this->geneticMapper = $objectManager->getObject(
  38. \Magento\Framework\DB\GenericMapper::class,
  39. [
  40. 'select' => $this->selectMock,
  41. 'mapperFactory' => $this->mapperFactoryMock,
  42. ]
  43. );
  44. }
  45. /**
  46. * Run test mapCriteriaList method
  47. *
  48. * @return void
  49. */
  50. public function testMapCriteriaList()
  51. {
  52. $criteriaMock = $this->getMockForAbstractClass(
  53. \Magento\Framework\Api\CriteriaInterface::class,
  54. [],
  55. '',
  56. false,
  57. true,
  58. true,
  59. ['getMapperInterfaceName']
  60. );
  61. $mapperInstanceMock = $this->getMockForAbstractClass(
  62. \Magento\Framework\DB\MapperInterface::class,
  63. [],
  64. '',
  65. false,
  66. true,
  67. true,
  68. ['map']
  69. );
  70. $criteriaMock->expects($this->any())
  71. ->method('getMapperInterfaceName')
  72. ->will($this->returnValue('mapper-name'));
  73. $this->mapperFactoryMock->expects($this->exactly(4))
  74. ->method('create')
  75. ->with('mapper-name', ['select' => $this->selectMock])
  76. ->will($this->returnValue($mapperInstanceMock));
  77. $mapperInstanceMock->expects($this->exactly(4))
  78. ->method('map')
  79. ->will($this->returnValue($this->selectMock));
  80. $this->geneticMapper->mapCriteriaList(array_fill(0, 4, $criteriaMock));
  81. }
  82. /**
  83. * Run test mapFilters method
  84. *
  85. * @return void
  86. */
  87. public function testMapFilters()
  88. {
  89. $filters = [
  90. [
  91. 'type' => 'or',
  92. 'field' => 'test-field',
  93. 'condition' => 'test-condition',
  94. ],
  95. [
  96. 'type' => 'string',
  97. 'field' => 'test-field',
  98. 'condition' => 'test-condition'
  99. ],
  100. [
  101. 'type' => 'public',
  102. 'field' => 'test-field',
  103. 'condition' => 'test-condition'
  104. ],
  105. [
  106. 'type' => 'default',
  107. 'field' => 'test-field',
  108. 'condition' => 'test-condition'
  109. ],
  110. ];
  111. $connectionMock = $this->getMockForAbstractClass(
  112. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  113. [],
  114. '',
  115. false,
  116. true,
  117. true,
  118. ['quoteInto', 'prepareSqlCondition']
  119. );
  120. /** @var \Magento\Framework\DB\GenericMapper|\PHPUnit_Framework_MockObject_MockObject $geneticMapper */
  121. $geneticMapper =
  122. $this->createPartialMock(\Magento\Framework\DB\GenericMapper::class, ['getConnection', 'getSelect']);
  123. $geneticMapper->expects($this->any())
  124. ->method('getConnection')
  125. ->will($this->returnValue($connectionMock));
  126. $geneticMapper->expects($this->exactly(4))
  127. ->method('getSelect')
  128. ->will($this->returnValue($this->selectMock));
  129. $connectionMock->expects($this->exactly(2))
  130. ->method('quoteInto')
  131. ->with('test-field=?', 'test-condition')
  132. ->will($this->returnValue('test-condition'));
  133. $this->selectMock->expects($this->once())
  134. ->method('orWhere')
  135. ->with('test-condition');
  136. $this->selectMock->expects($this->exactly(3))
  137. ->method('where')
  138. ->with('test-condition');
  139. $connectionMock->expects($this->any())
  140. ->method('prepareSqlCondition')
  141. ->with('test-field', 'test-condition')
  142. ->will($this->returnValue('test-condition'));
  143. $geneticMapper->mapFilters($filters);
  144. }
  145. /**
  146. * Run test mapFields method
  147. *
  148. * @return void
  149. */
  150. public function testMapFields()
  151. {
  152. $fields = [
  153. [
  154. 'test-correlation-name',
  155. 'test-field',
  156. 'test-alias',
  157. ],
  158. [
  159. 'test-correlation-name',
  160. 'test-field',
  161. null
  162. ],
  163. [
  164. 'test-correlation-name',
  165. 'test-field',
  166. 'test-alias-unique'
  167. ],
  168. ];
  169. /** @var \Magento\Framework\DB\GenericMapper|\PHPUnit_Framework_MockObject_MockObject $geneticMapper */
  170. $geneticMapper = $this->createPartialMock(\Magento\Framework\DB\GenericMapper::class, ['getSelect']);
  171. $geneticMapper->expects($this->any())
  172. ->method('getSelect')
  173. ->will($this->returnValue($this->selectMock));
  174. $this->selectMock->expects($this->once())
  175. ->method('getPart')
  176. ->with(\Magento\Framework\DB\Select::COLUMNS)
  177. ->willReturn([]);
  178. $this->selectMock->expects($this->once())
  179. ->method('setPart')
  180. ->with(\Magento\Framework\DB\Select::COLUMNS, $this->equalTo($fields));
  181. $geneticMapper->mapFields($fields);
  182. }
  183. }