From 913ef88cf5c4aef7c275ce25e6a6eb36ff605d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Tue, 27 Oct 2020 17:15:30 +0700 Subject: [PATCH 1/6] Adding unit tests for MassConsumerEnvelopeCallback --- .../MassConsumerEnvelopeCallbackTest.php | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php new file mode 100644 index 0000000000000..f13059c93abad --- /dev/null +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php @@ -0,0 +1,138 @@ +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); + } +} From db3a9bf8231ef408908c8a830b91d6f3eeb41cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Wed, 28 Oct 2020 13:35:10 +0700 Subject: [PATCH 2/6] 30671 - adding unit test for Exchange --- .../Plugin/Framework/Amqp/Bulk/Exchange.php | 10 +- .../MassConsumerEnvelopeCallbackTest.php | 87 +++++- .../Framework/Amqp/Bulk/ExchangeTest.php | 249 ++++++++++++++++++ 3 files changed, 329 insertions(+), 17 deletions(-) create mode 100644 app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php diff --git a/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php b/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php index 37960a64d3861..79932b9d3bd5a 100644 --- a/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php +++ b/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php @@ -24,14 +24,14 @@ class Exchange { /** - * @var StoreManagerInterface + * @var EnvelopeFactory */ - private $storeManager; + private $envelopeFactory; /** - * @var EnvelopeFactory + * @var StoreManagerInterface */ - private $envelopeFactory; + private $storeManager; /** * @var LoggerInterface @@ -49,8 +49,8 @@ public function __construct( StoreManagerInterface $storeManager, LoggerInterface $logger ) { - $this->storeManager = $storeManager; $this->envelopeFactory = $envelopeFactory; + $this->storeManager = $storeManager; $this->logger = $logger; } diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php index f13059c93abad..9c03a70a217df 100644 --- a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php @@ -9,8 +9,10 @@ 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; @@ -60,9 +62,7 @@ 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->storeMock = $this->createMock(Store::class); $this->envelopeFactoryMock = $this->createMock(EnvelopeFactory::class); $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); @@ -86,9 +86,8 @@ public function testAroundExecuteWhenApplicationHeadersDoesNotExist() ->willReturn(null); $isProceedCalled = false; - // @SuppressWarnings(PHPMD.UnusedFormalParameter) - $proceed = function ($attributeId) use (&$isProceedCalled) { - $isProceedCalled = true; + $proceed = function ($message) use (&$isProceedCalled) { + $isProceedCalled = !!$message; }; $this->massConsumerEnvelopeCallbackPlugin->aroundExecute( @@ -100,12 +99,62 @@ public function testAroundExecuteWhenApplicationHeadersDoesNotExist() $this->assertTrue($isProceedCalled); } - public function testAroundExecuteWhenApplicationHeadersExist() + public function testAroundExecuteWhenCanNotGetCurrentStoreId() { $storeId = 333; - $currentStoreId = 99; $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 $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') @@ -119,12 +168,15 @@ public function testAroundExecuteWhenApplicationHeadersExist() ->willReturn($currentStoreId); $this->storeManagerMock->expects($this->exactly(2)) - ->method('setCurrentStore'); + ->method('setCurrentStore') + ->withConsecutive( + [$storeId], + [$currentStoreId] + ); $isProceedCalled = false; - // @SuppressWarnings(PHPMD.UnusedFormalParameter) - $proceed = function ($attributeId) use (&$isProceedCalled) { - $isProceedCalled = true; + $proceed = function ($message) use (&$isProceedCalled) { + $isProceedCalled = !!$message; }; $this->massConsumerEnvelopeCallbackPlugin->aroundExecute( @@ -135,4 +187,15 @@ public function testAroundExecuteWhenApplicationHeadersExist() $this->assertTrue($isProceedCalled); } + + public function provideApplicationHeadersForAroundExecuteSuccess() + { + $storeId = 123; + $currentStoreId = 99; + + return [ + [['store_id' => 123], $storeId, $currentStoreId], + [new AMQPTable(['store_id' => 123]), $storeId, $currentStoreId], + ]; + } } diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php new file mode 100644 index 0000000000000..f179dba3fd44e --- /dev/null +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php @@ -0,0 +1,249 @@ +subjectExchange = $this->createMock(SubjectExchange::class); + $this->storeMock = $this->createMock(Store::class); + + $this->envelopeFactoryMock = $this->createMock(EnvelopeFactory::class); + $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); + + $objectManager = new ObjectManager($this); + $this->exchangePlugin = $objectManager->getObject( + Exchange::class, + [ + 'envelopeFactory' => $this->envelopeFactoryMock, + 'storeManager' => $this->storeManagerMock, + 'logger' => $this->loggerMock, + ] + ); + } + + public function testBeforeEnqueueWhenCanNotGetCurrentStoreId() + { + $topic = 'test_topic'; + $envelopes = []; + + $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 get current storeId and inject to amqp message. Error $message."); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage("Can't get current storeId and inject to amqp message. Error $message."); + + $this->exchangePlugin->beforeEnqueue( + $this->subjectExchange, + $topic, + $envelopes + ); + } + + public function testBeforeEnqueueWhenEnvelopePropertiesNull() + { + $topic = 'test_topic'; + $envelope_1 = $this->getMockForAbstractClass(EnvelopeInterface::class); + $envelopes = [$envelope_1]; + $storeId = 123; + + $this->storeManagerMock + ->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock + ->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + + $envelope_1 + ->expects($this->once()) + ->method('getProperties'); + + $body = 'envelope_body'; + $envelope_1 + ->expects($this->once()) + ->method('getBody') + ->willReturn($body); + + $newEnvelope = $this->getMockForAbstractClass(EnvelopeInterface::class); + $this->envelopeFactoryMock + ->expects($this->once()) + ->method('create') + ->with( + [ + 'body' => $body, + 'properties' => ['application_headers' => new AMQPTable(['store_id' => $storeId])] + ] + )->willReturn($newEnvelope); + + $actualResult = $this->exchangePlugin->beforeEnqueue( + $this->subjectExchange, + $topic, + $envelopes + ); + + $this->assertSame($topic, $actualResult[0]); + $this->assertSame($newEnvelope, $actualResult[1][0]); + } + + public function testBeforeEnqueueWhenHeaderIsAmqpTableButCanNotSetStoreId() + { + $topic = 'test_topic'; + $envelope_1 = $this->getMockForAbstractClass(EnvelopeInterface::class); + $envelopes = [$envelope_1]; + $storeId = 123; + $headers = $this->createMock(AMQPTable::class); + $properties = [ + 'application_headers' => $headers + ]; + + $this->storeManagerMock + ->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock + ->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + + $envelope_1 + ->expects($this->once()) + ->method('getProperties') + ->willReturn($properties); + + $exceptionMessage = 'errrorrrr!'; + $headers + ->expects($this->once()) + ->method('set') + ->with('store_id', $storeId) + ->willThrowException(new AMQPInvalidArgumentException($exceptionMessage)); + $this->loggerMock + ->expects($this->once()) + ->method('error') + ->with("Can't set storeId to amqp message. Error $exceptionMessage."); + $this->expectExceptionMessage("Can't set storeId to amqp message. Error $exceptionMessage."); + + $this->exchangePlugin->beforeEnqueue( + $this->subjectExchange, + $topic, + $envelopes + ); + } + + public function testBeforeEnqueueWhenIsAmqpTableAndSuccess() + { + $topic = 'test_topic'; + $envelope_1 = $this->getMockForAbstractClass(EnvelopeInterface::class); + $envelopes = [$envelope_1]; + $storeId = 123; + $headers = $this->createMock(AMQPTable::class); + $properties = [ + 'application_headers' => $headers + ]; + + $this->storeManagerMock + ->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock + ->expects($this->once()) + ->method('getId') + ->willReturn($storeId); + + $envelope_1 + ->expects($this->once()) + ->method('getProperties') + ->willReturn($properties); + + $headers + ->expects($this->once()) + ->method('set') + ->with('store_id', $storeId); + + $body = 'envelope_body'; + $envelope_1 + ->expects($this->once()) + ->method('getBody') + ->willReturn($body); + + $newEnvelope = $this->getMockForAbstractClass(EnvelopeInterface::class); + $this->envelopeFactoryMock + ->expects($this->once()) + ->method('create') + ->with( + [ + 'body' => $body, + 'properties' => ['application_headers' => $headers] + ] + )->willReturn($newEnvelope); + + $actualResult = $this->exchangePlugin->beforeEnqueue( + $this->subjectExchange, + $topic, + $envelopes + ); + + $this->assertSame($topic, $actualResult[0]); + $this->assertSame($newEnvelope, $actualResult[1][0]); + } +} From 3cbbaed762482ba71aa0136bb54b2464b7c437e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Wed, 28 Oct 2020 14:57:03 +0700 Subject: [PATCH 3/6] 30671 - fix logic isset $properties --- .../Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php b/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php index 79932b9d3bd5a..a09fc9d02063c 100644 --- a/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php +++ b/app/code/Magento/AmqpStore/Plugin/Framework/Amqp/Bulk/Exchange.php @@ -83,9 +83,6 @@ public function beforeEnqueue(SubjectExchange $subject, $topic, array $envelopes $updatedEnvelopes = []; foreach ($envelopes as $envelope) { $properties = $envelope->getProperties(); - if (!isset($properties)) { - $properties = []; - } if (isset($properties['application_headers'])) { $headers = $properties['application_headers']; if ($headers instanceof AMQPTable) { From f452fb72553d6ebf4b4d8692daf9cb1b1689fad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Wed, 28 Oct 2020 16:04:06 +0700 Subject: [PATCH 4/6] 30671 - reduce use namespace --- .../Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php index f179dba3fd44e..476a563436a6a 100644 --- a/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php @@ -19,7 +19,6 @@ use PhpAmqpLib\Wire\AMQPTable; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Psr\Log\LoggerInterface; class ExchangeTest extends TestCase { @@ -39,7 +38,7 @@ class ExchangeTest extends TestCase private $storeManagerMock; /** - * @var MockObject|LoggerInterface + * @var \Psr\Log\LoggerInterface|MockObject */ private $loggerMock; @@ -60,7 +59,7 @@ protected function setUp(): void $this->envelopeFactoryMock = $this->createMock(EnvelopeFactory::class); $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class); - $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); + $this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class); $objectManager = new ObjectManager($this); $this->exchangePlugin = $objectManager->getObject( From 52148e573a5c1fc352d83fcf59f8450c4a608077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=ACnh=20Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c?= Date: Thu, 29 Oct 2020 09:34:36 +0700 Subject: [PATCH 5/6] 30671 - refactor code --- .../MassConsumerEnvelopeCallbackTest.php | 9 ++- .../Framework/Amqp/Bulk/ExchangeTest.php | 56 ++++++++----------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php index 9c03a70a217df..f6a376055bc06 100644 --- a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php @@ -19,7 +19,6 @@ use PhpAmqpLib\Wire\AMQPTable; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Psr\Log\LoggerInterface; class MassConsumerEnvelopeCallbackTest extends TestCase { @@ -39,7 +38,7 @@ class MassConsumerEnvelopeCallbackTest extends TestCase private $storeManagerMock; /** - * @var MockObject|LoggerInterface + * @var \Psr\Log\LoggerInterface|MockObject */ private $loggerMock; @@ -61,12 +60,12 @@ class MassConsumerEnvelopeCallbackTest extends TestCase protected function setUp(): void { $this->subjectMassConsumerEnvelopeCallbackMock = $this->createMock(SubjectMassConsumerEnvelopeCallback::class); - $this->messageMock = $this->getMockForAbstractClass(EnvelopeInterface::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(LoggerInterface::class); + $this->loggerMock = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class); $objectManager = new ObjectManager($this); $this->massConsumerEnvelopeCallbackPlugin = $objectManager->getObject( @@ -149,7 +148,7 @@ public function testAroundExecuteWhenCanNotGetCurrentStoreId() /** * @dataProvider provideApplicationHeadersForAroundExecuteSuccess * - * @param $headers + * @param mixed $headers * @param int $storeId * @param int $currentStoreId */ diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php index 476a563436a6a..96fc9572bb7d3 100644 --- a/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/Framework/Amqp/Bulk/ExchangeTest.php @@ -82,7 +82,6 @@ public function testBeforeEnqueueWhenCanNotGetCurrentStoreId() ->expects($this->once()) ->method('getStore') ->willThrowException(new NoSuchEntityException(__($message))); - $this->loggerMock ->expects($this->once()) ->method('error') @@ -105,14 +104,7 @@ public function testBeforeEnqueueWhenEnvelopePropertiesNull() $envelopes = [$envelope_1]; $storeId = 123; - $this->storeManagerMock - ->expects($this->once()) - ->method('getStore') - ->willReturn($this->storeMock); - $this->storeMock - ->expects($this->once()) - ->method('getId') - ->willReturn($storeId); + $this->prepareMocksToGetStoreId($storeId); $envelope_1 ->expects($this->once()) @@ -147,23 +139,14 @@ public function testBeforeEnqueueWhenEnvelopePropertiesNull() public function testBeforeEnqueueWhenHeaderIsAmqpTableButCanNotSetStoreId() { - $topic = 'test_topic'; + $topic = 'test_topic_xxx'; $envelope_1 = $this->getMockForAbstractClass(EnvelopeInterface::class); $envelopes = [$envelope_1]; $storeId = 123; $headers = $this->createMock(AMQPTable::class); - $properties = [ - 'application_headers' => $headers - ]; + $properties = ['application_headers' => $headers]; - $this->storeManagerMock - ->expects($this->once()) - ->method('getStore') - ->willReturn($this->storeMock); - $this->storeMock - ->expects($this->once()) - ->method('getId') - ->willReturn($storeId); + $this->prepareMocksToGetStoreId($storeId); $envelope_1 ->expects($this->once()) @@ -180,6 +163,8 @@ public function testBeforeEnqueueWhenHeaderIsAmqpTableButCanNotSetStoreId() ->expects($this->once()) ->method('error') ->with("Can't set storeId to amqp message. Error $exceptionMessage."); + + $this->expectException(AMQPInvalidArgumentException::class); $this->expectExceptionMessage("Can't set storeId to amqp message. Error $exceptionMessage."); $this->exchangePlugin->beforeEnqueue( @@ -194,26 +179,16 @@ public function testBeforeEnqueueWhenIsAmqpTableAndSuccess() $topic = 'test_topic'; $envelope_1 = $this->getMockForAbstractClass(EnvelopeInterface::class); $envelopes = [$envelope_1]; - $storeId = 123; + $storeId = 999; $headers = $this->createMock(AMQPTable::class); - $properties = [ - 'application_headers' => $headers - ]; + $properties = ['application_headers' => $headers]; - $this->storeManagerMock - ->expects($this->once()) - ->method('getStore') - ->willReturn($this->storeMock); - $this->storeMock - ->expects($this->once()) - ->method('getId') - ->willReturn($storeId); + $this->prepareMocksToGetStoreId($storeId); $envelope_1 ->expects($this->once()) ->method('getProperties') ->willReturn($properties); - $headers ->expects($this->once()) ->method('set') @@ -244,5 +219,18 @@ public function testBeforeEnqueueWhenIsAmqpTableAndSuccess() $this->assertSame($topic, $actualResult[0]); $this->assertSame($newEnvelope, $actualResult[1][0]); + $this->assertSame(1, count($actualResult[1])); + } + + private function prepareMocksToGetStoreId(int $storeId) + { + $this->storeManagerMock + ->expects($this->once()) + ->method('getStore') + ->willReturn($this->storeMock); + $this->storeMock + ->expects($this->once()) + ->method('getId') + ->willReturn($storeId); } } From 5bfdc03dc2efedaa9a7383b0d6d4a4823a9cc46f Mon Sep 17 00:00:00 2001 From: Khien Pham Date: Mon, 2 Nov 2020 10:04:23 +0700 Subject: [PATCH 6/6] Update app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php Co-authored-by: Lukasz Bajsarowicz --- .../AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php index f6a376055bc06..bdd81a7096bb6 100644 --- a/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php +++ b/app/code/Magento/AmqpStore/Test/Unit/Plugin/AsynchronousOperations/MassConsumerEnvelopeCallbackTest.php @@ -148,7 +148,7 @@ public function testAroundExecuteWhenCanNotGetCurrentStoreId() /** * @dataProvider provideApplicationHeadersForAroundExecuteSuccess * - * @param mixed $headers + * @param array|AMQPTable $headers * @param int $storeId * @param int $currentStoreId */