Skip to content

Cover AmqpStore module with unit tests #30660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AmqpStore\Test\Unit\Plugin\AsynchronousOperations;

use Magento\AmqpStore\Plugin\AsynchronousOperations\MassConsumerEnvelopeCallback;
use Magento\AsynchronousOperations\Model\MassConsumerEnvelopeCallback as SubjectMassConsumerEnvelopeCallback;
use Magento\Framework\MessageQueue\EnvelopeFactory;
use Magento\Framework\MessageQueue\EnvelopeInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use PhpAmqpLib\Wire\AMQPTable;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class MassConsumerEnvelopeCallbackTest extends TestCase
{
/**
* @var MassConsumerEnvelopeCallback
*/
private $massConsumerEnvelopeCallbackPlugin;

/**
* @var EnvelopeFactory|MockObject
*/
private $envelopeFactoryMock;

/**
* @var StoreManagerInterface|MockObject
*/
private $storeManagerMock;

/**
* @var MockObject|LoggerInterface
*/
private $loggerMock;

/**
* @var SubjectMassConsumerEnvelopeCallback|MockObject
*/
private $subjectMassConsumerEnvelopeCallbackMock;

/**
* @var EnvelopeInterface|MockObject
*/
private $messageMock;

/**
* @var Store|MockObject
*/
private $storeMock;

protected function setUp(): void
{
$this->subjectMassConsumerEnvelopeCallbackMock = $this->createMock(SubjectMassConsumerEnvelopeCallback::class);
$this->messageMock = $this->getMockForAbstractClass(EnvelopeInterface::class);
$this->storeMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->getMock();

$this->envelopeFactoryMock = $this->createMock(EnvelopeFactory::class);
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);

$objectManager = new ObjectManager($this);
$this->massConsumerEnvelopeCallbackPlugin = $objectManager->getObject(
MassConsumerEnvelopeCallback::class,
[
'envelopeFactory' => $this->envelopeFactoryMock,
'storeManager' => $this->storeManagerMock,
'logger' => $this->loggerMock,
]
);
}

public function testAroundExecuteWhenApplicationHeadersDoesNotExist()
{
$this->messageMock->expects($this->once())
->method('getProperties')
->willReturn(null);

$isProceedCalled = false;
// @SuppressWarnings(PHPMD.UnusedFormalParameter)
$proceed = function ($attributeId) use (&$isProceedCalled) {
$isProceedCalled = true;
};

$this->massConsumerEnvelopeCallbackPlugin->aroundExecute(
$this->subjectMassConsumerEnvelopeCallbackMock,
$proceed,
$this->messageMock
);

$this->assertTrue($isProceedCalled);
}

public function testAroundExecuteWhenApplicationHeadersExist()
{
$storeId = 333;
$currentStoreId = 99;
$headers = ['store_id' => $storeId];

$amqpProperties = ['application_headers' => $headers];
$this->messageMock->expects($this->once())
->method('getProperties')
->willReturn($amqpProperties);

$this->storeManagerMock->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);
$this->storeMock->expects($this->once())
->method('getId')
->willReturn($currentStoreId);

$this->storeManagerMock->expects($this->exactly(2))
->method('setCurrentStore');

$isProceedCalled = false;
// @SuppressWarnings(PHPMD.UnusedFormalParameter)
$proceed = function ($attributeId) use (&$isProceedCalled) {
$isProceedCalled = true;
};

$this->massConsumerEnvelopeCallbackPlugin->aroundExecute(
$this->subjectMassConsumerEnvelopeCallbackMock,
$proceed,
$this->messageMock
);

$this->assertTrue($isProceedCalled);
}
}