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

/protected/modules/yupe/commands/TestEnvCommand.php

https://gitlab.com/RonLab1987/YupePlusClear
PHP | 182 lines | 99 code | 18 blank | 65 comment | 5 complexity | 29e6ceba878034c92f3d14c2d73aa95e MD5 | raw file
  1. <?php
  2. /**
  3. * TestEnvCommand
  4. *
  5. * Console application for create and manage testing environment.
  6. * @package yupe.commands
  7. * @author Anton Kucherov <idexter.ru@gmail.com>
  8. * @author YupeTeam <team@yupe.ru>
  9. * @link http://yupe.ru
  10. */
  11. /*
  12. *
  13. * @TODO добавить возможность указать параметры для Selenium2 драйвера, можно ограичиться только url
  14. *
  15. **/
  16. /**
  17. * Class TestEnvCommand
  18. */
  19. class TestEnvCommand extends CConsoleCommand
  20. {
  21. /**
  22. *
  23. */
  24. const COMMAND_DIR = __DIR__;
  25. /**
  26. *
  27. */
  28. const ROOT_DIR = "/../..";
  29. /**
  30. *
  31. */
  32. const CONFIG_DIR = "/../config";
  33. /**
  34. *
  35. */
  36. const TESTS_DIR = "/../../tests";
  37. /**
  38. * @var array
  39. */
  40. public $dbOptions = ['dbname' => 'yupe_test', 'dbuser' => 'root', 'dbpass' => ''];
  41. /**
  42. *
  43. */
  44. public function actionIndex()
  45. {
  46. echo self::COMMAND_DIR."\n";
  47. echo self::COMMAND_DIR.self::ROOT_DIR."\n";
  48. echo self::COMMAND_DIR.self::CONFIG_DIR."\n";
  49. echo self::COMMAND_DIR.self::TESTS_DIR."\n";
  50. $this->readOptions();
  51. }
  52. /**
  53. *
  54. */
  55. public function actionCreate()
  56. {
  57. $this->createConfigFile(
  58. self::COMMAND_DIR.self::CONFIG_DIR,
  59. 'db-test.php',
  60. 'db.back.php'
  61. );
  62. $this->createConfigFile(
  63. self::COMMAND_DIR.self::ROOT_DIR,
  64. 'codeception.yml',
  65. 'codeception.dist.yml'
  66. );
  67. $this->createConfigFile(
  68. self::COMMAND_DIR.self::TESTS_DIR,
  69. 'acceptance.suite.yml',
  70. 'acceptance.suite.dist.yml'
  71. );
  72. $this->createConfigFile(
  73. self::COMMAND_DIR.self::TESTS_DIR,
  74. 'functional.suite.yml',
  75. 'functional.suite.dist.yml'
  76. );
  77. $this->createConfigFile(
  78. self::COMMAND_DIR.self::TESTS_DIR,
  79. 'unit.suite.yml',
  80. 'unit.suite.dist.yml'
  81. );
  82. $this->readOptions();
  83. $this->replaceDbOptionsInConfig(self::COMMAND_DIR.self::CONFIG_DIR."/db-test.php");
  84. $this->replaceDbOptionsInConfig(self::COMMAND_DIR.self::ROOT_DIR."/codeception.yml");
  85. $this->replaceDbOptionsInConfig(self::COMMAND_DIR.self::TESTS_DIR."/acceptance.suite.yml");
  86. $this->replaceDbOptionsInConfig(self::COMMAND_DIR.self::TESTS_DIR."/functional.suite.yml");
  87. $this->replaceDbOptionsInConfig(self::COMMAND_DIR.self::TESTS_DIR."/unit.suite.yml");
  88. }
  89. /**
  90. *
  91. */
  92. public function actionReset()
  93. {
  94. $this->removeConfigFile(self::COMMAND_DIR.self::CONFIG_DIR."/db-test.php");
  95. $this->removeConfigFile(self::COMMAND_DIR.self::ROOT_DIR."/codeception.yml");
  96. $this->removeConfigFile(self::COMMAND_DIR.self::TESTS_DIR."/acceptance.suite.yml");
  97. $this->removeConfigFile(self::COMMAND_DIR.self::TESTS_DIR."/functional.suite.yml");
  98. $this->removeConfigFile(self::COMMAND_DIR.self::TESTS_DIR."/unit.suite.yml");
  99. }
  100. /**
  101. * Creates config file from source file.
  102. * @param $dir string Config directory
  103. * @param $fileName string Config filename
  104. * @param $distFileName string SourceConfig filename
  105. * @return bool True if file was created, False if not
  106. */
  107. private function createConfigFile($dir, $fileName, $distFileName)
  108. {
  109. $config = $dir."/".$fileName;
  110. if (!file_exists($config)) {
  111. return copy($dir.'/'.$distFileName, $config);
  112. }
  113. return false;
  114. }
  115. /**
  116. * Reads options for environment from console.
  117. */
  118. private function readOptions()
  119. {
  120. echo "Enter name of testing DB (default: yupe_test): ";
  121. $this->readFromConsole($this->dbOptions["dbname"]);
  122. echo "Enter login for testing DB (default: root): ";
  123. $this->readFromConsole($this->dbOptions["dbuser"]);
  124. echo "Enter password for testing DB (default: ): ";
  125. $this->readFromConsole($this->dbOptions["dbpass"]);
  126. }
  127. /**
  128. * Reads line from the console and writes it to variable.
  129. * @param $value string.
  130. */
  131. private function readFromConsole(&$value)
  132. {
  133. $stdin = substr(trim(fgets(STDIN)), 0, 50);
  134. $value = (strlen($stdin) > 0) ? $stdin : $value;
  135. }
  136. /**
  137. * Replace DataBase config parameters in config files
  138. * @param $file string Config File Name
  139. * @return bool True if replaced, False if not
  140. */
  141. private function replaceDbOptionsInConfig($file)
  142. {
  143. if (file_exists($file)) {
  144. $fileContents = file_get_contents($file, FILE_TEXT);
  145. $fileContents = preg_replace('/<db\.name>/is', $this->dbOptions['dbname'], $fileContents);
  146. $fileContents = preg_replace('/<db\.user>/is', $this->dbOptions['dbuser'], $fileContents);
  147. $fileContents = preg_replace('/<db\.pass>/is', $this->dbOptions['dbpass'], $fileContents);
  148. return (bool)file_put_contents($file, $fileContents, FILE_TEXT);
  149. }
  150. return false;
  151. }
  152. /**
  153. * Remove file if it exists
  154. * @param $file string File name
  155. * @return bool True if file removed of not exists, False in another case
  156. */
  157. private function removeConfigFile($file)
  158. {
  159. if (file_exists($file)) {
  160. return unlink($file);
  161. }
  162. return true;
  163. }
  164. }