123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Search\Test\Unit\Model;
- use Magento\Search\Helper\Data;
- use Magento\Framework\App\Helper\Context;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Search\Model\QueryFactory;
- use Magento\Framework\Stdlib\StringUtils;
- use Magento\Search\Model\Query;
- /**
- * Class QueryFactoryTest tests Magento\Search\Model\QueryFactory
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class QueryFactoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var QueryFactory
- */
- private $model;
- /**
- * @var Data|\PHPUnit_Framework_MockObject_MockObject
- */
- private $queryHelper;
- /**
- * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $request;
- /**
- * @var StringUtils|\PHPUnit_Framework_MockObject_MockObject
- */
- private $string;
- /**
- * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $objectManager;
- /**
- * @var Query|\PHPUnit_Framework_MockObject_MockObject
- */
- private $query;
- /**
- * SetUp method
- */
- protected function setUp()
- {
- $this->queryHelper = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->request = $this->getMockBuilder(RequestInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->string = $this->getMockBuilder(StringUtils::class)
- ->setMethods(['substr', 'strlen', 'cleanString'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->query = $this->getMockBuilder(Query::class)
- ->setMethods(['setIsQueryTextExceeded', 'setIsQueryTextShort', 'loadByQueryText', 'getId', 'setQueryText'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- /** @var Context|\PHPUnit_Framework_MockObject_MockObject $context */
- $context = $this->getMockBuilder(Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $context->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->request);
- $this->model = (new ObjectManager($this))->getObject(
- QueryFactory::class,
- [
- 'queryHelper' => $this->queryHelper,
- 'context' => $context,
- 'string' => $this->string,
- 'objectManager' => $this->objectManager
- ]
- );
- }
- /**
- * Test for create method
- */
- public function testCreate()
- {
- $data = [1, 2, 3];
- $this->objectManager->expects($this->once())
- ->method('create')
- ->withConsecutive([Query::class, $data])
- ->willReturn($this->query);
- $result = $this->model->create($data);
- $this->assertSame($this->query, $result);
- }
- /**
- * Test for get new query method
- */
- public function testGetNewQuery()
- {
- $queryId = 123;
- $maxQueryLength = 100;
- $minQueryLength = 3;
- $rawQueryText = ' Simple product ';
- $cleanedRawText = 'Simple product';
- $isQueryTextExceeded = false;
- $isQueryTextShort = false;
- $this->mockSetQueryTextNeverExecute($cleanedRawText);
- $this->mockString($cleanedRawText);
- $this->mockQueryLengths($maxQueryLength, $minQueryLength);
- $this->mockGetRawQueryText($rawQueryText);
- $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
- $this->mockCreateQuery();
- $result = $this->model->get();
- $this->assertSame($this->query, $result);
- }
- /**
- * Test for get query twice method
- */
- public function testGetQueryTwice()
- {
- $queryId = 123;
- $maxQueryLength = 100;
- $minQueryLength = 3;
- $rawQueryText = ' Simple product ';
- $cleanedRawText = 'Simple product';
- $isQueryTextExceeded = false;
- $isQueryTextShort = false;
- $this->mockSetQueryTextNeverExecute($cleanedRawText);
- $this->mockString($cleanedRawText);
- $this->mockQueryLengths($maxQueryLength, $minQueryLength);
- $this->mockGetRawQueryText($rawQueryText);
- $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
- $this->mockCreateQuery();
- $result = $this->model->get();
- $this->assertSame($this->query, $result, 'After first execution queries are not same');
- $result = $this->model->get();
- $this->assertSame($this->query, $result, 'After second execution queries are not same');
- }
- /**
- * Test for get query is too long method
- */
- public function testGetTooLongQuery()
- {
- $queryId = 123;
- $maxQueryLength = 8;
- $minQueryLength = 3;
- $rawQueryText = ' Simple product ';
- $cleanedRawText = 'Simple product';
- $subRawText = 'Simple p';
- $isQueryTextExceeded = true;
- $isQueryTextShort = false;
- $this->string->expects($this->any())
- ->method('substr')
- ->withConsecutive([$cleanedRawText, 0, $maxQueryLength])
- ->willReturn($subRawText);
- $this->mockSetQueryTextNeverExecute($cleanedRawText);
- $this->mockString($cleanedRawText);
- $this->mockQueryLengths($maxQueryLength, $minQueryLength);
- $this->mockGetRawQueryText($rawQueryText);
- $this->mockSimpleQuery($subRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
- $this->mockCreateQuery();
- $result = $this->model->get();
- $this->assertSame($this->query, $result);
- }
- /**
- * Test for get query is Short long method
- */
- public function testGetTooShortQuery()
- {
- $queryId = 123;
- $maxQueryLength = 800;
- $minQueryLength = 500;
- $rawQueryText = ' Simple product ';
- $cleanedRawText = 'Simple product';
- $isQueryTextExceeded = false;
- $isQueryTextShort = true;
- $this->mockSetQueryTextNeverExecute($cleanedRawText);
- $this->mockString($cleanedRawText);
- $this->mockQueryLengths($maxQueryLength, $minQueryLength);
- $this->mockGetRawQueryText($rawQueryText);
- $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
- $this->mockCreateQuery();
- $result = $this->model->get();
- $this->assertSame($this->query, $result);
- }
- /**
- * Test for get query is Short long method
- */
- public function testGetQueryWithoutId()
- {
- $queryId = 0;
- $maxQueryLength = 100;
- $minQueryLength = 3;
- $rawQueryText = ' Simple product ';
- $cleanedRawText = 'Simple product';
- $isQueryTextExceeded = false;
- $isQueryTextShort = false;
- $this->mockSetQueryTextOnceExecute($cleanedRawText);
- $this->mockString($cleanedRawText);
- $this->mockQueryLengths($maxQueryLength, $minQueryLength);
- $this->mockGetRawQueryText($rawQueryText);
- $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
- $this->mockCreateQuery();
- $result = $this->model->get();
- $this->assertSame($this->query, $result);
- }
- /**
- * @param int $maxQueryLength
- * @param int $minQueryLength
- * @return void
- */
- private function mockQueryLengths($maxQueryLength, $minQueryLength)
- {
- $this->queryHelper->expects($this->once())
- ->method('getMaxQueryLength')
- ->willReturn($maxQueryLength);
- $this->queryHelper->expects($this->once())
- ->method('getMinQueryLength')
- ->willReturn($minQueryLength);
- }
- /**
- * @param string $rawQueryText
- * @return void
- */
- private function mockGetRawQueryText($rawQueryText)
- {
- $this->request->expects($this->any())
- ->method('getParam')
- ->withConsecutive([QueryFactory::QUERY_VAR_NAME])
- ->willReturn($rawQueryText);
- }
- /**
- * @param string $cleanedRawText
- * @return void
- */
- private function mockString($cleanedRawText)
- {
- $this->string->expects($this->any())
- ->method('cleanString')
- ->withConsecutive([$cleanedRawText])
- ->willReturnArgument(0);
- $this->string->expects($this->any())
- ->method('strlen')
- ->withConsecutive([$cleanedRawText])
- ->willReturn(strlen($cleanedRawText));
- }
- /**
- * @return void
- */
- private function mockCreateQuery()
- {
- $this->objectManager->expects($this->once())
- ->method('create')
- ->withConsecutive([Query::class, []])
- ->willReturn($this->query);
- }
- /**
- * @param string $cleanedRawText
- * @param int $queryId
- * @param bool $isQueryTextExceeded
- * @param bool $isQueryTextShort
- * @return void
- */
- private function mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort)
- {
- $this->query->expects($this->once())
- ->method('loadByQueryText')
- ->withConsecutive([$cleanedRawText])
- ->willReturnSelf();
- $this->query->expects($this->once())
- ->method('getId')
- ->willReturn($queryId);
- $this->query->expects($this->once())
- ->method('setIsQueryTextExceeded')
- ->withConsecutive([$isQueryTextExceeded]);
- $this->query->expects($this->once())
- ->method('setIsQueryTextShort')
- ->withConsecutive([$isQueryTextShort]);
- }
- /**
- * @param string $cleanedRawText
- * @return void
- */
- private function mockSetQueryTextNeverExecute($cleanedRawText)
- {
- $this->query->expects($this->never())
- ->method('setQueryText')
- ->withConsecutive([$cleanedRawText])
- ->willReturnSelf();
- }
- /**
- * @param string $cleanedRawText
- * @return void
- */
- private function mockSetQueryTextOnceExecute($cleanedRawText)
- {
- $this->query->expects($this->once())
- ->method('setQueryText')
- ->withConsecutive([$cleanedRawText])
- ->willReturnSelf();
- }
- }
|