MessageValidatorTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Test\Unit;
  7. use Doctrine\Instantiator\Exception\InvalidArgumentException;
  8. use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
  9. use Magento\Framework\MessageQueue\MessageValidator;
  10. /**
  11. * @covers Magento\Framework\MessageQueue\MessageValidator
  12. * @SuppressWarnings(PHPMD)
  13. */
  14. class MessageValidatorTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var MessageValidator */
  17. protected $model;
  18. /** @var CommunicationConfig|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $communicationConfigMock;
  20. protected function setUp()
  21. {
  22. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. $this->model = $objectManager->getObject(MessageValidator::class);
  24. $this->communicationConfigMock = $this->getMockBuilder(CommunicationConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $objectManager->setBackwardCompatibleProperty(
  28. $this->model,
  29. 'communicationConfig',
  30. $this->communicationConfigMock
  31. );
  32. }
  33. /**
  34. * @expectedException \Magento\Framework\Exception\LocalizedException
  35. * @expectedExceptionMessage Specified topic "customer.created" is not declared.
  36. */
  37. public function testValidateInvalidTopic()
  38. {
  39. $this->model->validate('customer.created', 'Some message', true);
  40. }
  41. public function testValidateValidObjectType()
  42. {
  43. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  44. $this->getQueueConfigDataObjectType()
  45. );
  46. $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  47. ->disableOriginalConstructor()
  48. ->setMethods([])
  49. ->getMock();
  50. $this->model->validate('customer.created', $object, true);
  51. }
  52. public function testValidateValidMethodType()
  53. {
  54. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  55. $this->getQueueConfigDataMethodType()
  56. );
  57. $object = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods([])
  60. ->getMock();
  61. $this->model->validate('customer.created', [$object, 'password', 'redirect'], true);
  62. }
  63. public function testEncodeValidMessageObjectType()
  64. {
  65. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  66. $this->getQueueConfigDataObjectType()
  67. );
  68. $this->model->validate('customer.created', [], true);
  69. }
  70. /**
  71. * @expectedException InvalidArgumentException
  72. * @expectedExceptionMessage Data in topic "customer.created" must be of type "Magento\Customer\Api\Data\CustomerInt
  73. */
  74. public function testEncodeInvalidMessageMethodType()
  75. {
  76. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn(
  77. $this->getQueueConfigDataMethodType()
  78. );
  79. $this->model->validate('customer.created', [1, 2, 3], true);
  80. }
  81. /**
  82. * Data provider for queue config of object type
  83. *
  84. * @return array
  85. */
  86. private function getQueueConfigDataObjectType()
  87. {
  88. return [
  89. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  90. CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
  91. ];
  92. }
  93. /**
  94. * Data provider for queue config of method type
  95. *
  96. * @return array
  97. */
  98. private function getQueueConfigDataMethodType()
  99. {
  100. return [
  101. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_METHOD,
  102. CommunicationConfig::TOPIC_REQUEST => [
  103. [
  104. 'param_name' => 'customer',
  105. 'param_position' => 0,
  106. 'is_required' => true,
  107. 'param_type' => \Magento\Customer\Api\Data\CustomerInterface::class,
  108. ],
  109. [
  110. 'param_name' => 'password',
  111. 'param_position' => 1,
  112. 'is_required' => false,
  113. 'param_type' => 'string',
  114. ],
  115. [
  116. 'param_name' => 'redirectUrl',
  117. 'param_position' => 2,
  118. 'is_required' => false,
  119. 'param_type' => 'string',
  120. ],
  121. ]
  122. ];
  123. }
  124. /**
  125. * @dataProvider getQueueConfigRequestType
  126. */
  127. public function testInvalidMessageType($requestType, $message, $expectedResult = null)
  128. {
  129. $this->communicationConfigMock->expects($this->any())->method('getTopic')->willReturn($requestType);
  130. if ($expectedResult) {
  131. $this->expectException('InvalidArgumentException');
  132. $this->expectExceptionMessage($expectedResult);
  133. }
  134. $this->model->validate('topic', $message);
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function getQueueConfigRequestType()
  140. {
  141. $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods([])
  144. ->getMock();
  145. $customerMockTwo = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
  146. ->disableOriginalConstructor()
  147. ->setMethods([])
  148. ->getMock();
  149. return [
  150. [
  151. [
  152. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  153. CommunicationConfig::TOPIC_REQUEST => 'string'
  154. ],
  155. 'valid string',
  156. null
  157. ],
  158. [
  159. [
  160. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  161. CommunicationConfig::TOPIC_REQUEST => 'string'
  162. ],
  163. 1,
  164. 'Data in topic "topic" must be of type "string". "int" given.'
  165. ],
  166. [
  167. [
  168. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  169. CommunicationConfig::TOPIC_REQUEST => 'string[]'
  170. ],
  171. ['string1', 'string2'],
  172. null
  173. ],
  174. [
  175. [
  176. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  177. CommunicationConfig::TOPIC_REQUEST => 'string[]'
  178. ],
  179. [],
  180. null
  181. ],
  182. [
  183. [
  184. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  185. CommunicationConfig::TOPIC_REQUEST => 'string[]'
  186. ],
  187. 'single string',
  188. 'Data in topic "topic" must be of type "string[]". "string" given.'
  189. ],
  190. [
  191. [
  192. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  193. CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
  194. ],
  195. $customerMock,
  196. null
  197. ],
  198. [
  199. [
  200. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  201. CommunicationConfig::TOPIC_REQUEST => \Magento\Customer\Api\Data\CustomerInterface::class
  202. ],
  203. 'customer',
  204. 'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface". "string" given.'
  205. ],
  206. [
  207. [
  208. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  209. CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
  210. ],
  211. [$customerMock, $customerMockTwo],
  212. null
  213. ],
  214. [
  215. [
  216. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  217. CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
  218. ],
  219. [],
  220. null
  221. ],
  222. [
  223. [
  224. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  225. CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
  226. ],
  227. 'customer',
  228. 'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface[]". "string" given.'
  229. ],
  230. [
  231. [
  232. CommunicationConfig::TOPIC_REQUEST_TYPE => CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS,
  233. CommunicationConfig::TOPIC_REQUEST => 'Magento\Customer\Api\Data\CustomerInterface[]'
  234. ],
  235. $customerMock,
  236. 'Data in topic "topic" must be of type "Magento\Customer\Api\Data\CustomerInterface[]". '
  237. ],
  238. ];
  239. }
  240. }