BuilderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Query;
  7. use Magento\Elasticsearch\SearchAdapter\Query\Builder;
  8. use Magento\Framework\Search\RequestInterface;
  9. use Magento\Elasticsearch\Model\Config;
  10. use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
  11. use Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation as AggregationBuilder;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class BuilderTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var Builder
  20. */
  21. protected $model;
  22. /**
  23. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $clientConfig;
  26. /**
  27. * @var SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $searchIndexNameResolver;
  30. /**
  31. * @var AggregationBuilder|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $aggregationBuilder;
  34. /**
  35. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $request;
  38. /**
  39. * @var \Magento\Framework\App\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $scopeResolver;
  42. /**
  43. * @var \Magento\Framework\App\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $scopeInterface;
  46. /**
  47. * Setup method
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
  53. ->setMethods(['getEntityType'])
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->searchIndexNameResolver = $this
  57. ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver::class)
  58. ->setMethods(['getIndexName'])
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->aggregationBuilder = $this
  62. ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation::class)
  63. ->setMethods(['build'])
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->request = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->scopeResolver = $this->getMockForAbstractClass(
  70. \Magento\Framework\App\ScopeResolverInterface::class,
  71. [],
  72. '',
  73. false
  74. );
  75. $this->scopeInterface = $this->getMockForAbstractClass(
  76. \Magento\Framework\App\ScopeInterface::class,
  77. [],
  78. '',
  79. false
  80. );
  81. $objectManagerHelper = new ObjectManagerHelper($this);
  82. $this->model = $objectManagerHelper->getObject(
  83. \Magento\Elasticsearch\SearchAdapter\Query\Builder::class,
  84. [
  85. 'clientConfig' => $this->clientConfig,
  86. 'searchIndexNameResolver' => $this->searchIndexNameResolver,
  87. 'aggregationBuilder' => $this->aggregationBuilder,
  88. 'scopeResolver' => $this->scopeResolver
  89. ]
  90. );
  91. }
  92. /**
  93. * Test initQuery() method
  94. */
  95. public function testInitQuery()
  96. {
  97. $dimensionValue = 1;
  98. $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
  99. ->setMethods(['getValue'])
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->request->expects($this->once())
  103. ->method('getDimensions')
  104. ->willReturn([$dimension]);
  105. $dimension->expects($this->once())
  106. ->method('getValue')
  107. ->willReturn($dimensionValue);
  108. $this->scopeResolver->expects($this->once())
  109. ->method('getScope')
  110. ->willReturn($this->scopeInterface);
  111. $this->scopeInterface->expects($this->once())
  112. ->method('getId')
  113. ->willReturn($dimensionValue);
  114. $this->request->expects($this->once())
  115. ->method('getFrom')
  116. ->willReturn(0);
  117. $this->request->expects($this->once())
  118. ->method('getSize')
  119. ->willReturn(10);
  120. $this->request->expects($this->once())
  121. ->method('getIndex')
  122. ->willReturn('catalogsearch_fulltext');
  123. $this->searchIndexNameResolver->expects($this->once())
  124. ->method('getIndexName')
  125. ->willReturn('indexName');
  126. $this->clientConfig->expects($this->once())
  127. ->method('getEntityType')
  128. ->willReturn('document');
  129. $this->model->initQuery($this->request);
  130. }
  131. /**
  132. * Test initQuery() method
  133. */
  134. public function testInitAggregations()
  135. {
  136. $this->aggregationBuilder->expects($this->any())
  137. ->method('build')
  138. ->willReturn([]);
  139. $result = $this->model->initAggregations($this->request, []);
  140. $this->assertNotNull($result);
  141. }
  142. }