publisherConfig = $this->getMockBuilder(PublisherConfig::class) ->disableOriginalConstructor() ->getMock(); $this->amqpConfig = $this->getMockBuilder(AmqpConfig::class) ->disableOriginalConstructor() ->getMock(); $this->messageValidator = $this->getMockBuilder(MessageValidator::class) ->disableOriginalConstructor() ->getMock(); $this->messageEncoder = $this->getMockBuilder(MessageEncoder::class) ->disableOriginalConstructor() ->getMock(); $this->exchangeRepository = $this->getMockBuilder(ExchangeRepository::class) ->disableOriginalConstructor() ->getMock(); $this->envelopeFactory = $this->getMockBuilder(EnvelopeFactory::class) ->disableOriginalConstructor() ->getMock(); $objectManager = new ObjectManager($this); $this->publisher = $objectManager->getObject( Publisher::class, [ 'messageValidator' => $this->messageValidator, 'envelopeFactory' => $this->envelopeFactory, 'messageEncoder' => $this->messageEncoder, 'exchangeRepository' => $this->exchangeRepository, ] ); $objectManager->setBackwardCompatibleProperty($this->publisher, 'publisherConfig', $this->publisherConfig); $objectManager->setBackwardCompatibleProperty($this->publisher, 'amqpConfig', $this->amqpConfig); } /** * @covers \Magento\Framework\MessageQueue\Publisher::publish() */ public function testPublish() { $topicName = 'tesTopicName'; $data = ['testData']; $encodedData = 'testEncodedData'; $body = 'testBody'; $envelope = new Envelope($body); $exchange = $this->getMockBuilder(Exchange::class) ->disableOriginalConstructor() ->getMock(); $exchange->expects(self::once()) ->method('enqueue') ->with(self::identicalTo($topicName), self::identicalTo($envelope)) ->willReturn(null); $connection = $this->getMockBuilder(PublisherConnection::class) ->disableOriginalConstructor() ->getMock(); $connection->expects(self::once()) ->method('getName') ->willReturn('amqp'); $publisher = $this->getMockBuilder(PublisherConfigItem::class) ->disableOriginalConstructor() ->getMock(); $publisher->expects(self::once()) ->method('getConnection') ->willReturn($connection); $this->messageValidator->expects(self::once()) ->method('validate'); $this->messageEncoder->expects(self::once()) ->method('encode') ->with(self::identicalTo($topicName), self::identicalTo($data)) ->willReturn($encodedData); $this->envelopeFactory->expects(self::once()) ->method('create') ->willReturn($envelope); $this->publisherConfig->expects(self::once()) ->method('getPublisher') ->with($topicName) ->willReturn($publisher); $this->amqpConfig->expects(self::once()) ->method('getValue') ->with(AmqpConfig::HOST) ->willReturn(''); $this->exchangeRepository->expects(self::once()) ->method('getByConnectionName') ->with('db') ->willReturn($exchange); self::assertNull($this->publisher->publish($topicName, $data)); } }