CakeTestLoader.php 3.2 KB

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