EngineTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Test\Unit\Model\ResourceModel;
  7. use Magento\Elasticsearch\Model\ResourceModel\Engine;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class EngineTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var Engine
  13. */
  14. private $model;
  15. /**
  16. * @var \Magento\Catalog\Model\Product\Visibility|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $catalogProductVisibility;
  19. /**
  20. * @var \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $indexScopeResolver;
  23. /**
  24. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $connection;
  27. /**
  28. * Setup
  29. *
  30. * @return void
  31. */
  32. protected function setUp()
  33. {
  34. $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  35. ->disableOriginalConstructor()
  36. ->setMethods(['getIfNullSql'])
  37. ->getMockForAbstractClass();
  38. $resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  39. ->disableOriginalConstructor()
  40. ->setMethods(['getConnection', 'getTableName'])
  41. ->getMock();
  42. $resource->expects($this->any())
  43. ->method('getConnection')
  44. ->will($this->returnValue($this->connection));
  45. $resource->expects($this->any())
  46. ->method('getTableName')
  47. ->will($this->returnArgument(0));
  48. $this->catalogProductVisibility = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class)
  49. ->disableOriginalConstructor()
  50. ->setMethods(['getVisibleInSiteIds'])
  51. ->getMock();
  52. $this->indexScopeResolver = $this->getMockBuilder(
  53. \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver::class
  54. )
  55. ->disableOriginalConstructor()
  56. ->setMethods(['getVisibleInSiteIds'])
  57. ->getMock();
  58. $objectManager = new ObjectManagerHelper($this);
  59. $this->model = $objectManager->getObject(
  60. \Magento\Elasticsearch\Model\ResourceModel\Engine::class,
  61. [
  62. 'catalogProductVisibility' => $this->catalogProductVisibility,
  63. 'indexScopeResolver' => $this->indexScopeResolver
  64. ]
  65. );
  66. }
  67. /**
  68. * @param null|string $expected
  69. * @param array $data
  70. * @dataProvider prepareEntityIndexDataProvider
  71. */
  72. public function testPrepareEntityIndex($expected, array $data)
  73. {
  74. $this->assertEquals($expected, $this->model->prepareEntityIndex($data['index'], $data['separator']));
  75. }
  76. /**
  77. * Test allowAdvancedIndex method
  78. */
  79. public function testAllowAdvancedIndex()
  80. {
  81. $this->assertEquals(false, $this->model->allowAdvancedIndex());
  82. }
  83. /**
  84. * Test isAvailable method
  85. */
  86. public function testIsAvailable()
  87. {
  88. $this->assertEquals(true, $this->model->isAvailable());
  89. }
  90. /**
  91. * Test getAllowedVisibility method
  92. * Will return getVisibleInSiteIds array
  93. */
  94. public function testGetAllowedVisibility()
  95. {
  96. $this->catalogProductVisibility->expects($this->once())
  97. ->method('getVisibleInSiteIds')
  98. ->willReturn([3, 2, 4]);
  99. $this->assertEquals([3, 2, 4], $this->model->getAllowedVisibility());
  100. }
  101. /**
  102. * Test processAttributeValue method
  103. */
  104. public function testProcessAttributeValue()
  105. {
  106. $this->assertEquals(1, $this->model->processAttributeValue('attribute', 1));
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function prepareEntityIndexDataProvider()
  112. {
  113. return [
  114. [
  115. [],
  116. [
  117. 'index' => [],
  118. 'separator' => ' ',
  119. ],
  120. ],
  121. ];
  122. }
  123. }