ConfigTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Amqp\Test\Unit;
  7. use Magento\Framework\Amqp\Config;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. class ConfigTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $deploymentConfigMock;
  16. /**
  17. * @var ObjectManager
  18. */
  19. private $objectManager;
  20. /**
  21. * @var Config
  22. */
  23. private $amqpConfig;
  24. protected function setUp()
  25. {
  26. $this->objectManager = new ObjectManager($this);
  27. $this->deploymentConfigMock = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
  28. ->disableOriginalConstructor()
  29. ->setMethods(['getConfigData'])
  30. ->getMock();
  31. $this->amqpConfig = $this->objectManager->getObject(
  32. \Magento\Framework\Amqp\Config::class,
  33. [
  34. 'config' => $this->deploymentConfigMock,
  35. ]
  36. );
  37. }
  38. /**
  39. * @expectedException \LogicException
  40. * @expectedExceptionMessage Unknown connection name amqp
  41. */
  42. public function testGetNullConfig()
  43. {
  44. $this->deploymentConfigMock->expects($this->once())
  45. ->method('getConfigData')
  46. ->with(Config::QUEUE_CONFIG)
  47. ->will($this->returnValue(null));
  48. $this->amqpConfig->getValue(Config::HOST);
  49. }
  50. /**
  51. * @expectedException \LogicException
  52. * @expectedExceptionMessage Unknown connection name amqp
  53. */
  54. public function testGetEmptyConfig()
  55. {
  56. $this->deploymentConfigMock->expects($this->once())
  57. ->method('getConfigData')
  58. ->with(Config::QUEUE_CONFIG)
  59. ->will($this->returnValue([]));
  60. $this->amqpConfig->getValue(Config::HOST);
  61. }
  62. public function testGetStandardConfig()
  63. {
  64. $expectedHost = 'example.com';
  65. $expectedPort = 5672;
  66. $expectedUsername = 'guest_username';
  67. $expectedPassword = 'guest_password';
  68. $expectedVirtualHost = '/';
  69. $expectedSsl = false;
  70. $expectedSslOptions = ['some' => 'value'];
  71. $this->deploymentConfigMock->expects($this->once())
  72. ->method('getConfigData')
  73. ->with(Config::QUEUE_CONFIG)
  74. ->will($this->returnValue(
  75. [
  76. Config::AMQP_CONFIG => [
  77. 'host' => $expectedHost,
  78. 'port' => $expectedPort,
  79. 'user' => $expectedUsername,
  80. 'password' => $expectedPassword,
  81. 'virtualhost' => $expectedVirtualHost,
  82. 'ssl' => $expectedSsl,
  83. 'ssl_options' => $expectedSslOptions,
  84. 'randomKey' => 'randomValue',
  85. ]
  86. ]
  87. ));
  88. $this->assertEquals($expectedHost, $this->amqpConfig->getValue(Config::HOST));
  89. $this->assertEquals($expectedPort, $this->amqpConfig->getValue(Config::PORT));
  90. $this->assertEquals($expectedUsername, $this->amqpConfig->getValue(Config::USERNAME));
  91. $this->assertEquals($expectedPassword, $this->amqpConfig->getValue(Config::PASSWORD));
  92. $this->assertEquals($expectedVirtualHost, $this->amqpConfig->getValue(Config::VIRTUALHOST));
  93. $this->assertEquals($expectedSsl, $this->amqpConfig->getValue(Config::SSL));
  94. $this->assertEquals($expectedSslOptions, $this->amqpConfig->getValue(Config::SSL_OPTIONS));
  95. $this->assertEquals('randomValue', $this->amqpConfig->getValue('randomKey'));
  96. }
  97. public function testGetCustomConfig()
  98. {
  99. $amqpConfig = new \Magento\Framework\Amqp\Config($this->deploymentConfigMock, 'connection-01');
  100. $expectedHost = 'example.com';
  101. $expectedPort = 5672;
  102. $expectedUsername = 'guest_username';
  103. $expectedPassword = 'guest_password';
  104. $expectedVirtualHost = '/';
  105. $expectedSsl = ['some' => 'value'];
  106. $this->deploymentConfigMock->expects($this->once())
  107. ->method('getConfigData')
  108. ->with(Config::QUEUE_CONFIG)
  109. ->will($this->returnValue(
  110. [
  111. 'connections' => [
  112. 'connection-01' => [
  113. 'host' => $expectedHost,
  114. 'port' => $expectedPort,
  115. 'user' => $expectedUsername,
  116. 'password' => $expectedPassword,
  117. 'virtualhost' => $expectedVirtualHost,
  118. 'ssl' => $expectedSsl,
  119. 'randomKey' => 'randomValue',
  120. ]
  121. ]
  122. ]
  123. ));
  124. $this->assertEquals($expectedHost, $amqpConfig->getValue(Config::HOST));
  125. $this->assertEquals($expectedPort, $amqpConfig->getValue(Config::PORT));
  126. $this->assertEquals($expectedUsername, $amqpConfig->getValue(Config::USERNAME));
  127. $this->assertEquals($expectedPassword, $amqpConfig->getValue(Config::PASSWORD));
  128. $this->assertEquals($expectedVirtualHost, $amqpConfig->getValue(Config::VIRTUALHOST));
  129. $this->assertEquals($expectedSsl, $amqpConfig->getValue(Config::SSL));
  130. $this->assertEquals('randomValue', $amqpConfig->getValue('randomKey'));
  131. }
  132. }