CompositeReaderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Test\Unit\Publisher\Config;
  7. use Magento\Framework\MessageQueue\Publisher\Config\CompositeReader;
  8. use Magento\Framework\MessageQueue\Publisher\Config\ValidatorInterface;
  9. use Magento\Framework\MessageQueue\Publisher\Config\ReaderInterface;
  10. class CompositeReaderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var CompositeReader
  14. */
  15. private $reader;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $validatorMock;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $readerOneMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $readerTwoMock;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $readerThreeMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $defaultConfigProviderMock;
  36. /**
  37. * Initialize parameters
  38. */
  39. protected function setUp()
  40. {
  41. $this->validatorMock = $this->createMock(ValidatorInterface::class);
  42. $this->readerOneMock = $this->createMock(ReaderInterface::class);
  43. $this->readerTwoMock = $this->createMock(ReaderInterface::class);
  44. $this->readerThreeMock = $this->createMock(ReaderInterface::class);
  45. $this->defaultConfigProviderMock =
  46. $this->createMock(\Magento\Framework\MessageQueue\DefaultValueProvider::class);
  47. $this->reader = new CompositeReader(
  48. $this->validatorMock,
  49. $this->defaultConfigProviderMock,
  50. [
  51. 'readerOne' => $this->readerOneMock,
  52. 'readerThree' => $this->readerThreeMock,
  53. 'readerTwo' => $this->readerTwoMock,
  54. ]
  55. );
  56. }
  57. public function testRead()
  58. {
  59. $this->defaultConfigProviderMock->expects($this->any())->method('getConnection')->willReturn('amqp');
  60. $this->defaultConfigProviderMock->expects($this->any())->method('getExchange')->willReturn('magento');
  61. $dataOne = include __DIR__ . '/../../_files/queue_publisher/reader_one.php';
  62. $dataTwo = include __DIR__ . '/../../_files/queue_publisher/reader_two.php';
  63. $expectedValidationData = include __DIR__ . '/../../_files/queue_publisher/data_to_validate.php';
  64. $this->readerOneMock->expects($this->once())->method('read')->with(null)->willReturn($dataOne);
  65. $this->readerTwoMock->expects($this->once())->method('read')->with(null)->willReturn($dataTwo);
  66. $this->readerThreeMock->expects($this->once())->method('read')->with(null)->willReturn([]);
  67. $this->validatorMock->expects($this->once())->method('validate')->with($expectedValidationData);
  68. $data = $this->reader->read();
  69. $expectedData = [
  70. //disabling existing connection and adding new
  71. 'top04' => [
  72. 'topic' => 'top04',
  73. 'disabled' => false,
  74. 'connection' => ['name' => 'db', 'disabled' => false, 'exchange' => 'magento2'],
  75. ],
  76. //two disabled connections are ignored
  77. 'top05' => [
  78. 'topic' => 'top05',
  79. 'disabled' => false,
  80. 'connection' => ['name' => 'amqp', 'exchange' => 'exch01', 'disabled' => false],
  81. ],
  82. //added default connection if not declared
  83. 'top06' => [
  84. 'topic' => 'top06',
  85. 'disabled' => false,
  86. 'connection' => ['name' => 'amqp', 'exchange' => 'magento', 'disabled' => false],
  87. ],
  88. //added default connection if all declared connections are disabled
  89. 'top07' => [
  90. 'topic' => 'top07',
  91. 'disabled' => false,
  92. 'connection' => ['name' => 'amqp', 'exchange' => 'magento', 'disabled' => false],
  93. ],
  94. ];
  95. $this->assertEquals($expectedData, $data);
  96. }
  97. }