-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
base: 2.4-develop
Are you sure you want to change the base?
Changes from all commits
913ef88
db3a9bf
3cbbaed
f452fb7
52148e5
d3f2765
8e6ad2e
b769e70
5bfdc03
b41f3c6
ca1ef2b
47c75ee
e5a78b7
0f3f525
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,200 @@ | ||||||
<?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\Exception\NoSuchEntityException; | ||||||
use Magento\Framework\MessageQueue\EnvelopeFactory; | ||||||
use Magento\Framework\MessageQueue\EnvelopeInterface; | ||||||
use Magento\Framework\MessageQueue\QueueInterface; | ||||||
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; | ||||||
|
||||||
class MassConsumerEnvelopeCallbackTest extends TestCase | ||||||
{ | ||||||
/** | ||||||
* @var MassConsumerEnvelopeCallback | ||||||
*/ | ||||||
private $massConsumerEnvelopeCallbackPlugin; | ||||||
|
||||||
/** | ||||||
* @var EnvelopeFactory|MockObject | ||||||
*/ | ||||||
private $envelopeFactoryMock; | ||||||
|
||||||
/** | ||||||
* @var StoreManagerInterface|MockObject | ||||||
*/ | ||||||
private $storeManagerMock; | ||||||
|
||||||
/** | ||||||
* @var \Psr\Log\LoggerInterface|MockObject | ||||||
*/ | ||||||
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->storeMock = $this->createMock(Store::class); | ||||||
$this->messageMock = $this->getMockForAbstractClass(EnvelopeInterface::class); | ||||||
|
||||||
$this->envelopeFactoryMock = $this->createMock(EnvelopeFactory::class); | ||||||
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); | ||||||
$this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\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; | ||||||
$proceed = function ($message) use (&$isProceedCalled) { | ||||||
$isProceedCalled = !!$message; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion. In this case, if I use Also, I want to confirm that Ak, maybe it is better if we change to Haha, it is the same :D |
||||||
}; | ||||||
|
||||||
$this->massConsumerEnvelopeCallbackPlugin->aroundExecute( | ||||||
$this->subjectMassConsumerEnvelopeCallbackMock, | ||||||
$proceed, | ||||||
$this->messageMock | ||||||
); | ||||||
|
||||||
$this->assertTrue($isProceedCalled); | ||||||
} | ||||||
|
||||||
public function testAroundExecuteWhenCanNotGetCurrentStoreId() | ||||||
{ | ||||||
$storeId = 333; | ||||||
$headers = ['store_id' => $storeId]; | ||||||
|
||||||
$amqpProperties = ['application_headers' => $headers]; | ||||||
$this->messageMock->expects($this->once()) | ||||||
->method('getProperties') | ||||||
->willReturn($amqpProperties); | ||||||
|
||||||
$message = 'no_such_entity_exception'; | ||||||
$this->storeManagerMock | ||||||
->expects($this->once()) | ||||||
->method('getStore') | ||||||
->willThrowException(new NoSuchEntityException(__($message))); | ||||||
|
||||||
$this->loggerMock | ||||||
->expects($this->once()) | ||||||
->method('error') | ||||||
->with("Can't set currentStoreId during processing queue. Message rejected. Error $message."); | ||||||
|
||||||
$queue = $this->getMockBuilder(QueueInterface::class) | ||||||
->disableOriginalConstructor() | ||||||
->getMockForAbstractClass(); | ||||||
$this->subjectMassConsumerEnvelopeCallbackMock | ||||||
->expects($this->once()) | ||||||
->method('getQueue') | ||||||
->willReturn($queue); | ||||||
$queue | ||||||
->expects($this->once()) | ||||||
->method('reject') | ||||||
->with($this->messageMock, false, $message); | ||||||
|
||||||
$isProceedCalled = false; | ||||||
$proceed = function ($message) use (&$isProceedCalled) { | ||||||
$isProceedCalled = !!$message; | ||||||
}; | ||||||
|
||||||
$this->massConsumerEnvelopeCallbackPlugin->aroundExecute( | ||||||
$this->subjectMassConsumerEnvelopeCallbackMock, | ||||||
$proceed, | ||||||
$this->messageMock | ||||||
); | ||||||
|
||||||
$this->assertFalse($isProceedCalled); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @dataProvider provideApplicationHeadersForAroundExecuteSuccess | ||||||
* | ||||||
* @param array|AMQPTable $headers | ||||||
* @param int $storeId | ||||||
* @param int $currentStoreId | ||||||
*/ | ||||||
public function testAroundExecuteWhenSuccess($headers, int $storeId, int $currentStoreId) | ||||||
{ | ||||||
$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') | ||||||
->withConsecutive( | ||||||
[$storeId], | ||||||
[$currentStoreId] | ||||||
); | ||||||
|
||||||
$isProceedCalled = false; | ||||||
$proceed = function ($message) use (&$isProceedCalled) { | ||||||
$isProceedCalled = !!$message; | ||||||
mbvb1223 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}; | ||||||
|
||||||
$this->massConsumerEnvelopeCallbackPlugin->aroundExecute( | ||||||
$this->subjectMassConsumerEnvelopeCallbackMock, | ||||||
$proceed, | ||||||
$this->messageMock | ||||||
); | ||||||
|
||||||
$this->assertTrue($isProceedCalled); | ||||||
} | ||||||
|
||||||
public function provideApplicationHeadersForAroundExecuteSuccess() | ||||||
{ | ||||||
$storeId = 123; | ||||||
$currentStoreId = 99; | ||||||
|
||||||
return [ | ||||||
[['store_id' => 123], $storeId, $currentStoreId], | ||||||
[new AMQPTable(['store_id' => 123]), $storeId, $currentStoreId], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a new object AMQPTable because I could not create a mock and mock for the final method "getNativeData" Do you have any suggestions for this? |
||||||
]; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$properties is always array so I removed this logic
https://github.com/mbvb1223/magento2/blob/5d2f2e87a6f23db0483a53d445bbf055826c022a/lib/internal/Magento/Framework/MessageQueue/Envelope.php#L29