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-2012, 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-2012, 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['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: * Get the list of files for the test listing.
82: *
83: * @return void
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: * Gets a recursive list of files from a given directory and matches then against
101: * a given fileTestFunction, like isTestCaseFile()
102: *
103: * @param string $directory The directory to scan for files.
104: * @param mixed $fileTestFunction
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: