ResourceConnectionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit\App;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\App\ResourceConnection\ConfigInterface;
  10. use Magento\Framework\Config\ConfigOptionsListConstants;
  11. use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. class ResourceConnectionTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var ResourceConnection
  17. */
  18. private $unit;
  19. /**
  20. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $deploymentConfigMock;
  23. /**
  24. * @var ConnectionFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $connectionFactoryMock;
  27. /**
  28. * @var ObjectManager
  29. */
  30. private $objectManager;
  31. /**
  32. * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $configMock;
  35. protected function setUp()
  36. {
  37. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->connectionFactoryMock = $this->getMockBuilder(ConnectionFactoryInterface::class)
  41. ->getMock();
  42. $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMock();
  43. $this->objectManager = (new ObjectManager($this));
  44. $this->unit = $this->objectManager->getObject(
  45. ResourceConnection::class,
  46. [
  47. 'deploymentConfig' => $this->deploymentConfigMock,
  48. 'connectionFactory' => $this->connectionFactoryMock,
  49. 'config' => $this->configMock,
  50. ]
  51. );
  52. }
  53. public function testGetTablePrefixWithInjectedPrefix()
  54. {
  55. /** @var ResourceConnection $resourceConnection */
  56. $resourceConnection = $this->objectManager->getObject(
  57. ResourceConnection::class,
  58. [
  59. 'deploymentConfig' => $this->deploymentConfigMock,
  60. 'connectionFactory' => $this->connectionFactoryMock,
  61. 'config' => $this->configMock,
  62. 'tablePrefix' => 'some_prefix'
  63. ]
  64. );
  65. self::assertEquals($resourceConnection->getTablePrefix(), 'some_prefix');
  66. }
  67. public function testGetTablePrefix()
  68. {
  69. $this->deploymentConfigMock->expects(self::once())
  70. ->method('get')
  71. ->with(ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX)
  72. ->willReturn('pref_');
  73. self::assertEquals('pref_', $this->unit->getTablePrefix());
  74. }
  75. public function testGetConnectionByName()
  76. {
  77. $this->deploymentConfigMock->expects(self::once())->method('get')
  78. ->with(ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/default')
  79. ->willReturn(['config']);
  80. $this->connectionFactoryMock->expects(self::once())->method('create')
  81. ->with(['config'])
  82. ->willReturn('connection');
  83. self::assertEquals('connection', $this->unit->getConnectionByName('default'));
  84. }
  85. public function testGetExistingConnectionByName()
  86. {
  87. $unit = $this->objectManager->getObject(
  88. ResourceConnection::class,
  89. [
  90. 'deploymentConfig' => $this->deploymentConfigMock,
  91. 'connections' => ['default_process_' . getmypid() => 'existing_connection']
  92. ]
  93. );
  94. $this->deploymentConfigMock->expects(self::never())->method('get');
  95. self::assertEquals('existing_connection', $unit->getConnectionByName('default'));
  96. }
  97. public function testCloseConnection()
  98. {
  99. $this->configMock->expects(self::once())->method('getConnectionName')->with('default');
  100. $this->unit->closeConnection('default');
  101. }
  102. }