PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/TestSuite/CakeTestLoader.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 122 lines | 56 code | 10 blank | 56 comment | 6 complexity | d77ea3ee64acda06520a40349884f9c8 MD5 | raw file
  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. /**
  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['app'])) {
  61. $result = APP_TEST_CASES;
  62. } else if (!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. } else {
  69. $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
  70. }
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Get the list of files for the test listing.
  76. *
  77. * @return void
  78. */
  79. public static function generateTestList($params) {
  80. $directory = self::_basePath($params);
  81. $fileList = self::_getRecursiveFileList($directory);
  82. $testCases = array();
  83. foreach ($fileList as $testCaseFile) {
  84. $case = str_replace($directory . DS, '', $testCaseFile);
  85. $case = str_replace('Test.php', '', $case);
  86. $testCases[$testCaseFile] = $case;
  87. }
  88. sort($testCases);
  89. return $testCases;
  90. }
  91. /**
  92. * Gets a recursive list of files from a given directory and matches then against
  93. * a given fileTestFunction, like isTestCaseFile()
  94. *
  95. * @param string $directory The directory to scan for files.
  96. * @param mixed $fileTestFunction
  97. */
  98. protected static function _getRecursiveFileList($directory = '.') {
  99. $fileList = array();
  100. if (!is_dir($directory)) {
  101. return $fileList;
  102. }
  103. $files = new RegexIterator(
  104. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
  105. '/.*Test.php$/'
  106. );
  107. foreach ($files as $file) {
  108. $fileList[] = $file->getPathname();
  109. }
  110. return $fileList;
  111. }
  112. }