FieldMapperResolverTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Adapter\FieldMapper;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Elasticsearch\Model\Adapter\FieldMapper\FieldMapperResolver;
  9. use Magento\Framework\ObjectManagerInterface;
  10. class FieldMapperResolverTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var FieldMapperResolver
  14. */
  15. private $model;
  16. /**
  17. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $objectManagerMock;
  20. /**
  21. * @var string[]
  22. */
  23. private $fieldMappers;
  24. /**
  25. * @var FieldMapperInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $fieldMapperEntity;
  28. /**
  29. * Set up test environment
  30. *
  31. * @return void
  32. */
  33. protected function setUp()
  34. {
  35. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->fieldMapperEntity = $this->getMockBuilder(
  39. \Magento\Elasticsearch\Model\Adapter\FieldMapperInterface::class
  40. )
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->fieldMappers = [
  44. 'product' => 'productFieldMapper',
  45. ];
  46. $objectManager = new ObjectManagerHelper($this);
  47. $this->model = $objectManager->getObject(
  48. \Magento\Elasticsearch\Model\Adapter\FieldMapper\FieldMapperResolver::class,
  49. [
  50. 'objectManager' => $this->objectManagerMock,
  51. 'fieldMappers' => $this->fieldMappers
  52. ]
  53. );
  54. }
  55. /**
  56. * Test getFieldName() with Exception
  57. * @return void
  58. * @expectedException \Exception
  59. */
  60. public function testGetFieldNameEmpty()
  61. {
  62. $this->model->getFieldName('attribute', ['entityType' => '']);
  63. }
  64. /**
  65. * Test getFieldName() with Exception
  66. * @return void
  67. * @expectedException \LogicException
  68. */
  69. public function testGetFieldNameWrongType()
  70. {
  71. $this->model->getFieldName('attribute', ['entityType' => 'error']);
  72. }
  73. /**
  74. * Test getFieldName() with Exception
  75. * @return void
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testGetFieldNameFailure()
  79. {
  80. $this->objectManagerMock->expects($this->once())
  81. ->method('create')
  82. ->willReturn(false);
  83. $this->model->getFieldName('attribute', ['entityType' => 'product']);
  84. }
  85. /**
  86. * Test getFieldName() method
  87. * @return void
  88. */
  89. public function testGetFieldName()
  90. {
  91. $this->objectManagerMock->expects($this->once())
  92. ->method('create')
  93. ->willReturn($this->fieldMapperEntity);
  94. $this->model->getFieldName('attribute', []);
  95. }
  96. /**
  97. * Test getAllAttributesTypes() method
  98. * @return void
  99. */
  100. public function testGetAllAttributesTypes()
  101. {
  102. $this->objectManagerMock->expects($this->once())
  103. ->method('create')
  104. ->willReturn($this->fieldMapperEntity);
  105. $this->model->getAllAttributesTypes([]);
  106. }
  107. }