PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Framework/Communication/ConfigTest.php

https://gitlab.com/svillegas/magento2
PHP | 319 lines | 185 code | 24 blank | 110 comment | 0 complexity | a77841cc71fceec3089aab8b16267b72 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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. public function testGetTopic()
  27. {
  28. $topics = $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('customerCreated');
  29. $expectedParsedTopics = include __DIR__ . '/_files/valid_communication_expected.php';
  30. $this->assertEquals($expectedParsedTopics['customerCreated'], $topics);
  31. }
  32. /**
  33. * Get topic configuration by its name
  34. *
  35. * @expectedException \Magento\Framework\Exception\LocalizedException
  36. * @expectedExceptionMessage Topic "invalidTopic" is not configured.
  37. */
  38. public function testGetTopicInvalidName()
  39. {
  40. $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('invalidTopic');
  41. }
  42. /**
  43. * @expectedException \LogicException
  44. * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerUpdated"
  45. */
  46. public function testGetTopicsExceptionMissingRequest()
  47. {
  48. $this->getConfigInstance(__DIR__ . '/_files/communication_missing_request.xml')->getTopics();
  49. }
  50. /**
  51. * @expectedException \LogicException
  52. * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
  53. */
  54. public function testGetTopicsExceptionNotExistingServiceMethod()
  55. {
  56. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service_method.xml')->getTopics();
  57. }
  58. /**
  59. * @expectedException \LogicException
  60. * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
  61. */
  62. public function testGetTopicsExceptionNotExistingService()
  63. {
  64. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service.xml')->getTopics();
  65. }
  66. /**
  67. * @expectedException \LogicException
  68. * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerRetrieved"
  69. */
  70. public function testGetTopicsExceptionNoAttributes()
  71. {
  72. $this->getConfigInstance(__DIR__ . '/_files/communication_no_attributes.xml')->getTopics();
  73. }
  74. /**
  75. * @expectedException \LogicException
  76. * @expectedExceptionMessage Response schema definition for topic "customerUpdated" should reference existing
  77. */
  78. public function testGetTopicsExceptionInvalidResponseSchema()
  79. {
  80. $this->getConfigInstance(__DIR__ . '/_files/communication_response_not_existing_service.xml')->getTopics();
  81. }
  82. /**
  83. * @expectedException \LogicException
  84. * @expectedExceptionMessage Request schema definition for topic "customerUpdated" should reference existing
  85. */
  86. public function testGetTopicsExceptionInvalidRequestSchema()
  87. {
  88. $this->getConfigInstance(__DIR__ . '/_files/communication_request_not_existing_service.xml')->getTopics();
  89. }
  90. /**
  91. * @expectedException \LogicException
  92. * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
  93. */
  94. public function testGetTopicsExceptionMultipleHandlersSynchronousMode()
  95. {
  96. $this->getConfigInstance(__DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.xml')->getTopics();
  97. }
  98. /**
  99. * @expectedException \LogicException
  100. * @expectedExceptionMessage Service method specified in the definition of handler "customHandler" for topic "custo
  101. */
  102. public function testGetTopicsExceptionInvalidHandler()
  103. {
  104. $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_handler_method.xml')->getTopics();
  105. }
  106. /**
  107. * @expectedException \LogicException
  108. * @expectedExceptionMessage Topic name "customerAdded" and attribute "name" = "customerCreated" must be equal
  109. */
  110. public function testGetTopicsExceptionInvalidTopicNameInEnv()
  111. {
  112. $this->getConfigInstance(
  113. __DIR__ . '/_files/valid_communication.xml',
  114. __DIR__ . '/_files/communication_invalid_topic_name.php'
  115. )->getTopics();
  116. }
  117. /**
  118. * @expectedException \LogicException
  119. * @expectedExceptionMessage Topic "customerCreated" must contain data
  120. */
  121. public function testGetTopicsExceptionTopicWithoutDataInEnv()
  122. {
  123. $this->getConfigInstance(
  124. __DIR__ . '/_files/valid_communication.xml',
  125. __DIR__ . '/_files/communication_topic_without_data.php'
  126. )->getTopics();
  127. }
  128. /**
  129. * @expectedException \LogicException
  130. * @expectedExceptionMessage Topic "customerCreated" has missed keys: [response]
  131. */
  132. public function testGetTopicsExceptionTopicWithMissedKeysInEnv()
  133. {
  134. $this->getConfigInstance(
  135. __DIR__ . '/_files/valid_communication.xml',
  136. __DIR__ . '/_files/communication_topic_with_missed_keys.php'
  137. )->getTopics();
  138. }
  139. /**
  140. * @expectedException \LogicException
  141. * @expectedExceptionMessage Topic "customerCreated" has excessive keys: [some_incorrect_key]
  142. */
  143. public function testGetTopicsExceptionTopicWithExcessiveKeysInEnv()
  144. {
  145. $this->getConfigInstance(
  146. __DIR__ . '/_files/valid_communication.xml',
  147. __DIR__ . '/_files/communication_topic_with_excessive_keys.php'
  148. )->getTopics();
  149. }
  150. /**
  151. * @expectedException \LogicException
  152. * @expectedExceptionMessage Topic name "customerDeleted" and attribute "name" = "customerRemoved" must be equal
  153. */
  154. public function testGetTopicsExceptionTopicWithNonMatchedNameInEnv()
  155. {
  156. $this->getConfigInstance(
  157. __DIR__ . '/_files/valid_communication.xml',
  158. __DIR__ . '/_files/communication_with_non_matched_name.php'
  159. )->getTopics();
  160. }
  161. /**
  162. * @expectedException \LogicException
  163. * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
  164. */
  165. public function testGetTopicsExceptionMultipleHandlersSynchronousModeInEnv()
  166. {
  167. $this->getConfigInstance(
  168. __DIR__ . '/_files/valid_communication.xml',
  169. __DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.php'
  170. )->getTopics();
  171. }
  172. /**
  173. * @expectedException \LogicException
  174. * @expectedExceptionMessage Request schema definition for topic "customerCreated" should reference existing service
  175. */
  176. public function testGetTopicsExceptionInvalidRequestSchemaInEnv()
  177. {
  178. $this->getConfigInstance(
  179. __DIR__ . '/_files/valid_communication.xml',
  180. __DIR__ . '/_files/communication_request_not_existing_service.php'
  181. )->getTopics();
  182. }
  183. /**
  184. * @expectedException \LogicException
  185. * @expectedExceptionMessage Response schema definition for topic "customerCreated" should reference existing type o
  186. */
  187. public function testGetTopicsExceptionInvalidResponseSchemaInEnv()
  188. {
  189. $this->getConfigInstance(
  190. __DIR__ . '/_files/valid_communication.xml',
  191. __DIR__ . '/_files/communication_response_not_existing_service.php'
  192. )->getTopics();
  193. }
  194. /**
  195. * @expectedException \LogicException
  196. * @expectedExceptionMessage Service method specified in the definition of handler "customerCreatedFirst" for topic
  197. */
  198. public function testGetTopicsExceptionInvalidMethodInHandlerInEnv()
  199. {
  200. $this->getConfigInstance(
  201. __DIR__ . '/_files/valid_communication.xml',
  202. __DIR__ . '/_files/communication_not_existing_handler_method.php'
  203. )->getTopics();
  204. }
  205. /**
  206. * @expectedException \LogicException
  207. * @expectedExceptionMessage Disabled handler "default" for topic "customerCreated" cannot be added to the config fi
  208. */
  209. public function testGetTopicsExceptionWithDisabledHandlerInEnv()
  210. {
  211. $this->getConfigInstance(
  212. __DIR__ . '/_files/valid_communication.xml',
  213. __DIR__ . '/_files/communication_with_disabled_handler.php'
  214. )->getTopics();
  215. }
  216. /**
  217. * @expectedException \LogicException
  218. * @expectedExceptionMessage Request schema type for topic "customerCreated" must be "object_interface" or "service_
  219. */
  220. public function testGetTopicsExceptionIncorrectRequestSchemaTypeInEnv()
  221. {
  222. $this->getConfigInstance(
  223. __DIR__ . '/_files/valid_communication.xml',
  224. __DIR__ . '/_files/communication_incorrect_request_schema_type.php'
  225. )->getTopics();
  226. }
  227. /**
  228. * @expectedException \LogicException
  229. * @expectedExceptionMessage The attribute "is_synchronous" for topic "customerCreated" should have the value of the
  230. */
  231. public function testGetTopicsExceptionIsNotBooleanTypeOfIsSynchronousInEnv()
  232. {
  233. $this->getConfigInstance(
  234. __DIR__ . '/_files/valid_communication.xml',
  235. __DIR__ . '/_files/communication_is_synchronous_is_not_boolean.php'
  236. )->getTopics();
  237. }
  238. /**
  239. * Create config instance initialized with configuration from $configFilePath
  240. *
  241. * @param string $configFilePath
  242. * @param string|null $envConfigFilePath
  243. * @return \Magento\Framework\Communication\ConfigInterface
  244. */
  245. protected function getConfigInstance($configFilePath, $envConfigFilePath = null)
  246. {
  247. $fileResolver = $this->getMockForAbstractClass(\Magento\Framework\Config\FileResolverInterface::class);
  248. $fileResolver->expects($this->any())
  249. ->method('get')
  250. ->willReturn([file_get_contents($configFilePath)]);
  251. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  252. $xmlReader = $objectManager->create(
  253. \Magento\Framework\Communication\Config\Reader\XmlReader::class,
  254. ['fileResolver' => $fileResolver]
  255. );
  256. $deploymentConfigReader = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig\Reader::class)
  257. ->disableOriginalConstructor()
  258. ->setMethods([])
  259. ->getMock();
  260. $envConfigData = include $envConfigFilePath ?: __DIR__ . '/_files/valid_communication_input.php';
  261. $deploymentConfigReader->expects($this->any())->method('load')->willReturn($envConfigData);
  262. $deploymentConfig = $objectManager->create(
  263. \Magento\Framework\App\DeploymentConfig::class,
  264. ['reader' => $deploymentConfigReader]
  265. );
  266. $methodsMap = $objectManager->create(\Magento\Framework\Reflection\MethodsMap::class);
  267. $envReader = $objectManager->create(
  268. \Magento\Framework\Communication\Config\Reader\EnvReader::class,
  269. [
  270. 'deploymentConfig' => $deploymentConfig,
  271. 'methodsMap' => $methodsMap
  272. ]
  273. );
  274. $readersConfig = [
  275. 'xmlReader' => ['reader' => $xmlReader, 'sortOrder' => 10],
  276. 'envReader' => ['reader' => $envReader, 'sortOrder' => 20]
  277. ];
  278. /** @var \Magento\Framework\Communication\Config\CompositeReader $reader */
  279. $reader = $objectManager->create(
  280. \Magento\Framework\Communication\Config\CompositeReader::class,
  281. ['readers' => $readersConfig]
  282. );
  283. /** @var \Magento\Framework\Communication\Config $config */
  284. $configData = $objectManager->create(
  285. \Magento\Framework\Communication\Config\Data::class,
  286. [
  287. 'reader' => $reader
  288. ]
  289. );
  290. return $objectManager->create(
  291. \Magento\Framework\Communication\ConfigInterface::class,
  292. ['configData' => $configData]
  293. );
  294. }
  295. }