ConfigTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Communication;
  7. /**
  8. * Test of communication configuration reading and parsing.
  9. *
  10. * @magentoCache config disabled
  11. */
  12. class ConfigTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * Check how valid communication XML config is parsed.
  16. */
  17. public function testGetTopics()
  18. {
  19. $topics = $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopics();
  20. $expectedParsedTopics = include __DIR__ . '/_files/valid_communication_expected.php';
  21. $this->assertEquals($expectedParsedTopics, $topics);
  22. }
  23. /**
  24. * Get topic configuration by its name
  25. *
  26. * @expectedException \LogicException
  27. * @expectedExceptionMessage Service method specified in the definition of topic "customerDeletedNumbers" is not av
  28. */
  29. public function testGetTopicsNumeric()
  30. {
  31. $this->getConfigInstance(__DIR__ . '/_files/valid_communication_numeric.xml')->getTopics();
  32. }
  33. // @codingStandardsIgnoreStart
  34. /**
  35. * Get topic configuration by its name
  36. *
  37. * @expectedException \Magento\Framework\Exception\LocalizedException
  38. * @expectedExceptionMessage The XML in file "0" is invalid:
  39. Element 'topic', attribute 'schema': [facet 'pattern'] The value '55\Customer\Api\CustomerRepositoryInterface::delete' is not accepted by the pattern '[a-zA-Z]+[a-zA-Z0-9\\]+::[a-zA-Z0-9]+'.
  40. Line: 9
  41. Element 'topic', attribute 'schema': '55\Customer\Api\CustomerRepositoryInterface::delete' is not a valid value of the atomic type 'schemaType'.
  42. Line: 9
  43. Element 'handler', attribute 'type': [facet 'pattern'] The value '55\Customer\Api\CustomerRepositoryInterface' is not accepted by the pattern '[a-zA-Z]+[a-zA-Z0-9\\]+'.
  44. Line: 10
  45. Element 'handler', attribute 'type': '55\Customer\Api\CustomerRepositoryInterface' is not a valid value of the atomic type 'serviceTypeType'.
  46. Line: 10
  47. Verify the XML and try again.
  48. *
  49. */
  50. // @codingStandardsIgnoreEnd
  51. public function testGetTopicsNumericInvalid()
  52. {
  53. $this->getConfigInstance(__DIR__ . '/_files/invalid_communication_numeric.xml')->getTopics();
  54. }
  55. /**
  56. * Get topic configuration by its name
  57. */
  58. public function testGetTopic()
  59. {
  60. $topics = $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('customerCreated');
  61. $expectedParsedTopics = include __DIR__ . '/_files/valid_communication_expected.php';
  62. $this->assertEquals($expectedParsedTopics['customerCreated'], $topics);
  63. }
  64. /**
  65. * Get topic configuration by its name
  66. *
  67. * @expectedException \Magento\Framework\Exception\LocalizedException
  68. * @expectedExceptionMessage Topic "invalidTopic" is not configured.
  69. */
  70. public function testGetTopicInvalidName()
  71. {
  72. $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('invalidTopic');
  73. }
  74. /**
  75. * @expectedException \LogicException
  76. * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerUpdated"
  77. */
  78. public function testGetTopicsExceptionMissingRequest()
  79. {
  80. $this->getConfigInstance(__DIR__ . '/_files/communication_missing_request.xml')->getTopics();
  81. }
  82. /**
  83. * @expectedException \LogicException
  84. * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
  85. */
  86. public function testGetTopicsExceptionNotExistingServiceMethod()
  87. {
  88. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service_method.xml')->getTopics();
  89. }
  90. /**
  91. * @expectedException \LogicException
  92. * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
  93. */
  94. public function testGetTopicsExceptionNotExistingService()
  95. {
  96. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service.xml')->getTopics();
  97. }
  98. /**
  99. * @expectedException \LogicException
  100. * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerRetrieved"
  101. */
  102. public function testGetTopicsExceptionNoAttributes()
  103. {
  104. $this->getConfigInstance(__DIR__ . '/_files/communication_no_attributes.xml')->getTopics();
  105. }
  106. /**
  107. * @expectedException \LogicException
  108. * @expectedExceptionMessage Response schema definition for topic "customerUpdated" should reference existing
  109. */
  110. public function testGetTopicsExceptionInvalidResponseSchema()
  111. {
  112. $this->getConfigInstance(__DIR__ . '/_files/communication_response_not_existing_service.xml')->getTopics();
  113. }
  114. /**
  115. * @expectedException \LogicException
  116. * @expectedExceptionMessage Request schema definition for topic "customerUpdated" should reference existing
  117. */
  118. public function testGetTopicsExceptionInvalidRequestSchema()
  119. {
  120. $this->getConfigInstance(__DIR__ . '/_files/communication_request_not_existing_service.xml')->getTopics();
  121. }
  122. /**
  123. * @expectedException \LogicException
  124. * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
  125. */
  126. public function testGetTopicsExceptionMultipleHandlersSynchronousMode()
  127. {
  128. $this->getConfigInstance(__DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.xml')->getTopics();
  129. }
  130. /**
  131. * @expectedException \LogicException
  132. * @expectedExceptionMessage Service method specified in the definition of handler "customHandler" for topic "custo
  133. */
  134. public function testGetTopicsExceptionInvalidHandler()
  135. {
  136. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_handler_method.xml')->getTopics();
  137. }
  138. /**
  139. * @expectedException \LogicException
  140. * @expectedExceptionMessage Topic name "customerAdded" and attribute "name" = "customerCreated" must be equal
  141. */
  142. public function testGetTopicsExceptionInvalidTopicNameInEnv()
  143. {
  144. $this->getConfigInstance(
  145. __DIR__ . '/_files/valid_communication.xml',
  146. __DIR__ . '/_files/communication_invalid_topic_name.php'
  147. )->getTopics();
  148. }
  149. /**
  150. * @expectedException \LogicException
  151. * @expectedExceptionMessage Topic "customerCreated" must contain data
  152. */
  153. public function testGetTopicsExceptionTopicWithoutDataInEnv()
  154. {
  155. $this->getConfigInstance(
  156. __DIR__ . '/_files/valid_communication.xml',
  157. __DIR__ . '/_files/communication_topic_without_data.php'
  158. )->getTopics();
  159. }
  160. /**
  161. * @expectedException \LogicException
  162. * @expectedExceptionMessage Topic "customerCreated" has missed keys: [response]
  163. */
  164. public function testGetTopicsExceptionTopicWithMissedKeysInEnv()
  165. {
  166. $this->getConfigInstance(
  167. __DIR__ . '/_files/valid_communication.xml',
  168. __DIR__ . '/_files/communication_topic_with_missed_keys.php'
  169. )->getTopics();
  170. }
  171. /**
  172. * @expectedException \LogicException
  173. * @expectedExceptionMessage Topic "customerCreated" has excessive keys: [some_incorrect_key]
  174. */
  175. public function testGetTopicsExceptionTopicWithExcessiveKeysInEnv()
  176. {
  177. $this->getConfigInstance(
  178. __DIR__ . '/_files/valid_communication.xml',
  179. __DIR__ . '/_files/communication_topic_with_excessive_keys.php'
  180. )->getTopics();
  181. }
  182. /**
  183. * @expectedException \LogicException
  184. * @expectedExceptionMessage Topic name "customerDeleted" and attribute "name" = "customerRemoved" must be equal
  185. */
  186. public function testGetTopicsExceptionTopicWithNonMatchedNameInEnv()
  187. {
  188. $this->getConfigInstance(
  189. __DIR__ . '/_files/valid_communication.xml',
  190. __DIR__ . '/_files/communication_with_non_matched_name.php'
  191. )->getTopics();
  192. }
  193. /**
  194. * @expectedException \LogicException
  195. * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
  196. */
  197. public function testGetTopicsExceptionMultipleHandlersSynchronousModeInEnv()
  198. {
  199. $this->getConfigInstance(
  200. __DIR__ . '/_files/valid_communication.xml',
  201. __DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.php'
  202. )->getTopics();
  203. }
  204. /**
  205. * @expectedException \LogicException
  206. * @expectedExceptionMessage Request schema definition for topic "customerCreated" should reference existing service
  207. */
  208. public function testGetTopicsExceptionInvalidRequestSchemaInEnv()
  209. {
  210. $this->getConfigInstance(
  211. __DIR__ . '/_files/valid_communication.xml',
  212. __DIR__ . '/_files/communication_request_not_existing_service.php'
  213. )->getTopics();
  214. }
  215. /**
  216. * @expectedException \LogicException
  217. * @expectedExceptionMessage Response schema definition for topic "customerCreated" should reference existing type o
  218. */
  219. public function testGetTopicsExceptionInvalidResponseSchemaInEnv()
  220. {
  221. $this->getConfigInstance(
  222. __DIR__ . '/_files/valid_communication.xml',
  223. __DIR__ . '/_files/communication_response_not_existing_service.php'
  224. )->getTopics();
  225. }
  226. /**
  227. * @expectedException \LogicException
  228. * @expectedExceptionMessage Service method specified in the definition of handler "customerCreatedFirst" for topic
  229. */
  230. public function testGetTopicsExceptionInvalidMethodInHandlerInEnv()
  231. {
  232. $this->getConfigInstance(
  233. __DIR__ . '/_files/valid_communication.xml',
  234. __DIR__ . '/_files/communication_not_existing_handler_method.php'
  235. )->getTopics();
  236. }
  237. /**
  238. * @expectedException \LogicException
  239. * @expectedExceptionMessage Disabled handler "default" for topic "customerCreated" cannot be added to the config fi
  240. */
  241. public function testGetTopicsExceptionWithDisabledHandlerInEnv()
  242. {
  243. $this->getConfigInstance(
  244. __DIR__ . '/_files/valid_communication.xml',
  245. __DIR__ . '/_files/communication_with_disabled_handler.php'
  246. )->getTopics();
  247. }
  248. /**
  249. * @expectedException \LogicException
  250. * @expectedExceptionMessage Request schema type for topic "customerCreated" must be "object_interface" or "service_
  251. */
  252. public function testGetTopicsExceptionIncorrectRequestSchemaTypeInEnv()
  253. {
  254. $this->getConfigInstance(
  255. __DIR__ . '/_files/valid_communication.xml',
  256. __DIR__ . '/_files/communication_incorrect_request_schema_type.php'
  257. )->getTopics();
  258. }
  259. /**
  260. * @expectedException \LogicException
  261. * @expectedExceptionMessage The attribute "is_synchronous" for topic "customerCreated" should have the value of the
  262. */
  263. public function testGetTopicsExceptionIsNotBooleanTypeOfIsSynchronousInEnv()
  264. {
  265. $this->getConfigInstance(
  266. __DIR__ . '/_files/valid_communication.xml',
  267. __DIR__ . '/_files/communication_is_synchronous_is_not_boolean.php'
  268. )->getTopics();
  269. }
  270. /**
  271. * Create config instance initialized with configuration from $configFilePath
  272. *
  273. * @param string $configFilePath
  274. * @param string|null $envConfigFilePath
  275. * @return \Magento\Framework\Communication\ConfigInterface
  276. */
  277. protected function getConfigInstance($configFilePath, $envConfigFilePath = null)
  278. {
  279. $fileResolver = $this->getMockForAbstractClass(\Magento\Framework\Config\FileResolverInterface::class);
  280. $fileResolver->expects($this->any())
  281. ->method('get')
  282. ->willReturn([file_get_contents($configFilePath)]);
  283. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  284. $xmlReader = $objectManager->create(
  285. \Magento\Framework\Communication\Config\Reader\XmlReader::class,
  286. ['fileResolver' => $fileResolver]
  287. );
  288. $deploymentConfigReader = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig\Reader::class)
  289. ->disableOriginalConstructor()
  290. ->setMethods([])
  291. ->getMock();
  292. $envConfigData = include $envConfigFilePath ?: __DIR__ . '/_files/valid_communication_input.php';
  293. $deploymentConfigReader->expects($this->any())->method('load')->willReturn($envConfigData);
  294. $deploymentConfig = $objectManager->create(
  295. \Magento\Framework\App\DeploymentConfig::class,
  296. ['reader' => $deploymentConfigReader]
  297. );
  298. $methodsMap = $objectManager->create(\Magento\Framework\Reflection\MethodsMap::class);
  299. $envReader = $objectManager->create(
  300. \Magento\Framework\Communication\Config\Reader\EnvReader::class,
  301. [
  302. 'deploymentConfig' => $deploymentConfig,
  303. 'methodsMap' => $methodsMap
  304. ]
  305. );
  306. $readersConfig = [
  307. 'xmlReader' => ['reader' => $xmlReader, 'sortOrder' => 10],
  308. 'envReader' => ['reader' => $envReader, 'sortOrder' => 20]
  309. ];
  310. /** @var \Magento\Framework\Communication\Config\CompositeReader $reader */
  311. $reader = $objectManager->create(
  312. \Magento\Framework\Communication\Config\CompositeReader::class,
  313. ['readers' => $readersConfig]
  314. );
  315. /** @var \Magento\Framework\Communication\Config $config */
  316. $configData = $objectManager->create(
  317. \Magento\Framework\Communication\Config\Data::class,
  318. [
  319. 'reader' => $reader
  320. ]
  321. );
  322. return $objectManager->create(
  323. \Magento\Framework\Communication\ConfigInterface::class,
  324. ['configData' => $configData]
  325. );
  326. }
  327. }