AdapterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\SearchAdapter;
  7. use Magento\Elasticsearch\SearchAdapter\Adapter;
  8. use Magento\Elasticsearch\SearchAdapter\QueryContainer;
  9. use Magento\Elasticsearch\SearchAdapter\QueryContainerFactory;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  11. /**
  12. * Class AdapterTest
  13. */
  14. class AdapterTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var QueryContainerFactory|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $queryContainerFactory;
  20. /**
  21. * @var Adapter
  22. */
  23. protected $model;
  24. /**
  25. * @var \Magento\Elasticsearch\SearchAdapter\ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $connectionManager;
  28. /**
  29. * @var \Magento\Elasticsearch\SearchAdapter\Mapper|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $mapper;
  32. /**
  33. * @var \Magento\Elasticsearch\SearchAdapter\ResponseFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $responseFactory;
  36. /**
  37. * @var \Magento\Framework\Search\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $request;
  40. /**
  41. * @var \Magento\Elasticsearch\SearchAdapter\Aggregation\Builder|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $aggregationBuilder;
  44. /**
  45. * Setup method
  46. * @return void
  47. */
  48. protected function setUp()
  49. {
  50. $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->mapper = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Mapper::class)
  54. ->setMethods([
  55. 'buildQuery',
  56. ])
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->responseFactory = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ResponseFactory::class)
  60. ->setMethods(['create'])
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->request = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->aggregationBuilder = $this->getMockBuilder(
  67. \Magento\Elasticsearch\SearchAdapter\Aggregation\Builder::class
  68. )
  69. ->setMethods([
  70. 'build',
  71. 'setQuery'
  72. ])
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->queryContainerFactory = $this->getMockBuilder(QueryContainerFactory::class)
  76. ->setMethods(['create'])
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $objectManager = new ObjectManagerHelper($this);
  80. $this->model = $objectManager->getObject(
  81. \Magento\Elasticsearch\SearchAdapter\Adapter::class,
  82. [
  83. 'connectionManager' => $this->connectionManager,
  84. 'mapper' => $this->mapper,
  85. 'responseFactory' => $this->responseFactory,
  86. 'aggregationBuilder' => $this->aggregationBuilder,
  87. 'queryContainerFactory' => $this->queryContainerFactory,
  88. ]
  89. );
  90. }
  91. /**
  92. * Test query() method
  93. *
  94. * @return void
  95. */
  96. public function testQuery()
  97. {
  98. $searchQuery = [
  99. 'index' => 'indexName',
  100. 'type' => 'product',
  101. 'body' => [
  102. 'from' => 0,
  103. 'size' => 1000,
  104. 'fields' => ['_id', '_score'],
  105. 'query' => [],
  106. ],
  107. ];
  108. $client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
  109. ->setMethods(['query'])
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->connectionManager->expects($this->once())
  113. ->method('getConnection')
  114. ->willReturn($client);
  115. $queryContainer = $this->getMockBuilder(QueryContainer::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->queryContainerFactory->expects($this->once())
  119. ->method('create')
  120. ->with(['query' => $searchQuery])
  121. ->willReturn($queryContainer);
  122. $this->aggregationBuilder->expects($this->once())
  123. ->method('setQuery')
  124. ->with($queryContainer);
  125. $this->mapper->expects($this->once())
  126. ->method('buildQuery')
  127. ->with($this->request)
  128. ->willReturn($searchQuery);
  129. $client->expects($this->once())
  130. ->method('query')
  131. ->willReturn([
  132. 'hits' => [
  133. 'total' => 1,
  134. 'hits' => [
  135. [
  136. '_index' => 'indexName',
  137. '_type' => 'product',
  138. '_id' => 1,
  139. '_score' => 1.0,
  140. ],
  141. ],
  142. ],
  143. ]);
  144. $this->aggregationBuilder->expects($this->once())
  145. ->method('build')
  146. ->willReturn($client);
  147. $this->model->query($this->request);
  148. }
  149. }