AggregationTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Builder;
  7. use Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation;
  8. use Magento\Framework\Search\Request\BucketInterface;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. class AggregationTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Aggregation
  14. */
  15. protected $model;
  16. /**
  17. * @var \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $fieldMapper;
  20. /**
  21. * @var \Magento\Framework\Search\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $requestInterface;
  24. /**
  25. * @var BucketInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $requestBucketInterface;
  28. /**
  29. * Set up test environment.
  30. *
  31. * @return void
  32. */
  33. protected function setUp()
  34. {
  35. $helper = new ObjectManager($this);
  36. $this->fieldMapper = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->requestInterface = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->requestBucketInterface = $this->getMockBuilder(BucketInterface::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->model = $helper->getObject(
  46. \Magento\Elasticsearch\SearchAdapter\Query\Builder\Aggregation::class,
  47. [
  48. 'fieldMapper' =>$this->fieldMapper,
  49. ]
  50. );
  51. }
  52. /**
  53. * Test build() method "dynamicBucket" with field "price"
  54. */
  55. public function testBuildDynamicPrice()
  56. {
  57. $query = [
  58. 'index' => 'magento2',
  59. 'type' => 'product',
  60. 'body' => [],
  61. ];
  62. $this->requestInterface->expects($this->any())
  63. ->method('getAggregation')
  64. ->willReturn([$this->requestBucketInterface]);
  65. $this->fieldMapper->expects($this->any())
  66. ->method('getFieldName')
  67. ->willReturn('price');
  68. $this->requestBucketInterface->expects($this->any())
  69. ->method('getField')
  70. ->willReturn('price');
  71. $this->requestBucketInterface->expects($this->any())
  72. ->method('getType')
  73. ->willReturn('dynamicBucket');
  74. $this->requestBucketInterface->expects($this->any())
  75. ->method('getName')
  76. ->willReturn('price_bucket');
  77. $result = $this->model->build($this->requestInterface, $query);
  78. $this->assertNotNull($result);
  79. }
  80. /**
  81. * Test build() method "dynamicBucket"
  82. */
  83. public function testBuildDynamic()
  84. {
  85. $query = [
  86. 'index' => 'magento2',
  87. 'type' => 'product',
  88. 'body' => [],
  89. ];
  90. $this->requestInterface->expects($this->any())
  91. ->method('getAggregation')
  92. ->willReturn([$this->requestBucketInterface]);
  93. $this->fieldMapper->expects($this->any())
  94. ->method('getFieldName')
  95. ->willReturn('field');
  96. $this->requestBucketInterface->expects($this->any())
  97. ->method('getField')
  98. ->willReturn('field');
  99. $this->requestBucketInterface->expects($this->any())
  100. ->method('getType')
  101. ->willReturn('dynamicBucket');
  102. $this->requestBucketInterface->expects($this->any())
  103. ->method('getName')
  104. ->willReturn('price_bucket');
  105. $result = $this->model->build($this->requestInterface, $query);
  106. $this->assertNotNull($result);
  107. }
  108. /**
  109. * Test build() method "dynamicBucket"
  110. */
  111. public function testBuildTerm()
  112. {
  113. $query = [
  114. 'index' => 'magento2',
  115. 'type' => 'product',
  116. 'body' => [],
  117. ];
  118. $bucketName = 'price_bucket';
  119. $this->requestInterface
  120. ->method('getAggregation')
  121. ->willReturn([$this->requestBucketInterface]);
  122. $this->fieldMapper
  123. ->method('getFieldName')
  124. ->willReturn('price');
  125. $this->requestBucketInterface
  126. ->method('getField')
  127. ->willReturn('price');
  128. $this->requestBucketInterface
  129. ->method('getType')
  130. ->willReturn(BucketInterface::TYPE_TERM);
  131. $this->requestBucketInterface
  132. ->method('getName')
  133. ->willReturn($bucketName);
  134. $result = $this->model->build($this->requestInterface, $query);
  135. $this->assertNotNull($result);
  136. $this->assertTrue(
  137. isset($result['body']['aggregations'][$bucketName]['terms']['size']),
  138. 'The size have to be specified since by default, ' .
  139. 'the terms aggregation will return only the buckets for the top ten terms ordered by the doc_count'
  140. );
  141. }
  142. }