123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Elasticsearch\Test\Unit\SearchAdapter\Dynamic;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DataProviderTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Elasticsearch\SearchAdapter\QueryContainer|\PHPUnit_Framework_MockObject_MockObject
- */
- private $queryContainer;
- /**
- * @var \Magento\Elasticsearch\SearchAdapter\Dynamic\DataProvider
- */
- protected $model;
- /**
- * @var \Magento\Elasticsearch\SearchAdapter\ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $connectionManager;
- /**
- * @var \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $fieldMapper;
- /**
- * @var \Magento\Catalog\Model\Layer\Filter\Price\Range|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $range;
- /**
- * @var \Magento\Framework\Search\Dynamic\IntervalFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $intervalFactory;
- /**
- * @var \Magento\Elasticsearch\Model\Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $clientConfig;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManager;
- /**
- * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerSession;
- /**
- * @var \Magento\Framework\Search\Dynamic\EntityStorage|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $entityStorage;
- /**
- * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeMock;
- /**
- * @var \Magento\Elasticsearch\Model\Client\Elasticsearch|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $clientMock;
- /**
- * @var \Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $searchIndexNameResolver;
- /**
- * @var \Magento\Framework\App\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeResolver;
- /**
- * @var \Magento\Framework\App\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeInterface;
- /**
- * A private helper for setUp method.
- * @return void
- */
- private function setUpMockObjects()
- {
- $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
- ->setMethods(['getConnection'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->range = $this->getMockBuilder(\Magento\Catalog\Model\Layer\Filter\Price\Range::class)
- ->setMethods(['getPriceRange'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->intervalFactory = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\IntervalFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->clientConfig = $this->getMockBuilder(\Magento\Elasticsearch\Model\Config::class)
- ->setMethods([
- 'getIndexName',
- 'getEntityType',
- ])
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
- ->setMethods(['getCustomerGroupId'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->entityStorage = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\EntityStorage::class)
- ->setMethods(['getSource'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->entityStorage->expects($this->any())
- ->method('getSource')
- ->willReturn([1]);
- $this->customerSession->expects($this->any())
- ->method('getCustomerGroupId')
- ->willReturn(1);
- $this->storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManager->expects($this->any())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->storeMock->expects($this->any())
- ->method('getWebsiteId')
- ->willReturn(1);
- $this->storeMock->expects($this->any())
- ->method('getId')
- ->willReturn(1);
- $this->clientConfig->expects($this->any())
- ->method('getIndexName')
- ->willReturn('indexName');
- $this->clientConfig->expects($this->any())
- ->method('getEntityType')
- ->willReturn('product');
- $this->clientMock = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
- ->setMethods(['query'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->connectionManager->expects($this->any())
- ->method('getConnection')
- ->willReturn($this->clientMock);
- $this->fieldMapper = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->searchIndexNameResolver = $this
- ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->scopeResolver = $this->getMockForAbstractClass(
- \Magento\Framework\App\ScopeResolverInterface::class,
- [],
- '',
- false
- );
- $this->scopeInterface = $this->getMockForAbstractClass(
- \Magento\Framework\App\ScopeInterface::class,
- [],
- '',
- false
- );
- $this->queryContainer = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\QueryContainer::class)
- ->disableOriginalConstructor()
- ->setMethods(['getQuery'])
- ->getMock();
- }
- /**
- * Setup method
- * @return void
- */
- protected function setUp()
- {
- $this->setUpMockObjects();
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $objectManagerHelper->getObject(
- \Magento\Elasticsearch\SearchAdapter\Dynamic\DataProvider::class,
- [
- 'connectionManager' => $this->connectionManager,
- 'fieldMapper' => $this->fieldMapper,
- 'range' => $this->range,
- 'intervalFactory' => $this->intervalFactory,
- 'clientConfig' => $this->clientConfig,
- 'storeManager' => $this->storeManager,
- 'customerSession' => $this->customerSession,
- 'searchIndexNameResolver' => $this->searchIndexNameResolver,
- 'indexerId' => 'catalogsearch_fulltext',
- 'scopeResolver' => $this->scopeResolver,
- 'queryContainer' => $this->queryContainer,
- ]
- );
- }
- /**
- * Test getRange() method
- */
- public function testGetRange()
- {
- $this->range->expects($this->once())
- ->method('getPriceRange')
- ->willReturn([]);
- $this->assertEquals(
- [],
- $this->model->getRange()
- );
- }
- /**
- * Test getAggregations() method
- */
- public function testGetAggregations()
- {
- $expectedResult = [
- 'count' => 1,
- 'max' => 1,
- 'min' => 1,
- 'std' => 1,
- ];
- $this->clientMock->expects($this->once())
- ->method('query')
- ->willReturn([
- 'aggregations' => [
- 'prices' => [
- 'count' => 1,
- 'max' => 1,
- 'min' => 1,
- 'std_deviation' => 1,
- ],
- ],
- ]);
- $this->queryContainer->expects($this->once())
- ->method('getQuery')
- ->willReturn([]);
- $this->assertEquals(
- $expectedResult,
- $this->model->getAggregations($this->entityStorage)
- );
- }
- /**
- * Test getInterval() method
- */
- public function testGetInterval()
- {
- $dimensionValue = 1;
- $bucket = $this->getMockBuilder(\Magento\Framework\Search\Request\BucketInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $interval = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\IntervalInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
- ->setMethods(['getValue'])
- ->disableOriginalConstructor()
- ->getMock();
- $dimension->expects($this->once())
- ->method('getValue')
- ->willReturn($dimensionValue);
- $this->scopeResolver->expects($this->once())
- ->method('getScope')
- ->willReturn($this->scopeInterface);
- $this->scopeInterface->expects($this->once())
- ->method('getId')
- ->willReturn($dimensionValue);
- $this->intervalFactory->expects($this->once())
- ->method('create')
- ->willReturn($interval);
- $this->assertEquals(
- $interval,
- $this->model->getInterval(
- $bucket,
- [$dimension],
- $this->entityStorage
- )
- );
- }
- /**
- * Test getAggregation() method
- */
- public function testGetAggregation()
- {
- $expectedResult = [
- 1 => 1,
- ];
- $bucket = $this->getMockBuilder(\Magento\Framework\Search\Request\BucketInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
- ->setMethods(['getValue'])
- ->disableOriginalConstructor()
- ->getMock();
- $dimension->expects($this->never())
- ->method('getValue');
- $this->scopeResolver->expects($this->never())
- ->method('getScope');
- $this->scopeInterface->expects($this->never())
- ->method('getId');
- $this->clientMock->expects($this->once())
- ->method('query')
- ->with($this->callback(function ($query) {
- $histogramParams = $query['body']['aggregations']['prices']['histogram'];
- // Assert the interval is queried as a float. See MAGETWO-95471
- if ($histogramParams['interval'] !== 10.0) {
- return false;
- }
- if (!isset($histogramParams['min_doc_count']) || $histogramParams['min_doc_count'] !== 1) {
- return false;
- }
- return true;
- }))
- ->willReturn([
- 'aggregations' => [
- 'prices' => [
- 'buckets' => [
- [
- 'key' => 1,
- 'doc_count' => 1,
- ],
- ],
- ],
- ],
- ]);
- $this->queryContainer->expects($this->once())
- ->method('getQuery')
- ->willReturn([]);
- $this->assertEquals(
- $expectedResult,
- $this->model->getAggregation(
- $bucket,
- [$dimension],
- 10,
- $this->entityStorage
- )
- );
- }
- /**
- * Test prepareData() method
- */
- public function testPrepareData()
- {
- $expectedResult = [
- [
- 'from' => '',
- 'to' => 10,
- 'count' => 1,
- ],
- [
- 'from' => 10,
- 'to' => '',
- 'count' => 1,
- ],
- ];
- $this->assertEquals(
- $expectedResult,
- $this->model->prepareData(
- 10,
- [
- 1 => 1,
- 2 => 1,
- ]
- )
- );
- }
- }
|