ConnectionManagerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\AdvancedSearch\Model\Client\ClientOptionsInterface;
  8. use Magento\AdvancedSearch\Model\Client\ClientFactoryInterface;
  9. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  10. use Psr\Log\LoggerInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  12. /**
  13. * Class ConnectionManagerTest
  14. */
  15. class ConnectionManagerTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var ConnectionManager
  19. */
  20. protected $model;
  21. /**
  22. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $logger;
  25. /**
  26. * @var ClientFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $clientFactory;
  29. /**
  30. * @var ClientOptionsInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $clientConfig;
  33. /**
  34. * Setup
  35. *
  36. * @return void
  37. */
  38. protected function setUp()
  39. {
  40. $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->clientFactory = $this->getMockBuilder(\Magento\AdvancedSearch\Model\Client\ClientFactoryInterface::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['create'])
  46. ->getMock();
  47. $this->clientConfig = $this->getMockBuilder(ClientOptionsInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->clientConfig->expects($this->any())
  51. ->method('prepareClientOptions')
  52. ->willReturn([
  53. 'hostname' => 'localhost',
  54. 'port' => '9200',
  55. 'timeout' => 15,
  56. 'enableAuth' => 1,
  57. 'username' => 'user',
  58. 'password' => 'passwd',
  59. ]);
  60. $objectManagerHelper = new ObjectManagerHelper($this);
  61. $this->model = $objectManagerHelper->getObject(
  62. \Magento\Elasticsearch\SearchAdapter\ConnectionManager::class,
  63. [
  64. 'clientFactory' => $this->clientFactory,
  65. 'clientConfig' => $this->clientConfig,
  66. 'logger' => $this->logger
  67. ]
  68. );
  69. }
  70. /**
  71. * Test getConnection() method without errors
  72. */
  73. public function testGetConnectionSuccessfull()
  74. {
  75. $client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->clientFactory->expects($this->once())
  79. ->method('create')
  80. ->willReturn($client);
  81. $this->model->getConnection();
  82. }
  83. /**
  84. * Test getConnection() method with errors
  85. * @expectedException \RuntimeException
  86. */
  87. public function testGetConnectionFailure()
  88. {
  89. $this->clientFactory->expects($this->any())
  90. ->method('create')
  91. ->willThrowException(new \Exception('Something went wrong'));
  92. $this->model->getConnection();
  93. }
  94. }