統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / TestSuite / CakeTestLoader.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (3.633 KB)

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
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP(tm) Project
16
 * @since         CakePHP(tm) v 2.0
17
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
18
 * @package Cake.TestSuite
19
 */
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
/**
31
 * Load a file and find the first test case / suite in that file.
32
 *
33
 * @param string $filePath The file path to load
34
 * @param string $params Additional parameters
35
 * @return ReflectionClass
36
 */
37
        public function load($filePath, $params = '') {
38
                $file = $this->_resolveTestFile($filePath, $params);
39
                return parent::load('', $file);
40
        }
41

    
42
/**
43
 * Convert path fragments used by CakePHP's test runner to absolute paths that can be fed to PHPUnit.
44
 *
45
 * @param string $filePath The file path to load.
46
 * @param string $params Additional parameters.
47
 * @return string Converted path fragments.
48
 */
49
        protected function _resolveTestFile($filePath, $params) {
50
                $basePath = $this->_basePath($params) . DS . $filePath;
51
                $ending = 'Test.php';
52
                return (strpos($basePath, $ending) === (strlen($basePath) - strlen($ending))) ? $basePath : $basePath . $ending;
53
        }
54

    
55
/**
56
 * Generates the base path to a set of tests based on the parameters.
57
 *
58
 * @param array $params The path parameters.
59
 * @return string The base path.
60
 */
61
        protected static function _basePath($params) {
62
                $result = null;
63
                if (!empty($params['core'])) {
64
                        $result = CORE_TEST_CASES;
65
                } elseif (!empty($params['plugin'])) {
66
                        if (!CakePlugin::loaded($params['plugin'])) {
67
                                try {
68
                                        CakePlugin::load($params['plugin']);
69
                                        $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
70
                                } catch (MissingPluginException $e) {
71
                                }
72
                        } else {
73
                                $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
74
                        }
75
                } elseif (!empty($params['app'])) {
76
                        $result = APP_TEST_CASES;
77
                }
78
                return $result;
79
        }
80

    
81
/**
82
 * Get the list of files for the test listing.
83
 *
84
 * @param string $params Path parameters
85
 * @return array
86
 */
87
        public static function generateTestList($params) {
88
                $directory = static::_basePath($params);
89
                $fileList = static::_getRecursiveFileList($directory);
90

    
91
                $testCases = array();
92
                foreach ($fileList as $testCaseFile) {
93
                        $case = str_replace($directory . DS, '', $testCaseFile);
94
                        $case = str_replace('Test.php', '', $case);
95
                        $testCases[$testCaseFile] = $case;
96
                }
97
                sort($testCases);
98
                return $testCases;
99
        }
100

    
101
/**
102
 * Gets a recursive list of files from a given directory and matches then against
103
 * a given fileTestFunction, like isTestCaseFile()
104
 *
105
 * @param string $directory The directory to scan for files.
106
 * @return array
107
 */
108
        protected static function _getRecursiveFileList($directory = '.') {
109
                $fileList = array();
110
                if (!is_dir($directory)) {
111
                        return $fileList;
112
                }
113

    
114
                $files = new RegexIterator(
115
                        new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
116
                        '/.*Test.php$/'
117
                );
118

    
119
                foreach ($files as $file) {
120
                        $fileList[] = $file->getPathname();
121
                }
122
                return $fileList;
123
        }
124

    
125
}