CakeTestLoader.php 3.5 KB

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