FactoryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Amqp\Test\Unit\Connection;
  8. use Magento\Framework\Amqp\Connection\Factory;
  9. use Magento\Framework\Amqp\Connection\FactoryOptions;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use PhpAmqpLib\Connection\AMQPSSLConnection;
  12. use PhpAmqpLib\Connection\AMQPStreamConnection;
  13. /**
  14. * Tests \Magento\Framework\Amqp\Connection\Factory.
  15. */
  16. class FactoryTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var Factory
  20. */
  21. private $object;
  22. /**
  23. * @var ObjectManager
  24. */
  25. private $objectManager;
  26. /**
  27. * @var \Magento\Framework\App\ObjectManager
  28. */
  29. private $objectManagerInterface;
  30. /**
  31. * @var FactoryOptions|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $optionsMock;
  34. /**
  35. * @inheritdoc
  36. */
  37. protected function setUp()
  38. {
  39. $this->objectManager = new ObjectManager($this);
  40. $className = \Magento\Framework\ObjectManagerInterface::class;
  41. $this->objectManagerInterface = $this->createMock($className);
  42. $this->optionsMock = $this->getMockBuilder(FactoryOptions::class)
  43. ->disableOriginalConstructor()
  44. ->setMethods(
  45. [
  46. 'isSslEnabled',
  47. 'getHost',
  48. 'getPort',
  49. 'getUsername',
  50. 'getPassword',
  51. 'getVirtualHost',
  52. 'getSslOptions',
  53. ]
  54. )
  55. ->getMock();
  56. $this->object = $this->objectManager->getObject(Factory::class);
  57. }
  58. /**
  59. * @param bool $sslEnabled
  60. * @param string $connectionClass
  61. * @return void
  62. * @dataProvider connectionDataProvider
  63. */
  64. public function testSSLConnection($sslEnabled, $connectionClass)
  65. {
  66. $this->optionsMock->expects($this->exactly(2))
  67. ->method('isSslEnabled')
  68. ->willReturn($sslEnabled);
  69. $this->optionsMock->expects($this->once())
  70. ->method('getHost')
  71. ->willReturn('127.0.0.1');
  72. $this->optionsMock->expects($this->once())
  73. ->method('getPort')
  74. ->willReturn('5672');
  75. $this->optionsMock->expects($this->once())
  76. ->method('getUsername')
  77. ->willReturn('guest');
  78. $this->optionsMock->expects($this->once())
  79. ->method('getPassword')
  80. ->willReturn('guest');
  81. $this->optionsMock->expects($this->exactly(2))
  82. ->method('getVirtualHost')
  83. ->willReturn('/');
  84. $this->optionsMock->expects($this->any())
  85. ->method('getSslOptions')
  86. ->willReturn(null);
  87. $this->objectManagerInterface->expects($this->any())
  88. ->method('create')
  89. ->with($connectionClass)
  90. ->willReturn($this->createMock($connectionClass));
  91. \Magento\Framework\App\ObjectManager::setInstance($this->objectManagerInterface);
  92. $connection = $this->object->create($this->optionsMock);
  93. $this->assertInstanceOf($connectionClass, $connection);
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function connectionDataProvider()
  99. {
  100. return [
  101. [
  102. 'ssl_enabled' => true,
  103. 'connection_class' => AMQPSSLConnection::class,
  104. ],
  105. [
  106. 'ssl_enabled' => false,
  107. 'connection_class' => AMQPStreamConnection::class,
  108. ],
  109. ];
  110. }
  111. protected function tearDown()
  112. {
  113. $this->objectManager->setBackwardCompatibleProperty(
  114. null,
  115. '_instance',
  116. null,
  117. \Magento\Framework\App\ObjectManager::class
  118. );
  119. }
  120. }