3 * File for the CiviTestSuite class
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
12 * This file is part of CiviCRM
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.
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.
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/>.
30 * Parent class for test suites
34 class CiviTestSuite
extends PHPUnit\Framework\TestSuite
{
37 * Simple name based constructor.
38 * @param string $theClass
41 public function __construct($theClass = '', $name = '') {
43 $name = str_replace('_',
48 // also split AllTests to All Tests
49 $name = str_replace('AllTests', 'All Tests', $name);
51 parent
::__construct($name);
53 // also load the class loader
54 require_once 'CRM/Core/ClassLoader.php';
55 CRM_Core_ClassLoader
::singleton()->register();
61 protected function setUp() {
62 //print __METHOD__ . "\n";
66 * Test suite teardown.
68 protected function tearDown() {
69 //print __METHOD__ . "\n";
73 * suppress failed test error issued by phpunit when it finds.
74 * a test suite with no tests
76 public function testNothing() {
81 * @return \PHPUnit_Framework_TestSuite
83 protected function implSuite($myfile) {
84 $name = str_replace('_',
89 // also split AllTests to All Tests
90 $name = str_replace('AllTests', 'All Tests', $name);
92 $suite = new PHPUnit\Framework\
TestSuite($name);
93 $this->addAllTests($suite, $myfile,
94 new SplFileInfo(dirname($myfile))
100 * Add all test classes *Test and all test suites *Tests in subdirectories
102 * @param PHPUnit\Framework\TestSuite $suite
103 * Test suite object to add tests to
105 * @param SplFileInfo $dirInfo
110 protected function addAllTests(
111 PHPUnit\Framework\TestSuite
&$suite,
112 $myfile, SplFileInfo
$dirInfo
114 //echo get_class($this)."::addAllTests($myfile,".$dirInfo->getRealPath().")\n";
115 if (!$dirInfo->isReadable()
116 ||
!$dirInfo->isDir()
121 // Pass 1: Check all *Tests.php files
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()
132 if ($fileInfo->getRealPath() == $myfile) {
133 // Don't create an infinite loop
134 //echo "ignoring {$fileInfo->getRealPath()}\n";
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,
147 if (preg_match('/Tests$/', $name)) {
148 $addTests[] = $name . '::suite';
154 foreach ($addTests as $addTest) {
155 $suite->addTest(call_user_func($addTest));
158 // Pass 2: Scan all subdirectories
159 // array(array(0 => $suite, 1 => $file, 2 => SplFileinfo))
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) != '.')
167 // This is a directory that may contain tests so scan it
168 $addAllTests[] = clone $fileInfo;
171 //$addAllTests = CRM_Utils_Array::crmArraySortByField($addAllTests, '1');
172 usort($addAllTests, function ($a, $b) {
173 return strnatcmp($a->getRealPath(), $b->getRealPath());
175 foreach ($addAllTests as $addAllTest) {
176 $this->addAllTests($suite, $myfile, $addAllTest);
179 // Pass 3: Check all *Test.php files in this directory
180 //echo "start Pass 3 on {$dirInfo->getRealPath()}\n";
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()
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,
200 if (strpos($fileInfo->getRealPath(), strtr($name, '_\\', '//') . ".php") !== FALSE) {
201 if (preg_match('/Test$/', $name)) {
202 $addTestSuites[] = $name;
208 sort($addTestSuites);
209 foreach ($addTestSuites as $addTestSuite) {
210 $suite->addTestSuite($addTestSuite);
213 // print_r(array($prefix, 'addTests' => $addTests, 'addAllTests' => $addAllTests, 'addTestSuites' => $addTestSuites));