ConfigTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\ResourceConnection;
  7. use Magento\Framework\Config\ConfigOptionsListConstants;
  8. class ConfigTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\ResourceConnection\Config
  12. */
  13. private $config;
  14. /**
  15. * @var \Magento\Framework\Config\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $scopeMock;
  18. /**
  19. * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $cacheMock;
  22. /**
  23. * @var \Magento\Framework\App\ResourceConnection\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $readerMock;
  26. /**
  27. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $serializerMock;
  30. /**
  31. * @var array
  32. */
  33. private $resourcesConfig;
  34. /**
  35. * @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $deploymentConfig;
  38. protected function setUp()
  39. {
  40. $this->scopeMock = $this->createMock(\Magento\Framework\Config\ScopeInterface::class);
  41. $this->cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  42. $this->readerMock = $this->createMock(\Magento\Framework\App\ResourceConnection\Config\Reader::class);
  43. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  44. $this->resourcesConfig = [
  45. 'mainResourceName' => ['name' => 'mainResourceName', 'extends' => 'anotherResourceName'],
  46. 'otherResourceName' => ['name' => 'otherResourceName', 'connection' => 'otherConnectionName'],
  47. 'anotherResourceName' => ['name' => 'anotherResourceName', 'connection' => 'anotherConnection'],
  48. 'brokenResourceName' => ['name' => 'brokenResourceName', 'extends' => 'absentResourceName'],
  49. 'extendedResourceName' => ['name' => 'extendedResourceName', 'extends' => 'validResource'],
  50. ];
  51. $serializedData = 'serialized data';
  52. $this->cacheMock->expects($this->any())
  53. ->method('load')
  54. ->willReturn($serializedData);
  55. $this->serializerMock->method('unserialize')
  56. ->with($serializedData)
  57. ->willReturn($this->resourcesConfig);
  58. $this->deploymentConfig = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  59. $this->config = new \Magento\Framework\App\ResourceConnection\Config(
  60. $this->readerMock,
  61. $this->scopeMock,
  62. $this->cacheMock,
  63. $this->deploymentConfig,
  64. 'cacheId',
  65. $this->serializerMock
  66. );
  67. }
  68. /**
  69. * @param string $resourceName
  70. * @param string $connectionName
  71. * @dataProvider getConnectionNameDataProvider
  72. */
  73. public function testGetConnectionName($resourceName, $connectionName)
  74. {
  75. $this->deploymentConfig->expects($this->once())
  76. ->method('getConfigData')
  77. ->with(ConfigOptionsListConstants::KEY_RESOURCE)
  78. ->willReturn([
  79. 'validResource' => ['connection' => 'validConnectionName'],
  80. ]);
  81. $this->assertEquals($connectionName, $this->config->getConnectionName($resourceName));
  82. }
  83. /**
  84. * @expectedException \InvalidArgumentException
  85. */
  86. public function testGetConnectionNameWithException()
  87. {
  88. $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  89. $deploymentConfigMock->expects($this->once())
  90. ->method('getConfigData')
  91. ->with(ConfigOptionsListConstants::KEY_RESOURCE)
  92. ->willReturn(['validResource' => ['somekey' => 'validConnectionName']]);
  93. $config = new \Magento\Framework\App\ResourceConnection\Config(
  94. $this->readerMock,
  95. $this->scopeMock,
  96. $this->cacheMock,
  97. $deploymentConfigMock,
  98. 'cacheId',
  99. $this->serializerMock
  100. );
  101. $config->getConnectionName('default');
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getConnectionNameDataProvider()
  107. {
  108. return [
  109. ['resourceName' => 'otherResourceName', 'connectionName' => 'otherConnectionName'],
  110. ['resourceName' => 'mainResourceName', 'connectionName' => 'anotherConnection'],
  111. [
  112. 'resourceName' => 'brokenResourceName',
  113. 'connectionName' => \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
  114. ],
  115. ['resourceName' => 'extendedResourceName', 'connectionName' => 'validConnectionName'],
  116. ['resourceName' => 'validResource', 'connectionName' => 'validConnectionName']
  117. ];
  118. }
  119. }