Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PublisherTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\Amqp\Config as AmqpConfig;
21 
25 class PublisherTest extends \PHPUnit\Framework\TestCase
26 {
32  private $publisher;
33 
39  private $publisherConfig;
40 
46  private $amqpConfig;
47 
53  private $messageValidator;
54 
60  private $messageEncoder;
61 
67  private $exchangeRepository;
68 
74  private $envelopeFactory;
75 
79  protected function setUp()
80  {
81  $this->publisherConfig = $this->getMockBuilder(PublisherConfig::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84  $this->amqpConfig = $this->getMockBuilder(AmqpConfig::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->messageValidator = $this->getMockBuilder(MessageValidator::class)
88  ->disableOriginalConstructor()
89  ->getMock();
90  $this->messageEncoder = $this->getMockBuilder(MessageEncoder::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->exchangeRepository = $this->getMockBuilder(ExchangeRepository::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->envelopeFactory = $this->getMockBuilder(EnvelopeFactory::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99  $objectManager = new ObjectManager($this);
100  $this->publisher = $objectManager->getObject(
101  Publisher::class,
102  [
103  'messageValidator' => $this->messageValidator,
104  'envelopeFactory' => $this->envelopeFactory,
105  'messageEncoder' => $this->messageEncoder,
106  'exchangeRepository' => $this->exchangeRepository,
107  ]
108  );
109  $objectManager->setBackwardCompatibleProperty($this->publisher, 'publisherConfig', $this->publisherConfig);
110  $objectManager->setBackwardCompatibleProperty($this->publisher, 'amqpConfig', $this->amqpConfig);
111  }
112 
116  public function testPublish()
117  {
118  $topicName = 'tesTopicName';
119  $data = ['testData'];
120  $encodedData = 'testEncodedData';
121  $body = 'testBody';
122  $envelope = new Envelope($body);
123  $exchange = $this->getMockBuilder(Exchange::class)
124  ->disableOriginalConstructor()
125  ->getMock();
126  $exchange->expects(self::once())
127  ->method('enqueue')
128  ->with(self::identicalTo($topicName), self::identicalTo($envelope))
129  ->willReturn(null);
130  $connection = $this->getMockBuilder(PublisherConnection::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133  $connection->expects(self::once())
134  ->method('getName')
135  ->willReturn('amqp');
136  $publisher = $this->getMockBuilder(PublisherConfigItem::class)
137  ->disableOriginalConstructor()
138  ->getMock();
139  $publisher->expects(self::once())
140  ->method('getConnection')
141  ->willReturn($connection);
142  $this->messageValidator->expects(self::once())
143  ->method('validate');
144  $this->messageEncoder->expects(self::once())
145  ->method('encode')
146  ->with(self::identicalTo($topicName), self::identicalTo($data))
147  ->willReturn($encodedData);
148  $this->envelopeFactory->expects(self::once())
149  ->method('create')
150  ->willReturn($envelope);
151  $this->publisherConfig->expects(self::once())
152  ->method('getPublisher')
153  ->with($topicName)
154  ->willReturn($publisher);
155  $this->amqpConfig->expects(self::once())
156  ->method('getValue')
157  ->with(AmqpConfig::HOST)
158  ->willReturn('');
159  $this->exchangeRepository->expects(self::once())
160  ->method('getByConnectionName')
161  ->with('db')
162  ->willReturn($exchange);
163  self::assertNull($this->publisher->publish($topicName, $data));
164  }
165 }
$objectManager
Definition: bootstrap.php:17
$connection
Definition: bulk.php:13