Base for a static organization website

CakeTestLoader.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * TestLoader for CakePHP Test suite.
  4. *
  5. * Turns partial paths used on the testsuite console and web UI into full file paths.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. * @package Cake.TestSuite
  19. */
  20. /**
  21. * TestLoader for CakePHP Test suite.
  22. *
  23. * Turns partial paths used on the testsuite console and web UI into full file paths.
  24. *
  25. * @package Cake.TestSuite
  26. */
  27. class CakeTestLoader extends PHPUnit_Runner_StandardTestSuiteLoader {
  28. /**
  29. * Load a file and find the first test case / suite in that file.
  30. *
  31. * @param string $filePath The file path to load
  32. * @param string $params Additional parameters
  33. * @return ReflectionClass
  34. */
  35. public function load($filePath, $params = '') {
  36. $file = $this->_resolveTestFile($filePath, $params);
  37. return parent::load('', $file);
  38. }
  39. /**
  40. * Convert path fragments used by CakePHP's test runner to absolute paths that can be fed to PHPUnit.
  41. *
  42. * @param string $filePath The file path to load.
  43. * @param string $params Additional parameters.
  44. * @return string Converted path fragments.
  45. */
  46. protected function _resolveTestFile($filePath, $params) {
  47. $basePath = $this->_basePath($params) . DS . $filePath;
  48. $ending = 'Test.php';
  49. return (strpos($basePath, $ending) === (strlen($basePath) - strlen($ending))) ? $basePath : $basePath . $ending;
  50. }
  51. /**
  52. * Generates the base path to a set of tests based on the parameters.
  53. *
  54. * @param array $params The path parameters.
  55. * @return string The base path.
  56. */
  57. protected static function _basePath($params) {
  58. $result = null;
  59. if (!empty($params['core'])) {
  60. $result = CORE_TEST_CASES;
  61. } elseif (!empty($params['plugin'])) {
  62. if (!CakePlugin::loaded($params['plugin'])) {
  63. try {
  64. CakePlugin::load($params['plugin']);
  65. $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
  66. } catch (MissingPluginException $e) {
  67. }
  68. } else {
  69. $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
  70. }
  71. } elseif (!empty($params['app'])) {
  72. $result = APP_TEST_CASES;
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Get the list of files for the test listing.
  78. *
  79. * @param string $params Path parameters
  80. * @return array
  81. */
  82. public static function generateTestList($params) {
  83. $directory = static::_basePath($params);
  84. $fileList = static::_getRecursiveFileList($directory);
  85. $testCases = array();
  86. foreach ($fileList as $testCaseFile) {
  87. $case = str_replace($directory . DS, '', $testCaseFile);
  88. $case = str_replace('Test.php', '', $case);
  89. $testCases[$testCaseFile] = $case;
  90. }
  91. sort($testCases);
  92. return $testCases;
  93. }
  94. /**
  95. * Gets a recursive list of files from a given directory and matches then against
  96. * a given fileTestFunction, like isTestCaseFile()
  97. *
  98. * @param string $directory The directory to scan for files.
  99. * @return array
  100. */
  101. protected static function _getRecursiveFileList($directory = '.') {
  102. $fileList = array();
  103. if (!is_dir($directory)) {
  104. return $fileList;
  105. }
  106. $files = new RegexIterator(
  107. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
  108. '/.*Test.php$/'
  109. );
  110. foreach ($files as $file) {
  111. $fileList[] = $file->getPathname();
  112. }
  113. return $fileList;
  114. }
  115. }