ConfigurationTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\tests;
  3. use mikemadisonweb\rabbitmq\Configuration;
  4. use mikemadisonweb\rabbitmq\exceptions\InvalidConfigException;
  5. use yii\base\UnknownPropertyException;
  6. class ConfigurationTest extends TestCase
  7. {
  8. protected $invalidConfig = [];
  9. public function testIsNotConfigured()
  10. {
  11. $this->expectException(UnknownPropertyException::class);
  12. $this->mockApplication();
  13. \Yii::$app->rabbitmq->getConfig();
  14. }
  15. public function testConfigType()
  16. {
  17. $this->mockApplication([
  18. 'components' => [
  19. 'rabbitmq' => [
  20. 'class' => Configuration::class,
  21. 'connections' => [
  22. [
  23. 'host' => 'localhost',
  24. ],
  25. ],
  26. ],
  27. ],
  28. ]);
  29. $this->assertInstanceOf(Configuration::class, \Yii::$app->rabbitmq->getConfig());
  30. }
  31. /**
  32. * @dataProvider invalidConfig
  33. */
  34. public function testInvalidConfig($reason, $config, $exception)
  35. {
  36. $this->expectException($exception);
  37. $rabbitmq = array_merge(['class' => Configuration::class], $config);
  38. $components = [
  39. 'components' => [
  40. 'rabbitmq' => $rabbitmq,
  41. ],
  42. ];
  43. $this->mockApplication($components);
  44. \Yii::$app->rabbitmq->getConfig();
  45. }
  46. public function invalidConfig()
  47. {
  48. $required = ['connections' => [['host' => 'localhost']]];
  49. return [
  50. ['At least one connection required', ['connections' => []], InvalidConfigException::class],
  51. ['Unknown option', array_merge($required, ['unknown' => []]), UnknownPropertyException::class],
  52. ['Name should be specified on multiple connections', ['connections' => [['host' => 'rabbitmq'],['host' => 'rabbitmq']]], InvalidConfigException::class],
  53. ['Wrong auto-declare option', array_merge($required, ['auto_declare' => 43]), InvalidConfigException::class],
  54. ['Logger is not an array', array_merge($required, ['logger' => 43]), InvalidConfigException::class],
  55. ['Unknown option in logger section', array_merge($required, ['logger' => ['unknown' => 43]]), InvalidConfigException::class],
  56. ['Connections array should be multidimensional and numeric', ['connections' => ['some-key' => 'some-value']], InvalidConfigException::class],
  57. ['Bindings array should be multidimensional and numeric', ['bindings' => ['some-key' => 'some-value']], InvalidConfigException::class],
  58. ['Exchanges array should be multidimensional and numeric', ['exchanges' => ['some-key' => 'some-value']], InvalidConfigException::class],
  59. ['Queues array should be multidimensional and numeric', ['queues' => ['some-key' => 'some-value']], InvalidConfigException::class],
  60. ['Producers array should be multidimensional and numeric', ['producers' => ['some-key' => 'some-value']], InvalidConfigException::class],
  61. ['Consumers array should be multidimensional and numeric', ['consumers' => ['some-key' => 'some-value']], InvalidConfigException::class],
  62. ['Url or host is required', ['connections' => [[]]], InvalidConfigException::class],
  63. ['Not both url and host allowed', ['connections' => [['url' => 'some-value1', 'host' => 'some-value2']]], InvalidConfigException::class],
  64. ['No additional fields are allowed in connections', ['connections' => [['wrong' => 'wrong', 'host' => 'localhost']]], InvalidConfigException::class],
  65. ['Connection type should be of correct subclass', ['connections' => [['type' => 'SomeTypeUnknown', 'host' => 'localhost']]], InvalidConfigException::class],
  66. ['Exchange name is required', array_merge($required, ['exchanges' => [['type' => 'direct']]]), InvalidConfigException::class],
  67. ['Exchange type is required', array_merge($required, ['exchanges' => [['name' => 'direct']]]), InvalidConfigException::class],
  68. ['Exchange type should be one of allowed', array_merge($required, ['exchanges' => [['type' => 'wrong']]]), InvalidConfigException::class],
  69. ['Exchange wrong field', array_merge($required, ['exchanges' => [['type' => 'direct', 'name' => 'direct', 'wrong' => 'wrong']]]), InvalidConfigException::class],
  70. ['Queue wrong field', array_merge($required, ['queues' => [['wrong' => 'wrong']]]), InvalidConfigException::class],
  71. ['Exchange name is required for binding', array_merge($required, ['bindings' => [[]]]), InvalidConfigException::class],
  72. ['Routing key is required for binding', array_merge($required, ['bindings' => [['exchange' => 'smth']]]), InvalidConfigException::class],
  73. ['Either `queue` or `to_exchange` options should be specified to create binding', array_merge($required, ['bindings' => [['exchange' => 'smth', 'routing_keys' => ['smth'],]]]), InvalidConfigException::class],
  74. ['Exchanges and queues should be configured in corresponding sections', array_merge($required, ['bindings' => [['exchange' => 'smth', 'routing_keys' => ['smth'], 'queue' => 'smth',]]]), InvalidConfigException::class],
  75. ['Binding wrong field', array_merge($required, ['bindings' => [['wrong' => 'wrong']]]), InvalidConfigException::class],
  76. ['Producer wrong field', array_merge($required, ['producers' => [['wrong' => 'wrong']]]), InvalidConfigException::class],
  77. ['Producer name is required', array_merge($required, ['producers' => [[]]]), InvalidConfigException::class],
  78. ['Connection defined in producer should exist', array_merge($required, ['producers' => [['name' => 'smth', 'connection' => 'unknown']]]), InvalidConfigException::class],
  79. ['Safe option should be a boolean', array_merge($required, ['producers' => [['name' => 'smth', 'safe' => 'non_bool']]]), InvalidConfigException::class],
  80. ['Serializer should be callable', array_merge($required, ['producers' => [['name' => 'smth', 'serializer' => 'non_callable']]]), InvalidConfigException::class],
  81. ['Consumer wrong field', array_merge($required, ['consumers' => [['wrong' => 'wrong']]]), InvalidConfigException::class],
  82. ['Consumer name is required', array_merge($required, ['consumers' => [[]]]), InvalidConfigException::class],
  83. ['Connection defined in consumer should exist', array_merge($required, ['consumers' => [['name' => 'smth', 'connection' => 'unknown']]]), InvalidConfigException::class],
  84. ['No callbacks specified in consumer', array_merge($required, ['consumers' => [['name' => 'smth', 'callbacks' => []]]]), InvalidConfigException::class],
  85. ['Option qos is not an array', array_merge($required, ['consumers' => [['name' => 'smth', 'qos' => 'not_an_array']]]), InvalidConfigException::class],
  86. ['Option proceed_on_exception is not a boolean', array_merge($required, ['consumers' => [['name' => 'smth', 'proceed_on_exception' => 'not_a_boolean']]]), InvalidConfigException::class],
  87. ['Queue defined in consumer should exist', array_merge($required, ['consumers' => [['name' => 'smth', 'callbacks' => ['unknown' => 'some-callback']]]]), InvalidConfigException::class],
  88. ['Consumer callback parameter should be string', array_merge($required, ['queues' => [['name' => 'smth']], 'consumers' => [['name' => 'smth', 'callbacks' => ['smth' => 34]]]]), InvalidConfigException::class],
  89. ['Deserializer should be callable', array_merge($required, ['consumers' => [['name' => 'smth', 'deserializer' => 'non_callable']]]), InvalidConfigException::class],
  90. ['Connection in consumer not exist', ['connections' => [['host' => 'rabbitmq']], 'consumers' => [['name' => 'smth', 'connection' => 'default2']]], InvalidConfigException::class],
  91. ['Named connection not specified in producer', ['connections' => [['host' => 'rabbitmq', 'name' => 'default2']], 'producers' => [['name' => 'smth']]], InvalidConfigException::class],
  92. ['Duplicate names in producer', array_merge($required, ['producers' => [['name' => 'smth'], ['name' => 'smth']]]), InvalidConfigException::class],
  93. ];
  94. }
  95. }