Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /** | |
3 | * File for the CiviTestSuite class | |
4 | * | |
5 | * (PHP 5) | |
6 | * | |
7 | * @copyright Copyright CiviCRM LLC (C) 2009 | |
8 | * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html | |
9 | * GNU Affero General Public License version 3 | |
10 | * @package CiviCRM | |
11 | * | |
12 | * This file is part of CiviCRM | |
13 | * | |
14 | * CiviCRM is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU Affero General Public License | |
16 | * as published by the Free Software Foundation; either version 3 of | |
17 | * the License, or (at your option) any later version. | |
18 | * | |
19 | * CiviCRM is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU Affero General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU Affero General Public | |
25 | * License along with this program. If not, see | |
26 | * <http://www.gnu.org/licenses/>. | |
27 | */ | |
28 | ||
6a488035 TO |
29 | /** |
30 | * Parent class for test suites | |
31 | * | |
32 | * @package CiviCRM | |
33 | */ | |
a6439b6a | 34 | class CiviTestSuite extends PHPUnit\Framework\TestSuite { |
6a488035 TO |
35 | |
36 | /** | |
eceb18cc | 37 | * Simple name based constructor. |
1e1fdcf6 EM |
38 | * @param string $theClass |
39 | * @param string $name | |
6a488035 | 40 | */ |
00be9182 | 41 | public function __construct($theClass = '', $name = '') { |
6a488035 TO |
42 | if (empty($name)) { |
43 | $name = str_replace('_', | |
44 | ' ', | |
45 | get_class($this) | |
46 | ); | |
47 | ||
48 | // also split AllTests to All Tests | |
49 | $name = str_replace('AllTests', 'All Tests', $name); | |
50 | } | |
51 | parent::__construct($name); | |
52 | ||
53 | // also load the class loader | |
54 | require_once 'CRM/Core/ClassLoader.php'; | |
55 | CRM_Core_ClassLoader::singleton()->register(); | |
56 | } | |
57 | ||
58 | /** | |
eceb18cc | 59 | * Test suite setup. |
6a488035 TO |
60 | */ |
61 | protected function setUp() { | |
62 | //print __METHOD__ . "\n"; | |
63 | } | |
64 | ||
65 | /** | |
eceb18cc | 66 | * Test suite teardown. |
6a488035 TO |
67 | */ |
68 | protected function tearDown() { | |
69 | //print __METHOD__ . "\n"; | |
70 | } | |
71 | ||
72 | /** | |
eceb18cc | 73 | * suppress failed test error issued by phpunit when it finds. |
6a488035 TO |
74 | * a test suite with no tests |
75 | */ | |
00be9182 | 76 | public function testNothing() { |
6a488035 TO |
77 | } |
78 | ||
79 | /** | |
1e1fdcf6 EM |
80 | * @param $myfile |
81 | * @return \PHPUnit_Framework_TestSuite | |
6a488035 TO |
82 | */ |
83 | protected function implSuite($myfile) { | |
84 | $name = str_replace('_', | |
85 | ' ', | |
86 | get_class($this) | |
87 | ); | |
88 | ||
89 | // also split AllTests to All Tests | |
90 | $name = str_replace('AllTests', 'All Tests', $name); | |
91 | ||
a6439b6a | 92 | $suite = new PHPUnit\Framework\TestSuite($name); |
6a488035 TO |
93 | $this->addAllTests($suite, $myfile, |
94 | new SplFileInfo(dirname($myfile)) | |
95 | ); | |
96 | return $suite; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Add all test classes *Test and all test suites *Tests in subdirectories | |
101 | * | |
a6439b6a | 102 | * @param PHPUnit\Framework\TestSuite $suite |
72b3a70c | 103 | * Test suite object to add tests to |
2a6da8d7 EM |
104 | * @param $myfile |
105 | * @param SplFileInfo $dirInfo | |
72b3a70c | 106 | * object to scan |
2a6da8d7 | 107 | * |
72b3a70c | 108 | * @return void |
6a488035 | 109 | */ |
5896d037 | 110 | protected function addAllTests( |
a6439b6a | 111 | PHPUnit\Framework\TestSuite &$suite, |
5896d037 | 112 | $myfile, SplFileInfo $dirInfo |
6a488035 TO |
113 | ) { |
114 | //echo get_class($this)."::addAllTests($myfile,".$dirInfo->getRealPath().")\n"; | |
115 | if (!$dirInfo->isReadable() | |
116 | || !$dirInfo->isDir() | |
117 | ) { | |
118 | return; | |
119 | } | |
120 | ||
121 | // Pass 1: Check all *Tests.php files | |
39b959db SL |
122 | // array(callable) |
123 | $addTests = array(); | |
6a488035 TO |
124 | //echo "start Pass 1 on {$dirInfo->getRealPath()}\n"; |
125 | $dir = new DirectoryIterator($dirInfo->getRealPath()); | |
126 | foreach ($dir as $fileInfo) { | |
127 | if ($fileInfo->isReadable() && $fileInfo->isFile() | |
128 | && preg_match('/Tests.php$/', | |
129 | $fileInfo->getFilename() | |
130 | ) | |
131 | ) { | |
132 | if ($fileInfo->getRealPath() == $myfile) { | |
133 | // Don't create an infinite loop | |
134 | //echo "ignoring {$fileInfo->getRealPath()}\n"; | |
135 | continue; | |
136 | } | |
137 | //echo "checking file ".$fileInfo->getRealPath( )."\n"; | |
138 | // This is a file with a name ending in 'Tests.php'. | |
139 | // Get all classes defined in the file and add those | |
140 | // with a class name ending in 'Test' to the test suite | |
141 | $oldClassNames = get_declared_classes(); | |
142 | require_once $fileInfo->getRealPath(); | |
143 | $newClassNames = get_declared_classes(); | |
144 | foreach (array_diff($newClassNames, | |
145 | $oldClassNames | |
146 | ) as $name) { | |
147 | if (preg_match('/Tests$/', $name)) { | |
148 | $addTests[] = $name . '::suite'; | |
149 | } | |
150 | } | |
151 | } | |
152 | } | |
153 | sort($addTests); | |
154 | foreach ($addTests as $addTest) { | |
155 | $suite->addTest(call_user_func($addTest)); | |
156 | } | |
157 | ||
158 | // Pass 2: Scan all subdirectories | |
39b959db SL |
159 | // array(array(0 => $suite, 1 => $file, 2 => SplFileinfo)) |
160 | $addAllTests = array(); | |
6a488035 TO |
161 | $dir = new DirectoryIterator($dirInfo->getRealPath()); |
162 | //echo "start Pass 2 on {$dirInfo->getRealPath()}\n"; | |
163 | foreach ($dir as $fileInfo) { | |
164 | if ($fileInfo->isDir() | |
165 | && (substr($fileInfo->getFilename(), 0, 1) != '.') | |
166 | ) { | |
167 | // This is a directory that may contain tests so scan it | |
168 | $addAllTests[] = clone $fileInfo; | |
169 | } | |
170 | } | |
171 | //$addAllTests = CRM_Utils_Array::crmArraySortByField($addAllTests, '1'); | |
172 | usort($addAllTests, function ($a, $b) { | |
173 | return strnatcmp($a->getRealPath(), $b->getRealPath()); | |
174 | }); | |
175 | foreach ($addAllTests as $addAllTest) { | |
176 | $this->addAllTests($suite, $myfile, $addAllTest); | |
177 | } | |
178 | ||
179 | // Pass 3: Check all *Test.php files in this directory | |
180 | //echo "start Pass 3 on {$dirInfo->getRealPath()}\n"; | |
39b959db SL |
181 | // array(className) |
182 | $addTestSuites = array(); | |
6a488035 TO |
183 | $dir = new DirectoryIterator($dirInfo->getRealPath()); |
184 | foreach ($dir as $fileInfo) { | |
185 | if ($fileInfo->isReadable() && $fileInfo->isFile() | |
186 | && preg_match('/Test.php$/', | |
187 | $fileInfo->getFilename() | |
188 | ) | |
189 | ) { | |
190 | //echo "checking file ".$fileInfo->getRealPath( )."\n"; | |
191 | // This is a file with a name ending in 'Tests?.php'. | |
192 | // Get all classes defined in the file and add those | |
193 | // with a class name ending in 'Test' to the test suite | |
194 | $oldClassNames = get_declared_classes(); | |
195 | require_once $fileInfo->getRealPath(); | |
196 | $newClassNames = get_declared_classes(); | |
197 | foreach (array_diff($newClassNames, | |
198 | $oldClassNames | |
199 | ) as $name) { | |
4339bc55 | 200 | if (strpos($fileInfo->getRealPath(), strtr($name, '_\\', '//') . ".php") !== FALSE) { |
61f3a620 RN |
201 | if (preg_match('/Test$/', $name)) { |
202 | $addTestSuites[] = $name; | |
203 | } | |
6a488035 TO |
204 | } |
205 | } | |
206 | } | |
207 | } | |
208 | sort($addTestSuites); | |
209 | foreach ($addTestSuites as $addTestSuite) { | |
210 | $suite->addTestSuite($addTestSuite); | |
211 | } | |
212 | ||
213 | // print_r(array($prefix, 'addTests' => $addTests, 'addAllTests' => $addAllTests, 'addTestSuites' => $addTestSuites)); | |
214 | } | |
96025800 | 215 | |
6a488035 | 216 | } |