ResponseFactoryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  7. use Magento\Elasticsearch\SearchAdapter\ResponseFactory;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class ResponseFactoryTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var ResponseFactory|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $model;
  15. /**
  16. * @var \Magento\Elasticsearch\SearchAdapter\DocumentFactory|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $documentFactory;
  19. /**
  20. * @var \Magento\Elasticsearch\SearchAdapter\AggregationFactory|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $aggregationFactory;
  23. /**
  24. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $objectManager;
  27. /**
  28. * Set up test environment.
  29. *
  30. * @return void
  31. */
  32. protected function setUp()
  33. {
  34. $this->documentFactory = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\DocumentFactory::class)
  35. ->setMethods(['create'])
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->aggregationFactory = $this->getMockBuilder(
  39. \Magento\Elasticsearch\SearchAdapter\AggregationFactory::class
  40. )
  41. ->setMethods(['create'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  45. $objectManagerHelper = new ObjectManagerHelper($this);
  46. $this->model = $objectManagerHelper->getObject(
  47. \Magento\Elasticsearch\SearchAdapter\ResponseFactory::class,
  48. [
  49. 'objectManager' => $this->objectManager,
  50. 'documentFactory' => $this->documentFactory,
  51. 'aggregationFactory' => $this->aggregationFactory
  52. ]
  53. );
  54. }
  55. public function testCreate()
  56. {
  57. $documents = [
  58. ['title' => 'oneTitle', 'description' => 'oneDescription'],
  59. ['title' => 'twoTitle', 'description' => 'twoDescription'],
  60. ];
  61. $aggregations = [
  62. 'aggregation1' => [
  63. 'itemOne' => 10,
  64. 'itemTwo' => 20,
  65. ],
  66. 'aggregation2' => [
  67. 'itemOne' => 5,
  68. 'itemTwo' => 45,
  69. ]
  70. ];
  71. $rawResponse = ['documents' => $documents, 'aggregations' => $aggregations];
  72. $exceptedResponse = [
  73. 'documents' => [
  74. [
  75. ['name' => 'title', 'value' => 'oneTitle'],
  76. ['name' => 'description', 'value' => 'oneDescription'],
  77. ],
  78. [
  79. ['name' => 'title', 'value' => 'twoTitle'],
  80. ['name' => 'description', 'value' => 'twoDescription'],
  81. ],
  82. ],
  83. 'aggregations' => [
  84. 'aggregation1' => [
  85. 'itemOne' => 10,
  86. 'itemTwo' => 20
  87. ],
  88. 'aggregation2' => [
  89. 'itemOne' => 5,
  90. 'itemTwo' => 45
  91. ],
  92. ],
  93. ];
  94. $this->documentFactory->expects($this->at(0))->method('create')
  95. ->with($this->equalTo($documents[0]))
  96. ->will($this->returnValue('document1'));
  97. $this->documentFactory->expects($this->at(1))->method('create')
  98. ->with($documents[1])
  99. ->will($this->returnValue('document2'));
  100. $this->aggregationFactory->expects($this->at(0))->method('create')
  101. ->with($this->equalTo($exceptedResponse['aggregations']))
  102. ->will($this->returnValue('aggregationsData'));
  103. $this->objectManager->expects($this->once())->method('create')
  104. ->with(
  105. $this->equalTo(\Magento\Framework\Search\Response\QueryResponse::class),
  106. $this->equalTo(['documents' => ['document1', 'document2'], 'aggregations' => 'aggregationsData'])
  107. )
  108. ->will($this->returnValue('QueryResponseObject'));
  109. $result = $this->model->create($rawResponse);
  110. $this->assertEquals('QueryResponseObject', $result);
  111. }
  112. }