123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Communication;
- /**
- * Test of communication configuration reading and parsing.
- *
- * @magentoCache config disabled
- */
- class ConfigTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * Check how valid communication XML config is parsed.
- */
- public function testGetTopics()
- {
- $topics = $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopics();
- $expectedParsedTopics = include __DIR__ . '/_files/valid_communication_expected.php';
- $this->assertEquals($expectedParsedTopics, $topics);
- }
- /**
- * Get topic configuration by its name
- *
- * @expectedException \LogicException
- * @expectedExceptionMessage Service method specified in the definition of topic "customerDeletedNumbers" is not av
- */
- public function testGetTopicsNumeric()
- {
- $this->getConfigInstance(__DIR__ . '/_files/valid_communication_numeric.xml')->getTopics();
- }
- // @codingStandardsIgnoreStart
- /**
- * Get topic configuration by its name
- *
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage The XML in file "0" is invalid:
- 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]+'.
- Line: 9
- Element 'topic', attribute 'schema': '55\Customer\Api\CustomerRepositoryInterface::delete' is not a valid value of the atomic type 'schemaType'.
- Line: 9
- 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\\]+'.
- Line: 10
- Element 'handler', attribute 'type': '55\Customer\Api\CustomerRepositoryInterface' is not a valid value of the atomic type 'serviceTypeType'.
- Line: 10
- Verify the XML and try again.
- *
- */
- // @codingStandardsIgnoreEnd
- public function testGetTopicsNumericInvalid()
- {
- $this->getConfigInstance(__DIR__ . '/_files/invalid_communication_numeric.xml')->getTopics();
- }
- /**
- * Get topic configuration by its name
- */
- public function testGetTopic()
- {
- $topics = $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('customerCreated');
- $expectedParsedTopics = include __DIR__ . '/_files/valid_communication_expected.php';
- $this->assertEquals($expectedParsedTopics['customerCreated'], $topics);
- }
- /**
- * Get topic configuration by its name
- *
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage Topic "invalidTopic" is not configured.
- */
- public function testGetTopicInvalidName()
- {
- $this->getConfigInstance(__DIR__ . '/_files/valid_communication.xml')->getTopic('invalidTopic');
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerUpdated"
- */
- public function testGetTopicsExceptionMissingRequest()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_missing_request.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
- */
- public function testGetTopicsExceptionNotExistingServiceMethod()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service_method.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Service method specified in the definition of topic "customerRetrieved" is not
- */
- public function testGetTopicsExceptionNotExistingService()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_service.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Either "request" or "schema" attribute must be specified for topic "customerRetrieved"
- */
- public function testGetTopicsExceptionNoAttributes()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_no_attributes.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Response schema definition for topic "customerUpdated" should reference existing
- */
- public function testGetTopicsExceptionInvalidResponseSchema()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_response_not_existing_service.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Request schema definition for topic "customerUpdated" should reference existing
- */
- public function testGetTopicsExceptionInvalidRequestSchema()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_request_not_existing_service.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
- */
- public function testGetTopicsExceptionMultipleHandlersSynchronousMode()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Service method specified in the definition of handler "customHandler" for topic "custo
- */
- public function testGetTopicsExceptionInvalidHandler()
- {
- $this->getConfigInstance(__DIR__ . '/_files/communication_not_existing_handler_method.xml')->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic name "customerAdded" and attribute "name" = "customerCreated" must be equal
- */
- public function testGetTopicsExceptionInvalidTopicNameInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_invalid_topic_name.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic "customerCreated" must contain data
- */
- public function testGetTopicsExceptionTopicWithoutDataInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_topic_without_data.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic "customerCreated" has missed keys: [response]
- */
- public function testGetTopicsExceptionTopicWithMissedKeysInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_topic_with_missed_keys.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic "customerCreated" has excessive keys: [some_incorrect_key]
- */
- public function testGetTopicsExceptionTopicWithExcessiveKeysInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_topic_with_excessive_keys.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic name "customerDeleted" and attribute "name" = "customerRemoved" must be equal
- */
- public function testGetTopicsExceptionTopicWithNonMatchedNameInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_with_non_matched_name.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Topic "customerDeleted" is configured for synchronous requests, that is why it must
- */
- public function testGetTopicsExceptionMultipleHandlersSynchronousModeInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_multiple_handlers_synchronous_mode.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Request schema definition for topic "customerCreated" should reference existing service
- */
- public function testGetTopicsExceptionInvalidRequestSchemaInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_request_not_existing_service.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Response schema definition for topic "customerCreated" should reference existing type o
- */
- public function testGetTopicsExceptionInvalidResponseSchemaInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_response_not_existing_service.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Service method specified in the definition of handler "customerCreatedFirst" for topic
- */
- public function testGetTopicsExceptionInvalidMethodInHandlerInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_not_existing_handler_method.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Disabled handler "default" for topic "customerCreated" cannot be added to the config fi
- */
- public function testGetTopicsExceptionWithDisabledHandlerInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_with_disabled_handler.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage Request schema type for topic "customerCreated" must be "object_interface" or "service_
- */
- public function testGetTopicsExceptionIncorrectRequestSchemaTypeInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_incorrect_request_schema_type.php'
- )->getTopics();
- }
- /**
- * @expectedException \LogicException
- * @expectedExceptionMessage The attribute "is_synchronous" for topic "customerCreated" should have the value of the
- */
- public function testGetTopicsExceptionIsNotBooleanTypeOfIsSynchronousInEnv()
- {
- $this->getConfigInstance(
- __DIR__ . '/_files/valid_communication.xml',
- __DIR__ . '/_files/communication_is_synchronous_is_not_boolean.php'
- )->getTopics();
- }
- /**
- * Create config instance initialized with configuration from $configFilePath
- *
- * @param string $configFilePath
- * @param string|null $envConfigFilePath
- * @return \Magento\Framework\Communication\ConfigInterface
- */
- protected function getConfigInstance($configFilePath, $envConfigFilePath = null)
- {
- $fileResolver = $this->getMockForAbstractClass(\Magento\Framework\Config\FileResolverInterface::class);
- $fileResolver->expects($this->any())
- ->method('get')
- ->willReturn([file_get_contents($configFilePath)]);
- $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- $xmlReader = $objectManager->create(
- \Magento\Framework\Communication\Config\Reader\XmlReader::class,
- ['fileResolver' => $fileResolver]
- );
- $deploymentConfigReader = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig\Reader::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $envConfigData = include $envConfigFilePath ?: __DIR__ . '/_files/valid_communication_input.php';
- $deploymentConfigReader->expects($this->any())->method('load')->willReturn($envConfigData);
- $deploymentConfig = $objectManager->create(
- \Magento\Framework\App\DeploymentConfig::class,
- ['reader' => $deploymentConfigReader]
- );
- $methodsMap = $objectManager->create(\Magento\Framework\Reflection\MethodsMap::class);
- $envReader = $objectManager->create(
- \Magento\Framework\Communication\Config\Reader\EnvReader::class,
- [
- 'deploymentConfig' => $deploymentConfig,
- 'methodsMap' => $methodsMap
- ]
- );
- $readersConfig = [
- 'xmlReader' => ['reader' => $xmlReader, 'sortOrder' => 10],
- 'envReader' => ['reader' => $envReader, 'sortOrder' => 20]
- ];
- /** @var \Magento\Framework\Communication\Config\CompositeReader $reader */
- $reader = $objectManager->create(
- \Magento\Framework\Communication\Config\CompositeReader::class,
- ['readers' => $readersConfig]
- );
- /** @var \Magento\Framework\Communication\Config $config */
- $configData = $objectManager->create(
- \Magento\Framework\Communication\Config\Data::class,
- [
- 'reader' => $reader
- ]
- );
- return $objectManager->create(
- \Magento\Framework\Communication\ConfigInterface::class,
- ['configData' => $configData]
- );
- }
- }
|