Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExchangeTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ExchangeTest extends \PHPUnit\Framework\TestCase
13 {
17  private $amqpConfig;
18 
22  private $communicationConfig;
23 
27  private $publisherConfig;
28 
32  private $exchange;
33 
37  private $bulkExchange;
38 
44  protected function setUp()
45  {
46  $this->amqpConfig = $this->getMockBuilder(\Magento\Framework\Amqp\Config::class)
47  ->disableOriginalConstructor()->getMock();
48  $this->communicationConfig = $this->getMockBuilder(\Magento\Framework\Communication\ConfigInterface::class)
49  ->disableOriginalConstructor()->getMock();
50  $this->publisherConfig = $this
51  ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\ConfigInterface::class)
52  ->disableOriginalConstructor()->getMock();
53  $this->exchange = $this->getMockBuilder(\Magento\Framework\Amqp\Exchange::class)
54  ->disableOriginalConstructor()->getMock();
55 
56  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57  $this->bulkExchange = $objectManager->getObject(
58  \Magento\Framework\Amqp\Bulk\Exchange::class,
59  [
60  'amqpConfig' => $this->amqpConfig,
61  'communicationConfig' => $this->communicationConfig,
62  'publisherConfig' => $this->publisherConfig,
63  'exchange' => $this->exchange,
64  ]
65  );
66  }
67 
73  public function testEnqueue()
74  {
75  $topicName = 'topic.name';
76  $exchangeName = 'exchangeName';
77  $envelopeBody = 'envelopeBody';
78  $envelopeProperties = ['property_key_1' => 'property_value_1'];
79  $topicData = [
81  ];
82  $this->communicationConfig->expects($this->once())
83  ->method('getTopic')->with($topicName)->willReturn($topicData);
84  $channel = $this->getMockBuilder(\AMQPChannel::class)
85  ->setMethods(['batch_basic_publish', 'publish_batch'])
86  ->disableOriginalConstructor()->getMock();
87  $this->amqpConfig->expects($this->once())->method('getChannel')->willReturn($channel);
88  $publisher = $this
89  ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItemInterface::class)
90  ->disableOriginalConstructor()->getMock();
91  $this->publisherConfig->expects($this->once())
92  ->method('getPublisher')->with($topicName)->willReturn($publisher);
93  $connection = $this
94  ->getMockBuilder(\Magento\Framework\MessageQueue\Publisher\Config\PublisherConnectionInterface::class)
95  ->disableOriginalConstructor()->getMock();
96  $publisher->expects($this->once())->method('getConnection')->with()->willReturn($connection);
97  $connection->expects($this->once())->method('getExchange')->with()->willReturn($exchangeName);
98  $envelope = $this
99  ->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
100  ->disableOriginalConstructor()->getMock();
101  $envelope->expects($this->once())->method('getBody')->willReturn($envelopeBody);
102  $envelope->expects($this->once())->method('getProperties')->willReturn($envelopeProperties);
103  $channel->expects($this->once())->method('batch_basic_publish')
104  ->with($this->isInstanceOf(\PhpAmqpLib\Message\AMQPMessage::class), $exchangeName, $topicName);
105  $channel->expects($this->once())->method('publish_batch');
106  $this->assertNull($this->bulkExchange->enqueue($topicName, [$envelope]));
107  }
108 
115  {
116  $topicName = 'topic.name';
117  $response = 'responseBody';
118  $topicData = [
120  ];
121  $this->communicationConfig->expects($this->once())
122  ->method('getTopic')->with($topicName)->willReturn($topicData);
123  $envelope = $this
124  ->getMockBuilder(\Magento\Framework\MessageQueue\EnvelopeInterface::class)
125  ->disableOriginalConstructor()->getMock();
126  $this->exchange->expects($this->once())->method('enqueue')->with($topicName, $envelope)->willReturn($response);
127  $this->assertEquals([$response], $this->bulkExchange->enqueue($topicName, [$envelope]));
128  }
129 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
return false
Definition: gallery.phtml:36
$connection
Definition: bulk.php:13