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['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: 81: 82: 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: 100: 101: 102: 103: 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: