123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Elasticsearch\Test\Unit\Elasticsearch5\SearchAdapter\Aggregation;
- use Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
- use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Customer\Model\Session as CustomerSession;
- use Magento\Elasticsearch\Model\Config;
- use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch as ElasticsearchClient;
- use Magento\Store\Api\Data\StoreInterface;
- use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
- /**
- * Test for Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval class.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class IntervalTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Interval
- */
- private $model;
- /**
- * @var ConnectionManager|\PHPUnit_Framework_MockObject_MockObject
- */
- private $connectionManager;
- /**
- * @var FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $fieldMapper;
- /**
- * @var Config|\PHPUnit_Framework_MockObject_MockObject
- */
- private $clientConfig;
- /**
- * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManager;
- /**
- * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
- */
- private $customerSession;
- /**
- * @var ElasticsearchClient|\PHPUnit_Framework_MockObject_MockObject
- */
- private $clientMock;
- /**
- * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeMock;
- /**
- * @var SearchIndexNameResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- private $searchIndexNameResolver;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $this->connectionManager = $this->getMockBuilder(ConnectionManager::class)
- ->setMethods(['getConnection'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->fieldMapper = $this->getMockBuilder(FieldMapperInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->clientConfig = $this->getMockBuilder(Config::class)
- ->setMethods([
- 'getIndexName',
- 'getEntityType',
- ])
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerSession = $this->getMockBuilder(CustomerSession::class)
- ->setMethods(['getCustomerGroupId'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerSession->expects($this->any())
- ->method('getCustomerGroupId')
- ->willReturn(1);
- $this->storeMock = $this->getMockBuilder(StoreInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->searchIndexNameResolver = $this
- ->getMockBuilder(SearchIndexNameResolver::class)
- ->disableOriginalConstructor()
- ->getMock();
- $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(ElasticsearchClient::class)
- ->setMethods(['query'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->connectionManager->expects($this->any())
- ->method('getConnection')
- ->willReturn($this->clientMock);
- $objectManagerHelper = new ObjectManagerHelper($this);
- $this->model = $objectManagerHelper->getObject(
- Interval::class,
- [
- 'connectionManager' => $this->connectionManager,
- 'fieldMapper' => $this->fieldMapper,
- 'clientConfig' => $this->clientConfig,
- 'searchIndexNameResolver' => $this->searchIndexNameResolver,
- 'fieldName' => 'price_0_1',
- 'storeId' => 1,
- 'entityIds' => [265, 313, 281],
- ]
- );
- }
- /**
- * @dataProvider loadParamsProvider
- * @param string $limit
- * @param string $offset
- * @param string $lower
- * @param string $upper
- * @param array $queryResult
- * @param array $expected
- * @return void
- */
- public function testLoad(
- string $limit,
- string $offset,
- string $lower,
- string $upper,
- array $queryResult,
- array $expected
- ): void {
- $this->processQuery($queryResult);
- $this->assertEquals(
- $expected,
- $this->model->load($limit, $offset, $lower, $upper)
- );
- }
- /**
- * @dataProvider loadPrevParamsProvider
- * @param string $data
- * @param string $index
- * @param string $lower
- * @param array $queryResult
- * @param array|bool $expected
- * @return void
- */
- public function testLoadPrev(string $data, string $index, string $lower, array $queryResult, $expected): void
- {
- $this->processQuery($queryResult);
- $this->assertEquals(
- $expected,
- $this->model->loadPrevious($data, $index, $lower)
- );
- }
- /**
- * @dataProvider loadNextParamsProvider
- * @param string $data
- * @param string $rightIndex
- * @param string $upper
- * @param array $queryResult
- * @param array|bool $expected
- * @return void
- */
- public function testLoadNext(string $data, string $rightIndex, string $upper, array $queryResult, $expected): void
- {
- $this->processQuery($queryResult);
- $this->assertEquals(
- $expected,
- $this->model->loadNext($data, $rightIndex, $upper)
- );
- }
- /**
- * @param array $queryResult
- * @return void
- */
- private function processQuery(array $queryResult): void
- {
- $this->searchIndexNameResolver->expects($this->any())
- ->method('getIndexName')
- ->willReturn('magento2_product_1');
- $this->clientConfig->expects($this->any())
- ->method('getEntityType')
- ->willReturn('document');
- $this->clientMock->expects($this->any())
- ->method('query')
- ->willReturn($queryResult);
- }
- /**
- * @return array
- */
- public function loadParamsProvider(): array
- {
- return [
- [
- 'limit' => '6',
- 'offset' => '2',
- 'lower' => '24',
- 'upper' => '42',
- 'queryResult' => [
- 'hits' => [
- 'hits' => [
- [
- 'fields' => [
- 'price_0_1' => [25],
- ],
- ],
- ],
- ],
- ],
- 'expected' => [25],
- ],
- ];
- }
- /**
- * @return array
- */
- public function loadPrevParamsProvider(): array
- {
- return [
- [
- 'data' => '24',
- 'rightIndex' => '1',
- 'upper' => '24',
- 'queryResult' => [
- 'hits' => [
- 'total'=> '1',
- 'hits' => [
- [
- 'fields' => [
- 'price_0_1' => ['25'],
- ],
- ],
- ],
- ],
- ],
- 'expected' => ['25.0'],
- ],
- [
- 'data' => '24',
- 'rightIndex' => '1',
- 'upper' => '24',
- 'queryResult' => [
- 'hits' => ['total'=> '0'],
- ],
- 'expected' => false,
- ],
- ];
- }
- /**
- * @return array
- */
- public function loadNextParamsProvider(): array
- {
- return [
- [
- 'data' => '24',
- 'rightIndex' => '2',
- 'upper' => '42',
- 'queryResult' => [
- 'hits' => [
- 'total'=> '1',
- 'hits' => [
- [
- 'fields' => [
- 'price_0_1' => ['25'],
- ],
- ],
- ],
- ],
- ],
- 'expected' => ['25.0'],
- ],
- [
- 'data' => '24',
- 'rightIndex' => '2',
- 'upper' => '42',
- 'queryResult' => [
- 'hits' => ['total'=> '0'],
- ],
- 'expected' => false,
- ],
- ];
- }
- }
|