CakeTestLoader.php 3.2 KB

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