Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConnectionManagerTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Psr\Log\LoggerInterface;
13 
17 class ConnectionManagerTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $model;
23 
27  protected $logger;
28 
32  private $clientFactory;
33 
37  private $clientConfig;
38 
44  protected function setUp()
45  {
46  $this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->clientFactory = $this->getMockBuilder(\Magento\AdvancedSearch\Model\Client\ClientFactoryInterface::class)
50  ->disableOriginalConstructor()
51  ->setMethods(['create'])
52  ->getMock();
53  $this->clientConfig = $this->getMockBuilder(ClientOptionsInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->clientConfig->expects($this->any())
58  ->method('prepareClientOptions')
59  ->willReturn([
60  'hostname' => 'localhost',
61  'port' => '9200',
62  'timeout' => 15,
63  'enableAuth' => 1,
64  'username' => 'user',
65  'password' => 'passwd',
66  ]);
67 
68  $objectManagerHelper = new ObjectManagerHelper($this);
69  $this->model = $objectManagerHelper->getObject(
70  \Magento\Elasticsearch\SearchAdapter\ConnectionManager::class,
71  [
72  'clientFactory' => $this->clientFactory,
73  'clientConfig' => $this->clientConfig,
74  'logger' => $this->logger
75  ]
76  );
77  }
78 
82  public function testGetConnectionSuccessfull()
83  {
84  $client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->clientFactory->expects($this->once())
88  ->method('create')
89  ->willReturn($client);
90 
91  $this->model->getConnection();
92  }
93 
98  public function testGetConnectionFailure()
99  {
100  $this->clientFactory->expects($this->any())
101  ->method('create')
102  ->willThrowException(new \Exception('Something went wrong'));
103  $this->model->getConnection();
104  }
105 }