CakeTestLoader.php 3.4 KB

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