AbstractDb.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Abstract database handler for integration tests
  8. */
  9. namespace Magento\TestFramework\Db;
  10. abstract class AbstractDb
  11. {
  12. /**
  13. * DB host name
  14. *
  15. * @var string
  16. */
  17. protected $_host = '';
  18. /**
  19. * DB credentials -- user name
  20. *
  21. * @var string
  22. */
  23. protected $_user = '';
  24. /**
  25. * DB credentials -- password
  26. *
  27. * @var string
  28. */
  29. protected $_password = '';
  30. /**
  31. * DB name
  32. *
  33. * @var string
  34. */
  35. protected $_schema = '';
  36. /**
  37. * Path to a temporary directory in the file system
  38. *
  39. * @var string
  40. */
  41. protected $_varPath = '';
  42. /**
  43. * @var \Magento\Framework\Shell
  44. */
  45. protected $_shell;
  46. /**
  47. * Set initial essential parameters
  48. *
  49. * @param string $host
  50. * @param string $user
  51. * @param string $password
  52. * @param string $schema
  53. * @param string $varPath
  54. * @param \Magento\Framework\Shell $shell
  55. */
  56. public function __construct($host, $user, $password, $schema, $varPath, \Magento\Framework\Shell $shell)
  57. {
  58. $this->_host = $host;
  59. $this->_user = $user;
  60. $this->_password = $password;
  61. $this->_schema = $schema;
  62. $this->_varPath = $varPath;
  63. $this->_shell = $shell;
  64. }
  65. /**
  66. * Remove all DB objects
  67. */
  68. abstract public function cleanup();
  69. /**
  70. * Get filename for setup db dump
  71. *
  72. * @return string
  73. */
  74. abstract protected function getSetupDbDumpFilename();
  75. /**
  76. * Is dump exists
  77. *
  78. * @return bool
  79. */
  80. abstract public function isDbDumpExists();
  81. /**
  82. * Store setup db dump
  83. */
  84. abstract public function storeDbDump();
  85. /**
  86. * Restore db from setup db dump
  87. */
  88. abstract public function restoreFromDbDump();
  89. /**
  90. * @return string
  91. */
  92. abstract public function getVendorName();
  93. /**
  94. * @return string
  95. */
  96. public function getSchema()
  97. {
  98. return $this->_schema;
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function getHost()
  104. {
  105. return $this->_host;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getUser()
  111. {
  112. return $this->_user;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getPassword()
  118. {
  119. return $this->_password;
  120. }
  121. /**
  122. * Create file with sql script content.
  123. * Utility method that is used in children classes
  124. *
  125. * @param string $file
  126. * @param string $content
  127. * @return int
  128. */
  129. protected function _createScript($file, $content)
  130. {
  131. return file_put_contents($file, $content);
  132. }
  133. /**
  134. * @throws \LogicException
  135. */
  136. protected function assertVarPathWritable()
  137. {
  138. if (!is_dir($this->_varPath) || !is_writable($this->_varPath)) {
  139. throw new \LogicException("The specified '{$this->_varPath}' is not a directory or not writable.");
  140. }
  141. }
  142. }