PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/static/testsuite/Js/LiveCodeTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 206 lines | 118 code | 12 blank | 76 comment | 13 complexity | e109c96958211e6440d15e2ec6b9411d MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category tests
  22. * @package static
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * JSHint static code analysis tests for javascript files
  28. */
  29. class Js_LiveCodeTest extends PHPUnit_Framework_TestCase
  30. {
  31. /**
  32. * @var string
  33. */
  34. protected static $_reportFile = '';
  35. /**
  36. * @var array
  37. */
  38. protected static $_whiteListJsFiles = array();
  39. /**
  40. * @var array
  41. */
  42. protected static $_blackListJsFiles = array();
  43. /**
  44. * @static Return all files under a path
  45. * @param string $path
  46. * @return array
  47. */
  48. protected static function _scanJsFile($path)
  49. {
  50. if (is_file($path)) {
  51. return array($path);
  52. }
  53. $path = $path == '' ? dirname(__FILE__) : $path;
  54. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
  55. $regexIterator = new RegexIterator($iterator, '/\\.js$/');
  56. $filePaths = array();
  57. foreach ($regexIterator as $filePath) {
  58. $filePaths[] = $filePath->getPathname();
  59. }
  60. return $filePaths;
  61. }
  62. /**
  63. * @static Setup report file, black list and white list
  64. *
  65. */
  66. public static function setUpBeforeClass()
  67. {
  68. $reportDir = Utility_Files::init()->getPathToSource() . '/dev/tests/static/report';
  69. if (!is_dir($reportDir)) {
  70. mkdir($reportDir, 0777);
  71. }
  72. self::$_reportFile = $reportDir . '/js_report.txt';
  73. @unlink(self::$_reportFile);
  74. $whiteList = self::_readLists(__DIR__ . '/_files/whitelist/*.txt');
  75. $blackList = self::_readLists(__DIR__ . '/_files/blacklist/*.txt');
  76. foreach ($blackList as $listFiles) {
  77. self::$_blackListJsFiles = array_merge(self::$_blackListJsFiles, self::_scanJsFile($listFiles));
  78. }
  79. foreach ($whiteList as $listFiles) {
  80. self::$_whiteListJsFiles = array_merge(self::$_whiteListJsFiles, self::_scanJsFile($listFiles));
  81. }
  82. $blackListJsFiles = self::$_blackListJsFiles;
  83. $filter = function($value) use($blackListJsFiles)
  84. {
  85. return !in_array($value, $blackListJsFiles);
  86. };
  87. self::$_whiteListJsFiles = array_filter(self::$_whiteListJsFiles, $filter);
  88. }
  89. /**
  90. * @param $filename
  91. *
  92. * @throws Exception
  93. *
  94. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  95. */
  96. protected function _verifyTestRunnable($filename)
  97. {
  98. $command = 'which rhino';
  99. if ($this->_isOsWin()) {
  100. $command = 'cscript';
  101. }
  102. exec($command, $output, $retVal);
  103. if ($retVal != 0) {
  104. throw new Exception($command . ' does not exist.');
  105. }
  106. if (!file_exists($filename)) {
  107. throw new Exception($filename . ' does not exist.');
  108. }
  109. }
  110. /**
  111. * @dataProvider codeJsHintDataProvider
  112. */
  113. public function testCodeJsHint($filename)
  114. {
  115. try{
  116. $this->_verifyTestRunnable($filename);
  117. } catch (Exception $e) {
  118. $this->markTestSkipped($e->getMessage());
  119. }
  120. $result = $this->_executeJsHint($filename);
  121. if (!$result) {
  122. $this->fail("Failed JSHint.");
  123. }
  124. }
  125. /**
  126. * Build data provider array with command, js file name, and option
  127. * @return array
  128. */
  129. public function codeJsHintDataProvider()
  130. {
  131. self::setUpBeforeClass();
  132. $map = function($value)
  133. {
  134. return array($value);
  135. };
  136. return array_map($map, self::$_whiteListJsFiles);
  137. }
  138. /**
  139. * Returns cscript for windows and rhino for linux
  140. * @return string
  141. */
  142. protected function _getCommand()
  143. {
  144. if ($this->_isOsWin()) {
  145. return 'cscript ' . TESTS_JSHINT_PATH;
  146. } else {
  147. return 'rhino ' . TESTS_JSHINT_PATH;
  148. }
  149. }
  150. protected function _isOsWin()
  151. {
  152. return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  153. }
  154. /**
  155. * Run jsHint against js file; if failed output error to report file
  156. * @param $filename - js file name with full path
  157. * @return bool
  158. */
  159. protected function _executeJsHint($filename)
  160. {
  161. $command = $this->_getCommand() . ' "' . $filename . '" ' . TESTS_JSHINT_OPTIONS;
  162. exec($command, $output, $retVal);
  163. if ($retVal == 0) {
  164. return true;
  165. }
  166. if ($this->_isOsWin()) {
  167. $output = array_slice($output, 2);
  168. }
  169. $output[] = ''; //empty line to separate each file output
  170. file_put_contents(self::$_reportFile, implode(PHP_EOL, $output), FILE_APPEND);
  171. return false;
  172. }
  173. /**
  174. * Read all text files by specified glob pattern and combine them into an array of valid files/directories
  175. *
  176. * The Magento root path is prepended to all (non-empty) entries
  177. *
  178. * @param string $globPattern
  179. * @return array
  180. */
  181. protected static function _readLists($globPattern)
  182. {
  183. $result = array();
  184. foreach (glob($globPattern) as $list) {
  185. $result = array_merge($result, file($list));
  186. }
  187. $map = function($value)
  188. {
  189. return trim($value) ? Utility_Files::init()->getPathToSource() . DIRECTORY_SEPARATOR .
  190. str_replace('/', DIRECTORY_SEPARATOR, trim($value)) : '';
  191. };
  192. return array_filter(array_map($map, $result), 'file_exists');
  193. }
  194. }