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: /**
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: /**
32: * Load a file and find the first test case / suite in that file.
33: *
34: * @param string $filePath
35: * @param string $params
36: * @return ReflectionClass
37: */
38: public function load($filePath, $params = '') {
39: $file = $this->_resolveTestFile($filePath, $params);
40: return parent::load('', $file);
41: }
42:
43: /**
44: * Convert path fragments used by Cake's test runner to absolute paths that can be fed to PHPUnit.
45: *
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: /**
55: * Generates the base path to a set of tests based on the parameters.
56: *
57: * @param array $params
58: * @return string The base path.
59: */
60: protected static function _basePath($params) {
61: $result = null;
62: if (!empty($params['core'])) {
63: $result = CORE_TEST_CASES;
64: } elseif (!empty($params['app'])) {
65: $result = APP_TEST_CASES;
66: } else if (!empty($params['plugin'])) {
67: if (!CakePlugin::loaded($params['plugin'])) {
68: try {
69: CakePlugin::load($params['plugin']);
70: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
71: } catch (MissingPluginException $e) {}
72: } else {
73: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
74: }
75: }
76: return $result;
77: }
78:
79: /**
80: * Get the list of files for the test listing.
81: *
82: * @return void
83: */
84: public static function generateTestList($params) {
85: $directory = self::_basePath($params);
86: $fileList = self::_getRecursiveFileList($directory);
87:
88: $testCases = array();
89: foreach ($fileList as $testCaseFile) {
90: $case = str_replace($directory . DS, '', $testCaseFile);
91: $case = str_replace('Test.php', '', $case);
92: $testCases[$testCaseFile] = $case;
93: }
94: sort($testCases);
95: return $testCases;
96: }
97:
98: /**
99: * Gets a recursive list of files from a given directory and matches then against
100: * a given fileTestFunction, like isTestCaseFile()
101: *
102: * @param string $directory The directory to scan for files.
103: * @param mixed $fileTestFunction
104: */
105: protected static function _getRecursiveFileList($directory = '.') {
106: $fileList = array();
107: if (!is_dir($directory)) {
108: return $fileList;
109: }
110:
111: $files = new RegexIterator(
112: new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
113: '/.*Test.php$/'
114: );
115:
116: foreach ($files as $file) {
117: $fileList[] = $file->getPathname();
118: }
119: return $fileList;
120: }
121:
122: }
123: