CakeTestLoader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * @param string $filePath
  44. * @param string $params
  45. * @return void
  46. */
  47. protected function _resolveTestFile($filePath, $params) {
  48. $basePath = $this->_basePath($params) . DS . $filePath;
  49. $ending = 'Test.php';
  50. return (strpos($basePath, $ending) === (strlen($basePath) - strlen($ending))) ? $basePath : $basePath . $ending;
  51. }
  52. /**
  53. * Generates the base path to a set of tests based on the parameters.
  54. *
  55. * @param array $params
  56. * @return string The base path.
  57. */
  58. protected static function _basePath($params) {
  59. $result = null;
  60. if (!empty($params['core'])) {
  61. $result = CORE_TEST_CASES;
  62. } elseif (!empty($params['plugin'])) {
  63. if (!CakePlugin::loaded($params['plugin'])) {
  64. try {
  65. CakePlugin::load($params['plugin']);
  66. $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
  67. } catch (MissingPluginException $e) {
  68. }
  69. } else {
  70. $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
  71. }
  72. } elseif (!empty($params['app'])) {
  73. $result = APP_TEST_CASES;
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Get the list of files for the test listing.
  79. *
  80. * @param string $params
  81. * @return array
  82. */
  83. public static function generateTestList($params) {
  84. $directory = self::_basePath($params);
  85. $fileList = self::_getRecursiveFileList($directory);
  86. $testCases = array();
  87. foreach ($fileList as $testCaseFile) {
  88. $case = str_replace($directory . DS, '', $testCaseFile);
  89. $case = str_replace('Test.php', '', $case);
  90. $testCases[$testCaseFile] = $case;
  91. }
  92. sort($testCases);
  93. return $testCases;
  94. }
  95. /**
  96. * Gets a recursive list of files from a given directory and matches then against
  97. * a given fileTestFunction, like isTestCaseFile()
  98. *
  99. * @param string $directory The directory to scan for files.
  100. * @return array
  101. */
  102. protected static function _getRecursiveFileList($directory = '.') {
  103. $fileList = array();
  104. if (!is_dir($directory)) {
  105. return $fileList;
  106. }
  107. $files = new RegexIterator(
  108. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
  109. '/.*Test.php$/'
  110. );
  111. foreach ($files as $file) {
  112. $fileList[] = $file->getPathname();
  113. }
  114. return $fileList;
  115. }
  116. }