Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConsumerFactoryTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
14 
15 class ConsumerFactoryTest extends \PHPUnit\Framework\TestCase
16 {
20  private $objectManager;
21 
24 
27 
28  const TEST_CONSUMER_NAME = "test_consumer_name";
29  const TEST_CONSUMER_QUEUE = "test_consumer_queue";
30  const TEST_CONSUMER_METHOD = "test_consumer_method";
31 
32  protected function setUp()
33  {
34  $this->objectManager = new ObjectManager($this);
35  $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
36  ->disableOriginalConstructor()
37  ->getMock();
38  $this->consumerConfigMock = $this->getMockBuilder(ConsumerConfig::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41  }
42 
47  public function testUndeclaredConsumerName()
48  {
49  $consumerFactory = $this->objectManager->getObject(ConsumerFactory::class);
50  $this->objectManager->setBackwardCompatibleProperty(
51  $consumerFactory,
52  'communicationConfig',
53  $this->communicationConfigMock
54  );
55  $this->objectManager->setBackwardCompatibleProperty(
56  $consumerFactory,
57  'consumerConfig',
58  $this->consumerConfigMock
59  );
60  $consumerFactory->get(self::TEST_CONSUMER_NAME);
61  }
62 
64  {
65  $consumerType = 'async';
66  $consumerTypeValue = \Magento\Framework\MessageQueue\Model\TestConsumer::class;
67  $consumers = [
68  [
69  'type' => [$consumerType => $consumerTypeValue]
70  ]
71  ];
72  $consumerFactory = $this->getConsumerFactoryInstance($consumers);
73  $consumerInstanceMock = $this->getMockBuilder($consumerTypeValue)->getMock();
74  $this->assertInstanceOf(get_class($consumerInstanceMock), $consumerFactory->get(self::TEST_CONSUMER_NAME));
75  }
76 
83  private function getConsumerFactoryInstance($consumers)
84  {
85  $consumerTypeValue = \Magento\Framework\MessageQueue\Model\TestConsumer::class;
86  $handlerTypeValue = \Magento\Framework\DataObject::class;
87  $consumerType = 'async';
88 
90  $consumerConfigItemMock = $this->getMockBuilder(ConsumerConfigItem::class)->disableOriginalConstructor()
91  ->getMock();
92  $consumerConfigItemMock->expects($this->any())->method('getName')->willReturn(self::TEST_CONSUMER_NAME);
93  $consumerConfigItemMock->expects($this->any())->method('getQueue')->willReturn(self::TEST_CONSUMER_QUEUE);
94  $consumerConfigItemMock->expects($this->any())->method('getConsumerInstance')->willReturn($consumerTypeValue);
95  $consumerConfigItemMock->expects($this->any())->method('getHandlers')->willReturn([]);
96  $this->consumerConfigMock->expects($this->any())
97  ->method('getConsumer')
98  ->with('test_consumer_name')
99  ->willReturn($consumerConfigItemMock);
100  $this->communicationConfigMock->expects($this->any())
101  ->method('getTopics')
102  ->willReturn(
103  [
104  [
105  CommunicationConfig::TOPIC_NAME => 'topicName',
106  CommunicationConfig::TOPIC_IS_SYNCHRONOUS => false
107  ]
108  ]
109  );
110  $this->communicationConfigMock->expects($this->any())
111  ->method('getTopic')
112  ->with('topicName')
113  ->willReturn(
114  [
115  CommunicationConfig::TOPIC_HANDLERS => [
116  [
117  CommunicationConfig::HANDLER_TYPE => $handlerTypeValue,
118  CommunicationConfig::HANDLER_METHOD => self::TEST_CONSUMER_METHOD
119  ]
120  ],
121  ]
122  );
123 
124  $consumerInstanceMock = $this->getMockBuilder($consumerTypeValue)->getMock();
125  $consumerMock = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerInterface::class)
126  ->setMethods(['configure'])
127  ->getMockForAbstractClass();
128 
129  $consumerConfigurationMock =
130  $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerConfigurationInterface::class)
131  ->disableOriginalConstructor()
132  ->getMockForAbstractClass();
133  $consumerConfigurationMock->expects($this->any())->method('getType')->willReturn($consumerType);
134 
135  $objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
136  ->setMethods(['create'])
137  ->getMockForAbstractClass();
138 
139  $objectManagerMock->expects($this->any())
140  ->method('create')
141  ->willReturnOnConsecutiveCalls($consumerMock, $consumerConfigurationMock, $consumerInstanceMock);
142 
143  $consumerFactory = $this->objectManager->getObject(
144  \Magento\Framework\MessageQueue\ConsumerFactory::class,
145  [
146  'objectManager' => $objectManagerMock,
147  'consumers' => $consumers
148  ]
149  );
150  $this->objectManager->setBackwardCompatibleProperty(
151  $consumerFactory,
152  'communicationConfig',
153  $this->communicationConfigMock
154  );
155  $this->objectManager->setBackwardCompatibleProperty(
156  $consumerFactory,
157  'consumerConfig',
158  $this->consumerConfigMock
159  );
160  return $consumerFactory;
161  }
162 }