PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/.dev/tests/AllTests.php

https://github.com/istran/core
PHP | 348 lines | 228 code | 61 blank | 59 comment | 58 complexity | a3437c459ed6f521e944cc4d26ad4fa9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package Tests
  18. * @subpackage Core
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @version GIT: $Id: effe63e7750d58e6571f276df51ce51a7d85d09f $
  23. * @link http://www.litecommerce.com/
  24. * @see ____file_see____
  25. * @since 3.0.0
  26. */
  27. if (0 > version_compare(phpversion(), '5.3.0')) {
  28. echo ('PHP version must be 5.3.0 or later' . PHP_EOL);
  29. die(1);
  30. }
  31. if (false === defined('PHPUnit_MAIN_METHOD')) {
  32. define('PHPUnit_MAIN_METHOD', 'XLite_Tests_AllTests::main');
  33. }
  34. ini_set('memory_limit', '256M');
  35. // PHPUnit classes
  36. define('PATH_TESTS', realpath(__DIR__));
  37. define('PATH_ROOT', realpath(__DIR__ . '/../..'));
  38. // Include local code
  39. if (file_exists(PATH_TESTS . '/local.php')) {
  40. require_once PATH_TESTS . '/local.php';
  41. }
  42. if (defined('DRUPAL_SITE_PATH') && !defined('LOCAL_TESTS')) {
  43. define('PATH_SRC', realpath(DRUPAL_SITE_PATH . '/src/modules/lc_connector/litecommerce'));
  44. } else {
  45. define('PATH_SRC', realpath(PATH_ROOT . '/src'));
  46. }
  47. set_include_path(
  48. get_include_path()
  49. . PATH_SEPARATOR . '/usr/local/share/pear/'
  50. . PATH_SEPARATOR . PATH_SRC . '/classes'
  51. . PATH_SEPARATOR . PATH_SRC . '/var/run/classes'
  52. );
  53. require_once 'PHPUnit/Framework/TestSuite.php';
  54. require_once 'PHPUnit/TextUI/TestRunner.php';
  55. require_once PATH_TESTS . '/PHPUnit/TestSuite.php';
  56. require_once PATH_TESTS . '/PHPUnit/TestCase.php';
  57. require_once PATH_TESTS . '/PHPUnit/MetricWriter.php';
  58. require_once PATH_TESTS . '/PHPUnit/SeleniumTestCase.php';
  59. // Start X-Lite core
  60. define('LC_DO_NOT_REBUILD_CACHE', 1);
  61. require_once PATH_SRC . '/top.inc.php';
  62. if (!defined('SELENIUM_SOURCE_URL')) {
  63. $arr = explode('/', realpath(__DIR__ . '/../..'));
  64. array_shift($arr);
  65. array_shift($arr);
  66. array_shift($arr);
  67. array_shift($arr);
  68. define('SELENIUM_SOURCE_URL', 'http://xcart2-530.crtdev.local/~' . posix_getlogin() . '/' . implode('/', $arr));
  69. unset($arr);
  70. }
  71. if (!defined('SELENIUM_SERVER')) {
  72. define('SELENIUM_SERVER', 'cormorant.crtdev.local');
  73. }
  74. if (isset($_SERVER['argv']) && preg_match('/--log-xml\s+(\S+)\s/s', implode(' ', $_SERVER['argv']), $match)) {
  75. XLite_Tests_MetricWriter::init($match[1] . '.speed');
  76. unset($match);
  77. }
  78. if (!defined('INCLUDE_ONLY_TESTS') || !preg_match('/DEPLOY_/', constant('INCLUDE_ONLY_TESTS'))) {
  79. PHPUnit_Util_Filter::addDirectoryToFilter(PATH_ROOT . '/.dev');
  80. PHPUnit_Util_Filter::addDirectoryToFilter(PATH_SRC . '/etc');
  81. PHPUnit_Util_Filter::addDirectoryToWhitelist(PATH_SRC . '/var/run/classes');
  82. PHPUnit_Util_Filter::addDirectoryToFilter(PATH_SRC . '/var/run/classes/XLite/Model/Proxy');
  83. }
  84. foreach (glob(LC_ROOT_DIR . 'var/log/selenium.*.html') as $f) {
  85. @unlink($f);
  86. }
  87. /**
  88. * Class to run all the tests
  89. *
  90. * @package X-Lite_Tests
  91. * @subpackage Main
  92. * @see ____class_see____
  93. * @since 1.0.0
  94. */
  95. class XLite_Tests_AllTests
  96. {
  97. /**
  98. * Test suite main method
  99. *
  100. * @return void
  101. * @access public
  102. * @see ____func_see____
  103. * @since 1.0.0
  104. */
  105. public static function main()
  106. {
  107. PHPUnit_TextUI_TestRunner::run(self::suite());
  108. }
  109. /**
  110. * Creates the phpunit test suite
  111. *
  112. * @return XLite_Tests_TestSuite
  113. * @access public
  114. * @see ____func_see____
  115. * @since 1.0.0
  116. */
  117. public static function suite()
  118. {
  119. $suite = new XLite_Tests_TestSuite('LiteCommerce - AllTests');
  120. $deploy = null;
  121. $includes = false;
  122. $includeTests = array();
  123. $excludes = array();
  124. if (defined('INCLUDE_ONLY_TESTS')) {
  125. $includes = array_map('trim', explode(',', INCLUDE_ONLY_TESTS));
  126. if (in_array('LOCAL_TESTS', $includes)) {
  127. $k = array_search('LOCAL_TESTS', $includes);
  128. unset($includes[$k]);
  129. }
  130. if (in_array('NOWEB', $includes)) {
  131. if (!defined('SELENIUM_DISABLED')) {
  132. define('SELENIUM_DISABLED', true);
  133. }
  134. $k = array_search('NOWEB', $includes);
  135. unset($includes[$k]);
  136. }
  137. if (in_array('ONLYWEB', $includes)) {
  138. if (!defined('UNITS_DISABLED')) {
  139. define('UNITS_DISABLED', true);
  140. }
  141. $k = array_search('ONLYWEB', $includes);
  142. unset($includes[$k]);
  143. }
  144. if (in_array('DEPLOY_DRUPAL', $includes)) {
  145. $deploy = 'Drupal';
  146. } elseif (in_array('DEPLOY_STANDALONE', $includes)) {
  147. $deploy = 'Standalone';
  148. }
  149. if (!is_null($deploy)) {
  150. if (!defined('UNITS_DISABLED')) {
  151. define('UNITS_DISABLED', true);
  152. }
  153. $k = array_search('DEPLOY_' . strtoupper($deploy), $includes);
  154. if (!defined('DIR_TESTS')) {
  155. define('DIR_TESTS', 'Deploy' . DIRECTORY_SEPARATOR . $deploy);
  156. }
  157. unset($includes[$k]);
  158. }
  159. if (in_array('W3C', $includes)) {
  160. if (!defined('W3C_VALIDATION')) {
  161. define('W3C_VALIDATION', true);
  162. }
  163. $k = array_search('W3C', $includes);
  164. unset($includes[$k]);
  165. }
  166. foreach ($includes as $k => $v) {
  167. if ('-' == substr($v, 0, 1)) {
  168. $excludes[] = substr($v, 1);
  169. unset($includes[$k]);
  170. }
  171. }
  172. foreach ($includes as $k => $v) {
  173. $tmp = explode(':', $v, 2);
  174. $includes[$k] = $tmp[0];
  175. if (isset($tmp[1])) {
  176. $includeTests[$tmp[0]] = $tmp[1];
  177. }
  178. }
  179. }
  180. if (isset($deploy) && !defined('DEPLOYMENT_TEST')) {
  181. define('DEPLOYMENT_TEST', true);
  182. }
  183. // Include abstract classes
  184. $classesDir = dirname( __FILE__ );
  185. $pattern = '/^' . preg_quote($classesDir, '/') . '.*\/(?:\w*Abstract|A[A-Z][a-z]\w*)\.php$/Ss';
  186. $dirIterator = new RecursiveDirectoryIterator($classesDir . DIRECTORY_SEPARATOR);
  187. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
  188. foreach ($iterator as $filePath => $fileObject) {
  189. if (preg_match($pattern, $filePath, $matches)) {
  190. require_once $filePath;
  191. }
  192. }
  193. // Include fake classes
  194. if (!defined('DEPLOYMENT_TEST')) {
  195. $classesDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'FakeClass' . DIRECTORY_SEPARATOR;
  196. $pattern = '/^' . preg_quote($classesDir, '/') . '.+\.php$/Ss';
  197. $dirIterator = new RecursiveDirectoryIterator($classesDir . DIRECTORY_SEPARATOR);
  198. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
  199. foreach ($iterator as $filePath => $fileObject) {
  200. if (preg_match($pattern, $filePath, $matches)) {
  201. require_once $filePath;
  202. }
  203. }
  204. }
  205. // DB backup
  206. echo ('DB backup ... ');
  207. $path = dirname(__FILE__) . '/dump.sql';
  208. if (file_exists($path)) {
  209. unlink($path);
  210. }
  211. if (!isset($deploy) || !$deploy) {
  212. $config = XLite::getInstance()->getOptions('database_details');
  213. $cmd = 'mysqldump --opt -h' . $config['hostspec'];
  214. if ($config['port']) {
  215. $cmd .= ':' . $config['port'];
  216. }
  217. $cmd .= ' -u' . $config['username'] . ' -p' . $config['password'];
  218. if ($config['socket']) {
  219. $cmd .= ' -S' . $config['socket'];
  220. }
  221. exec($cmd .= ' ' . $config['database'] . ' > ' . $path);
  222. echo ('done' . PHP_EOL);
  223. } else {
  224. echo ('ignored' . PHP_EOL);
  225. }
  226. // Classes tests
  227. if (!defined('UNITS_DISABLED')) {
  228. $classesDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'Classes' . DIRECTORY_SEPARATOR;
  229. $pattern = '/^' . str_replace('/', '\/', preg_quote($classesDir)) . '(.*)\.php$/';
  230. $dirIterator = new RecursiveDirectoryIterator($classesDir);
  231. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
  232. foreach ($iterator as $filePath => $fileObject) {
  233. if (
  234. preg_match($pattern, $filePath, $matches)
  235. && !empty($matches[1])
  236. && !preg_match('/\/(\w+Abstract|A[A-Z]\w+)\.php$/Ss', $filePath)
  237. && (!$includes || in_array($matches[1], $includes))
  238. && (!$excludes || !in_array($matches[1], $excludes))
  239. ) {
  240. $class = XLite_Tests_TestCase::CLASS_PREFIX
  241. . str_replace(DIRECTORY_SEPARATOR, '_', $matches[1]);
  242. require_once $filePath;
  243. $suite->addTest(new XLite_Tests_TestSuite(new ReflectionClass($class)));
  244. if (isset($includeTests[$matches[1]])) {
  245. eval($class . '::$testsRange = array($includeTests[$matches[1]]);');
  246. }
  247. }
  248. }
  249. }
  250. // Web tests
  251. if (!defined('SELENIUM_DISABLED')) {
  252. if (!defined('DIR_TESTS')) {
  253. define('DIR_TESTS', 'Web');
  254. }
  255. $classesDir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . constant('DIR_TESTS') . DIRECTORY_SEPARATOR;
  256. $pattern = '/^' . str_replace('/', '\/', preg_quote($classesDir)) . '(.*)\.php$/';
  257. $dirIterator = new RecursiveDirectoryIterator($classesDir);
  258. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
  259. foreach ($iterator as $filePath => $fileObject) {
  260. if (
  261. preg_match($pattern, $filePath, $matches)
  262. && !empty($matches[1])
  263. && !preg_match('/\/(\w+Abstract|A[A-Z]\d+)\.php/Ss', $filePath)
  264. && (!$includes || in_array($matches[1], $includes))
  265. && (!$excludes || !in_array($matches[1], $excludes))
  266. ) {
  267. $classPrefix = !isset($deploy)
  268. ? XLite_Tests_SeleniumTestCase::CLASS_PREFIX
  269. : 'XLite_Deploy_' . $deploy . '_';
  270. $class = $classPrefix . str_replace(DIRECTORY_SEPARATOR, '_', $matches[1]);
  271. require_once $filePath;
  272. //$suite->addTest(new XLite_Tests_TestSuite(new ReflectionClass($class)));
  273. $suite->addTestSuite($class);
  274. if (isset($includeTests[$matches[1]])) {
  275. eval($class . '::$testsRange = array($includeTests[$matches[1]]);');
  276. }
  277. }
  278. }
  279. }
  280. error_reporting(E_ALL);
  281. return $suite;
  282. }
  283. }
  284. // Execute
  285. if (PHPUnit_MAIN_METHOD === 'XLite_Tests_AllTests::main') {
  286. XLite_Tests_AllTests::main();
  287. }