PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/CliController.php

https://github.com/rnixik/selenium2php
PHP | 294 lines | 236 code | 21 blank | 37 comment | 43 complexity | ee383f0a6afbd72823e8b39d954efda1 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /*
  3. * Copyright 2013 Rnix Valentine
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Selenium2php;
  18. /**
  19. * Handles CLI commands.
  20. */
  21. class CliController {
  22. protected $_converter;
  23. protected $_htmlPattern = "*.html";
  24. protected $_recursive = false;
  25. protected $_phpFilePrefix = '';
  26. protected $_phpFilePostfix = 'Test';
  27. protected $_destFolder = '';
  28. protected $_sourceBaseDir = '';
  29. protected $_useHashFilePostfix = false;
  30. protected $_tplFile = '';
  31. public function __construct() {
  32. require_once 'Converter.php';
  33. $this->_converter = new Converter;
  34. }
  35. protected function _printTitle() {
  36. print "Selenium2php converts Selenium HTML tests into PHPUnit test case code.";
  37. print "\n";
  38. print "\n";
  39. }
  40. protected function _printHelp() {
  41. print "Usage: selenium2php [switches] Test.html [Test.php]";
  42. print "\n";
  43. print " selenium2php [switches] <directory>";
  44. print "\n";
  45. print "\n";
  46. print " --dest=<path> Destination folder.\n";
  47. print " --selenium2 Use Selenium2 tests format.\n";
  48. print " --php-prefix=<string> Add prefix to php filenames.\n";
  49. print " --php-postfix=<string> Add postfix to php filenames.\n";
  50. print " --browser=<browsers string> Set browser for tests.\n";
  51. print " --browser-url=<url> Set URL for tests.\n";
  52. print " --remote-host=<host> Set Selenium server address for tests.\n";
  53. print " --remote-port=<port> Set Selenium server port for tests.\n";
  54. print " -r|--recursive Use subdirectories for converting.\n";
  55. print " --class-prefix=<prefix> Set TestCase class prefix.\n";
  56. print " --use-hash-postfix Add hash part to output filename.\n";
  57. print " --files-pattern=<pattern> Glob pattern for input test files (*.html).\n";
  58. print " --output-tpl=<file> Template for result file. See TestExampleTpl.\n";
  59. print " --custom-param1=<value> Assign value to \$customParam1 in template.\n";
  60. print " --custom-param2=<value> Assign value to \$customParam2 in template.\n";
  61. }
  62. protected function _applyOptionsAndFlags($options, $flags){
  63. if (is_array($options)){
  64. foreach ($options as $opt){
  65. if (is_string($opt)){
  66. switch ($opt){
  67. case 'recursive':
  68. $this->_recursive = true;
  69. break;
  70. case 'use-hash-postfix':
  71. $this->_useHashFilePostfix = true;
  72. break;
  73. case 'selenium2':
  74. $this->_converter->useSelenium2();
  75. break;
  76. default:
  77. print "Unknown option \"$opt\".\n";
  78. exit(1);
  79. }
  80. } else if (is_array($opt)){
  81. switch ($opt[0]){
  82. case 'php-prefix':
  83. $this->_phpFilePrefix = $opt[1];
  84. break;
  85. case 'php-postfix':
  86. $this->_phpFilePostfix = $opt[1];
  87. break;
  88. case 'browser':
  89. $this->_converter->setBrowser($opt[1]);
  90. break;
  91. case 'browser-url':
  92. $this->_converter->setTestUrl($opt[1]);
  93. break;
  94. case 'remote-host':
  95. $this->_converter->setRemoteHost($opt[1]);
  96. break;
  97. case 'remote-port':
  98. $this->_converter->setRemotePort($opt[1]);
  99. break;
  100. case 'dest':
  101. $this->_destFolder = $opt[1];
  102. break;
  103. case 'class-prefix':
  104. $this->_converter->setTplClassPrefix($opt[1]);
  105. break;
  106. case 'use-hash-postfix':
  107. $this->_useHashFilePostfix = true;
  108. break;
  109. case 'files-pattern':
  110. $this->_htmlPattern = $opt[1];
  111. break;
  112. case 'output-tpl':
  113. $this->_tplFile = $opt[1];
  114. break;
  115. case 'custom-param1':
  116. $this->_converter->setTplCustomParam1($opt[1]);
  117. break;
  118. case 'custom-param2':
  119. $this->_converter->setTplCustomParam2($opt[1]);
  120. break;
  121. default:
  122. print "Unknown option \"{$opt[0]}\".\n";
  123. exit(1);
  124. }
  125. }
  126. }
  127. }
  128. if (is_array($flags)){
  129. foreach ($flags as $flag){
  130. switch($flag){
  131. case 'r':
  132. $this->_recursive = true;
  133. break;
  134. default:
  135. print "Unknown flag \"$flag\".\n";
  136. exit(1);
  137. }
  138. }
  139. }
  140. }
  141. public function run($arguments, $options, $flags) {
  142. $this->_printTitle();
  143. $this->_applyOptionsAndFlags($options, $flags);
  144. if (empty($arguments)) {
  145. $this->_printHelp();
  146. } else if (!empty($arguments)) {
  147. $first = array_shift($arguments);
  148. $second = array_shift($arguments);
  149. if ($first && is_string($first)) {
  150. if (is_file($first)) {
  151. $htmlFileName = $first;
  152. if (is_readable($htmlFileName)) {
  153. if ($second && is_string($second)) {
  154. $phpFileName = $second;
  155. } else {
  156. $phpFileName = '';
  157. }
  158. $this->_sourceBaseDir = rtrim(dirname($htmlFileName), "\\/")."/";
  159. $this->convertFile($htmlFileName, $phpFileName);
  160. print "OK.\n";
  161. exit(0);
  162. } else {
  163. print "Cannot open file \"$htmlFileName\".\n";
  164. exit(1);
  165. }
  166. } else if (is_dir($first)) {
  167. $dir = rtrim($first, "\\/")."/";
  168. $this->_sourceBaseDir = $dir;
  169. $res = $this->convertFilesInDirectory($dir);
  170. if ($res){
  171. print "OK.\n";
  172. exit(0);
  173. } else {
  174. exit(1);
  175. }
  176. } else {
  177. print "\"$first\" is not existing file or directory.\n";
  178. exit(1);
  179. }
  180. }
  181. }
  182. }
  183. protected function convertFilesInDirectory($dir){
  184. if ($this->_recursive){
  185. $files = $this->globRecursive($dir . $this->_htmlPattern, GLOB_NOSORT);
  186. } else {
  187. $files = glob($dir . $this->_htmlPattern, GLOB_NOSORT);
  188. }
  189. if (count($files)){
  190. foreach ($files as $htmlFile){
  191. $this->convertFile($htmlFile);
  192. }
  193. return true;
  194. } else {
  195. print "Files \"{$this->_htmlPattern}\" not found in \"$dir\".";
  196. }
  197. return false;
  198. }
  199. protected function _makeOutputFilename($htmlFileName, $htmlContent) {
  200. $fileName = $this->_makeTestName($htmlFileName);
  201. if ($this->_destFolder) {
  202. $filePath = rtrim($this->_destFolder, "\\/") . "/";
  203. if (!realpath($filePath)) {
  204. //path is not absolute
  205. $filePath = $this->_sourceBaseDir . $filePath;
  206. if (!realpath($filePath)) {
  207. print "Directory \"$filePath\" not found.\n";
  208. exit(1);
  209. }
  210. }
  211. } else {
  212. $filePath = dirname($htmlFileName) . "/";
  213. }
  214. if ($this->_useHashFilePostfix) {
  215. $hashPostfix = '_' . substr(md5($htmlContent), 0, 8) . '_';
  216. } else {
  217. $hashPostfix = '';
  218. }
  219. $phpFileName = $filePath . $this->_phpFilePrefix
  220. . preg_replace("/\..+$/", '', $fileName)
  221. . $hashPostfix
  222. . $this->_phpFilePostfix . ".php";
  223. return $phpFileName;
  224. }
  225. /**
  226. * Makes output test name considering path.
  227. *
  228. * If destination folder is not defined
  229. * returns base name of html file without extension.
  230. * Example:
  231. * auth/login/simple.html -> simple
  232. *
  233. * If destination folder is defined
  234. * returns base name of html file prefixed with
  235. * name of folder accordingly to destination folder.
  236. * Example:
  237. * auth/login/simple.html -> Auth_login_simple
  238. *
  239. * @param string $htmlFileName input file name
  240. * @return string output test name
  241. */
  242. protected function _makeTestName($htmlFileName){
  243. /* get from file if this is empty */
  244. $testName = basename($htmlFileName);
  245. if ($this->_destFolder) {
  246. $absPath = str_replace('\\', '_', $htmlFileName);
  247. $absPath = str_replace('/', '_', $absPath);
  248. $destPath = str_replace('\\', '_', $this->_sourceBaseDir);
  249. $destPath = str_replace('/', '_', $destPath);
  250. $testName= str_replace($destPath, '', $absPath);
  251. }
  252. $testName = ucfirst(preg_replace("/\..+$/", '', $testName));
  253. return $testName;
  254. }
  255. public function convertFile($htmlFileName, $phpFileName = '') {
  256. $htmlContent = file_get_contents($htmlFileName);
  257. if ($htmlContent) {
  258. if (!$phpFileName) {
  259. $phpFileName = $this->_makeOutputFilename($htmlFileName, $htmlContent);
  260. }
  261. $result = $this->_converter->convert($htmlContent, $this->_makeTestName($htmlFileName), $this->_tplFile);
  262. file_put_contents($phpFileName, $result);
  263. print $phpFileName."\n";
  264. }
  265. }
  266. protected function globRecursive($pattern, $flags) {
  267. $files = glob($pattern, $flags);
  268. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  269. $files = array_merge($files, $this->globRecursive($dir . '/' . basename($pattern), $flags));
  270. }
  271. return $files;
  272. }
  273. }