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

/dev/tests/js/run_js_tests.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 241 lines | 160 code | 27 blank | 54 comment | 19 complexity | 759d5914d261bf850bc7057db244802c MD5 | raw file
  1. <?php
  2. /**
  3. * This script executes all Magento JavaScript unit tests.
  4. *
  5. * Magento
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * This source file is subject to the Open Software License (OSL 3.0)
  10. * that is bundled with this package in the file LICENSE.txt.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://opensource.org/licenses/osl-3.0.php
  13. * If you did not receive a copy of the license and are unable to
  14. * obtain it through the world-wide-web, please send an email
  15. * to license@magentocommerce.com so we can send you a copy immediately.
  16. *
  17. * DISCLAIMER
  18. *
  19. * Do not edit or add to this file if you wish to upgrade Magento to newer
  20. * versions in the future. If you wish to customize Magento for your
  21. * needs please refer to http://www.magentocommerce.com for more information.
  22. *
  23. * @category tests
  24. * @package js
  25. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. */
  28. define('RELATIVE_APP_ROOT', '../../..');
  29. require __DIR__ . '/../../../app/autoload.php';
  30. Magento_Autoload_IncludePath::addIncludePath(realpath(RELATIVE_APP_ROOT . '/lib'));
  31. $userConfig = normalize('jsTestDriver.php');
  32. $defaultConfig = normalize('jsTestDriver.php.dist');
  33. $configFile = file_exists($userConfig) ? $userConfig : $defaultConfig;
  34. $config = require($configFile);
  35. if (isset($config['JsTestDriver'])) {
  36. $jsTestDriver = $config['JsTestDriver'];
  37. } else {
  38. echo "Value for the 'JsTestDriver' configuration parameter is not specified." . PHP_EOL;
  39. showUsage();
  40. }
  41. if (!file_exists($jsTestDriver)) {
  42. reportError('JsTestDriver jar file does not exist: ' . $jsTestDriver);
  43. }
  44. if (isset($config['Browser'])) {
  45. $browser = $config['Browser'];
  46. } else {
  47. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  48. $browser = 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe';
  49. } else {
  50. $browser = exec('which firefox');
  51. }
  52. }
  53. if (!file_exists($browser)) {
  54. reportError('Browser executable not found: ' . $browser);
  55. }
  56. $server = isset($config['server']) ? $config['server'] : "http://localhost:9876";
  57. $port = substr(strrchr($server, ':'), 1);
  58. $proxies = isset($config['proxy']) ? $config['proxy'] : array();
  59. $testFilesPath = isset($config['test']) ? $config['test'] : array();
  60. $testFiles = listFiles($testFilesPath);
  61. $loadFilesPath = isset($config['load']) ? $config['load'] : array();
  62. $loadFiles = listFiles($loadFilesPath);
  63. if (empty($loadFiles)) {
  64. reportError('Could not find any files to load.');
  65. }
  66. $serveFilesPath = isset($config['serve']) ? $config['serve'] : array();
  67. $serveFiles = listFiles($serveFilesPath);
  68. $sortedFiles = array();
  69. $fileOrder = normalize('jsTestDriverOrder.php');
  70. if (file_exists($fileOrder)) {
  71. $loadOrder = require($fileOrder);
  72. foreach ($loadOrder as $file) {
  73. $sortedFiles[] = RELATIVE_APP_ROOT . $file;
  74. }
  75. foreach ($loadFiles as $loadFile) {
  76. $found = false;
  77. $normalizedLoadFile = normalize($loadFile);
  78. foreach ($loadOrder as $orderFile) {
  79. if (strcmp(normalize(RELATIVE_APP_ROOT . $orderFile), $normalizedLoadFile) == 0) {
  80. $found = true;
  81. break;
  82. }
  83. }
  84. if (!$found) {
  85. array_push($sortedFiles, $loadFile);
  86. }
  87. }
  88. }
  89. $jsTestDriverConf = __DIR__ . '/jsTestDriver.conf';
  90. $fh = fopen($jsTestDriverConf, 'w');
  91. fwrite($fh, "server: $server" . PHP_EOL);
  92. if (count($proxies) > 0) {
  93. fwrite($fh, "proxy:" . PHP_EOL);
  94. foreach ($proxies as $proxy) {
  95. $proxyServer = sprintf($proxy['server'], $server, normalize(RELATIVE_APP_ROOT));
  96. fwrite($fh, ' - {matcher: "' . $proxy['matcher'] . '", server: "' . $proxyServer . '"}' . PHP_EOL);
  97. }
  98. }
  99. fwrite($fh, "load:" . PHP_EOL);
  100. foreach ($sortedFiles as $file) {
  101. if (!in_array($file, $serveFiles)) {
  102. fwrite($fh, " - " . $file . PHP_EOL);
  103. }
  104. }
  105. fwrite($fh, "test:" . PHP_EOL);
  106. foreach ($testFiles as $file) {
  107. fwrite($fh, " - " . $file . PHP_EOL);
  108. }
  109. if (count($serveFiles) > 0) {
  110. fwrite($fh, "serve:" . PHP_EOL);
  111. foreach ($serveFiles as $file) {
  112. fwrite($fh, " - " . $file . PHP_EOL);
  113. }
  114. }
  115. fclose($fh);
  116. $testOutput = __DIR__ . '/test-output';
  117. Varien_Io_File::rmdirRecursive($testOutput);
  118. mkdir($testOutput);
  119. $command
  120. = 'java -jar "' . $jsTestDriver . '" --config "' . $jsTestDriverConf . '" --port ' . $port .
  121. ' --browser "' . $browser . '" --tests all --testOutput "' . $testOutput . '"';
  122. echo $command . PHP_EOL;
  123. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  124. system($command);
  125. } else {
  126. $shellCommand
  127. = '#!/bin/bash
  128. LSOF=`/usr/sbin/lsof -i :' . $port . ' -t`
  129. if [ "$LSOF" != "" ];
  130. then
  131. kill -9 $LSOF
  132. fi
  133. pkill Xvfb
  134. XVFB=`which Xvfb`
  135. if [ "$?" -eq 1 ];
  136. then
  137. echo "Xvfb not found."
  138. exit 1
  139. fi
  140. $XVFB :99 -nolisten inet6 -screen 0 1024x768x24 -ac &
  141. PID_XVFB="$!" # take the process ID
  142. export DISPLAY=:99.0 # set display to use that of the Xvfb
  143. # run the tests
  144. ' . $command . '
  145. kill $PID_XVFB # shut down Xvfb (firefox will shut down cleanly by JsTestDriver)
  146. echo "Done."';
  147. system($shellCommand);
  148. }
  149. /**
  150. * Show a message that displays how to use (invoke) this PHP script and exit.
  151. */
  152. function showUsage()
  153. {
  154. reportError('Usage: php run_js_tests.php');
  155. }
  156. /**
  157. * Reports an error given an error message and exits, effectively halting the PHP script's execution.
  158. *
  159. * @param string $message - Error message to be displayed to the user.
  160. *
  161. * @SuppressWarnings(PHPMD.ExitExpression)
  162. */
  163. function reportError($message)
  164. {
  165. echo $message . PHP_EOL;
  166. exit(1);
  167. }
  168. /**
  169. * Takes a file or directory path in any form and normalizes it to fully absolute canonical form
  170. * relative to this PHP script's location.
  171. *
  172. * @param string $filePath - File or directory path to be fully normalized to canonical form.
  173. *
  174. * @return string - The fully resolved path converted to absolute form.
  175. */
  176. function normalize($filePath)
  177. {
  178. return str_replace('\\', '/', realpath(__DIR__ . '/' . $filePath));
  179. }
  180. /**
  181. * Accepts an array of directories and generates a list of Javascript files (.js) in those directories and
  182. * all subdirectories recursively.
  183. *
  184. * @param array $dirs - An array of directories as specified in the configuration file (i.e. $configFile).
  185. *
  186. * @return array - An array of directory paths to all Javascript files found by recursively searching the
  187. * specified array of directories.
  188. */
  189. function listFiles($dirs)
  190. {
  191. $baseDir = normalize(RELATIVE_APP_ROOT);
  192. $result = array();
  193. foreach ($dirs as $dir) {
  194. $path = $baseDir . $dir;
  195. if (is_file($path)) {
  196. $path = substr_replace($path, RELATIVE_APP_ROOT, 0, strlen($baseDir));
  197. array_push($result, $path);
  198. } else {
  199. $paths = glob($path . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
  200. $paths = substr_replace($paths, '', 0, strlen($baseDir));
  201. $result = array_merge($result, listFiles($paths));
  202. $files = glob($path . '/*.js', GLOB_NOSORT);
  203. $files = substr_replace($files, RELATIVE_APP_ROOT, 0, strlen($baseDir));
  204. $result = array_merge($result, $files);
  205. }
  206. }
  207. return $result;
  208. }