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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * For full copyright and license information, please see the LICENSE.txt
14: * Redistributions of files must retain the above copyright notice.
15: *
16: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
17: * @link http://cakephp.org CakePHP(tm) Project
18: * @since CakePHP(tm) v 2.0
19: * @license http://www.opensource.org/licenses/mit-license.php MIT License
20: * @package Cake.TestSuite
21: */
22:
23: /**
24: * TestLoader for CakePHP Test suite.
25: *
26: * Turns partial paths used on the testsuite console and web UI into full file paths.
27: *
28: * @package Cake.TestSuite
29: */
30: class CakeTestLoader extends PHPUnit_Runner_StandardTestSuiteLoader {
31:
32: /**
33: * Load a file and find the first test case / suite in that file.
34: *
35: * @param string $filePath
36: * @param string $params
37: * @return ReflectionClass
38: */
39: public function load($filePath, $params = '') {
40: $file = $this->_resolveTestFile($filePath, $params);
41: return parent::load('', $file);
42: }
43:
44: /**
45: * Convert path fragments used by Cake's test runner to absolute paths that can be fed to PHPUnit.
46: *
47: * @param string $filePath
48: * @param string $params
49: * @return void
50: */
51: protected function _resolveTestFile($filePath, $params) {
52: $basePath = $this->_basePath($params) . DS . $filePath;
53: $ending = 'Test.php';
54: return (strpos($basePath, $ending) === (strlen($basePath) - strlen($ending))) ? $basePath : $basePath . $ending;
55: }
56:
57: /**
58: * Generates the base path to a set of tests based on the parameters.
59: *
60: * @param array $params
61: * @return string The base path.
62: */
63: protected static function _basePath($params) {
64: $result = null;
65: if (!empty($params['core'])) {
66: $result = CORE_TEST_CASES;
67: } elseif (!empty($params['plugin'])) {
68: if (!CakePlugin::loaded($params['plugin'])) {
69: try {
70: CakePlugin::load($params['plugin']);
71: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
72: } catch (MissingPluginException $e) {
73: }
74: } else {
75: $result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
76: }
77: } elseif (!empty($params['app'])) {
78: $result = APP_TEST_CASES;
79: }
80: return $result;
81: }
82:
83: /**
84: * Get the list of files for the test listing.
85: *
86: * @param string $params
87: * @return array
88: */
89: public static function generateTestList($params) {
90: $directory = self::_basePath($params);
91: $fileList = self::_getRecursiveFileList($directory);
92:
93: $testCases = array();
94: foreach ($fileList as $testCaseFile) {
95: $case = str_replace($directory . DS, '', $testCaseFile);
96: $case = str_replace('Test.php', '', $case);
97: $testCases[$testCaseFile] = $case;
98: }
99: sort($testCases);
100: return $testCases;
101: }
102:
103: /**
104: * Gets a recursive list of files from a given directory and matches then against
105: * a given fileTestFunction, like isTestCaseFile()
106: *
107: * @param string $directory The directory to scan for files.
108: * @return array
109: */
110: protected static function _getRecursiveFileList($directory = '.') {
111: $fileList = array();
112: if (!is_dir($directory)) {
113: return $fileList;
114: }
115:
116: $files = new RegexIterator(
117: new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
118: '/.*Test.php$/'
119: );
120:
121: foreach ($files as $file) {
122: $fileList[] = $file->getPathname();
123: }
124: return $fileList;
125: }
126:
127: }
128: