1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22: 23: 24: 25: 26: 27: 28:
29: class CakeTestLoader extends PHPUnit_Runner_StandardTestSuiteLoader {
30:
31: 32: 33: 34: 35: 36: 37:
38: public function load($filePath, $params = '') {
39: $file = $this->_resolveTestFile($filePath, $params);
40: return parent::load('', $file);
41: }
42:
43: 44: 45: 46: 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: 56: 57: 58: 59:
60: protected static function _basePath($params) {
61: $result = null;
62: if (!empty($params['core'])) {
63: $result = CORE_TEST_CASES;
64: } elseif (!empty($params['plugin'])) {
65: if (!CakePlugin::loaded($params['plugin'])) {
66: try {
67: CakePlugin::load($params['plugin']);
68: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
69: } catch (MissingPluginException $e) {
70: }
71: } else {
72: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
73: }
74: } elseif (!empty($params['app'])) {
75: $result = APP_TEST_CASES;
76: }
77: return $result;
78: }
79:
80: 81: 82: 83: 84:
85: public static function generateTestList($params) {
86: $directory = self::_basePath($params);
87: $fileList = self::_getRecursiveFileList($directory);
88:
89: $testCases = array();
90: foreach ($fileList as $testCaseFile) {
91: $case = str_replace($directory . DS, '', $testCaseFile);
92: $case = str_replace('Test.php', '', $case);
93: $testCases[$testCaseFile] = $case;
94: }
95: sort($testCases);
96: return $testCases;
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: protected static function _getRecursiveFileList($directory = '.') {
107: $fileList = array();
108: if (!is_dir($directory)) {
109: return $fileList;
110: }
111:
112: $files = new RegexIterator(
113: new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
114: '/.*Test.php$/'
115: );
116:
117: foreach ($files as $file) {
118: $fileList[] = $file->getPathname();
119: }
120: return $fileList;
121: }
122:
123: }
124: