/framework/Test/lib/Horde/Test/AllTests.php

https://github.com/ewandor/horde · PHP · 151 lines · 75 code · 13 blank · 63 comment · 12 complexity · 0fd18d095db4db7d1c939848ff305c72 MD5 · raw file

  1. <?php
  2. /**
  3. * Horde base test suite
  4. *
  5. * PHP version 5
  6. *
  7. * @category Horde
  8. * @package Test
  9. * @author Jan Schneider <jan@horde.org>
  10. * @author Gunnar Wrobel <wrobel@pardus.de>
  11. * @license http://www.horde.org/licenses/lgpl21 LGPL
  12. * @link http://www.horde.org/components/Horde_Test
  13. */
  14. if (!defined('PHPUnit_MAIN_METHOD')) {
  15. define('PHPUnit_MAIN_METHOD', 'Horde_Test_AllTests::main');
  16. }
  17. require_once 'PHPUnit/Autoload.php';
  18. /**
  19. * Horde base test suite
  20. *
  21. * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
  22. *
  23. * See the enclosed file COPYING for license information (LGPL). If you
  24. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  25. *
  26. * @category Horde
  27. * @package Test
  28. * @author Jan Schneider <jan@horde.org>
  29. * @author Gunnar Wrobel <wrobel@pardus.de>
  30. * @license http://www.horde.org/licenses/lgpl21 LGPL
  31. * @link http://www.horde.org/components/Horde_Test
  32. */
  33. class Horde_Test_AllTests
  34. {
  35. /** @todo: Use protected properties and LSB with PHP 5.3. */
  36. private static $_file = __FILE__;
  37. private static $_package = 'Horde_Test';
  38. /**
  39. * Main entry point for running the suite.
  40. */
  41. public static function main($package = null, $file = null)
  42. {
  43. if ($package) {
  44. self::$_package = $package;
  45. }
  46. if ($file) {
  47. self::$_file = $file;
  48. }
  49. PHPUnit_TextUI_TestRunner::run(self::suite());
  50. }
  51. /**
  52. * Initialize the test suite class.
  53. *
  54. * @param string $package The name of the package tested by this suite.
  55. * @param string $file The path of the AllTests class.
  56. *
  57. * @return NULL
  58. */
  59. public static function init($package, $file)
  60. {
  61. self::$_package = $package;
  62. self::$_file = $file;
  63. }
  64. /**
  65. * Collect the unit tests of this directory into a new suite.
  66. *
  67. * @return PHPUnit_Framework_TestSuite The test suite.
  68. */
  69. public static function suite()
  70. {
  71. self::setup();
  72. $suite = new PHPUnit_Framework_TestSuite('Horde Framework - ' . self::$_package);
  73. $basedir = dirname(self::$_file);
  74. $baseregexp = preg_quote($basedir . DIRECTORY_SEPARATOR, '/');
  75. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basedir)) as $file) {
  76. if ($file->isFile() && preg_match('/Test.php$/', $file->getFilename())) {
  77. $pathname = $file->getPathname();
  78. if (include $pathname) {
  79. $class = str_replace(DIRECTORY_SEPARATOR, '_',
  80. preg_replace("/^$baseregexp(.*)\.php/", '\\1', $pathname));
  81. try {
  82. $suite->addTestSuite(self::$_package . '_' . $class);
  83. } catch (InvalidArgumentException $e) {
  84. throw new Horde_Test_Exception(
  85. sprintf(
  86. 'Failed adding test suite "%s" from file "%s": %s',
  87. self::$_package . '_' . $class,
  88. $pathname,
  89. $e->getMessage()
  90. )
  91. );
  92. }
  93. }
  94. }
  95. }
  96. return $suite;
  97. }
  98. /**
  99. * Basic test suite setup. This includes error checking and autoloading.
  100. *
  101. * In the default situation this will set the error reporting to E_ALL |
  102. * E_STRICT and pull in Horde/Test/Autoload.php as autoloading
  103. * definition. If there is an Autoload.php alongside the AllTests.php
  104. * represented by self::$_file, then only this file will be used.
  105. *
  106. * In addition the setup() call will attempt to detect the "lib" directory
  107. * of the component currently under test and add it to the
  108. * include_path. This ensures that the component code from the checkout is
  109. * preferred over whatever else might be available in the default
  110. * include_path.
  111. *
  112. * @return NULL
  113. */
  114. public static function setup()
  115. {
  116. // Detect component root and add "lib" to the include path.
  117. for ($dirname = self::$_file, $i = 0;
  118. $dirname != '/', $i < 5;
  119. $dirname = dirname($dirname), $i++) {
  120. if (basename($dirname) == 'test' &&
  121. file_exists(dirname($dirname) . '/lib')) {
  122. set_include_path(
  123. dirname($dirname) . '/lib' . PATH_SEPARATOR . get_include_path()
  124. );
  125. break;
  126. }
  127. }
  128. $autoload = dirname(self::$_file) . '/Autoload.php';
  129. if (!file_exists($autoload)) {
  130. // Catch strict standards
  131. error_reporting(E_ALL | E_STRICT);
  132. // Set up autoload
  133. require_once 'Horde/Test/Autoload.php';
  134. } else {
  135. require_once $autoload;
  136. }
  137. }
  138. }