public static function suite() { global $argv, $freeze; PhpUnitServiceUtil::checkVersion(); $usage = "\n" . " Usage: phpunit [phpunit options] TestSuite.php <All|Framework|Misc|moduleName|TestClassName> [custom options]\n" . "\n" . " All Run all tests.\n" . " Framework Run the tests in app/protected/extensions/framework/tests/unit.\n" . " Misc Run the tests in app/protected/tests/unit.\n" . " moduleName Run the tests in app/protected/modules/moduleName/tests/unit.\n" . " TestClassName Run the tests in TestClassName.php, wherever that happens to be.\n" . "\n" . " Custom Options:\n" . "\n" . " --only-walkthroughs For the specified test, only includes tests under a walkthroughs directory.\n" . " --exclude-walkthroughs For the specified test, exclude tests under a walkthroughs directory.\n" . " --only-benchmarks For the specified test, only includes tests under a benchmarks directory.\n" . " --exclude-benchmarks For the specified test, exclude tests under a benchmarks directory.\n" . " --reuse-schema Reload a previously auto build database. (Will auto build if there is no\n" . " previous one. The auto built schema is dumped to the system temp dir in\n" . " autobuild.sql.)\n" . " --no-freeze Don't auto build and freeze the database.\n" . "\n" . " Examples:\n" . "\n" . " phpunit --verbose TestSuite.php accounts (Run the tests in the Accounts module.)\n" . " phpunit TestSuite.php RedBeanModelTest (Run the tests in RedBeanModelTest.php.)\n" . "\n" . " Note:\n" . "\n" . " Framework and Misc tests run only when -no-freeze is specified.\n" . "\n" . " To run specific tests use the phpunit --filter <regex> option.\n" . " phpunit has its own options. Check phpunit --help.\n\n"; // Not Coding Standard $onlyWalkthroughs = self::customOptionSet('--only-walkthroughs', $argv); $excludeWalkthroughs = self::customOptionSet('--exclude-walkthroughs', $argv); $onlyBenchmarks = self::customOptionSet('--only-benchmarks', $argv); $excludeBenchmarks = self::customOptionSet('--exclude-benchmarks', $argv); $reuse = self::customOptionSet('--reuse-schema', $argv); $freeze = !self::customOptionSet('--no-freeze', $argv); if ($freeze == true && FORCE_NO_FREEZE == true) { echo "\n\nBecause forceNoFreeze is set to TRUE in debugTest, you cannot run unit tests in frozen mode\n\n"; // Not Coding Standard exit; } if ($argv[count($argv) - 2] != 'TestSuite.php') { echo $usage; exit; } if ($onlyWalkthroughs && $onlyBenchmarks) { echo $usage; echo "It doesn't have sense to select both \"--only-walkthroughs\" and \"--only-benchmarks\" options. \n\n"; exit; } $whatToTest = $argv[count($argv) - 1]; $includeUnitTests = !$onlyWalkthroughs && !$onlyBenchmarks; $includeWalkthroughs = !$excludeWalkthroughs && !$onlyBenchmarks; $includeBenchmarks = !$excludeBenchmarks && !$onlyWalkthroughs; echo "Testing with database: '" . Yii::app()->db->connectionString . '\', ' . 'username: \'' . Yii::app()->db->username . "'.\n"; if ($freeze && !$reuse) { if (!is_writable(sys_get_temp_dir())) { echo "\n\nTemp directory must be writable to store reusable schema\n"; // Not Coding Standard echo "Temp directory: " . sys_get_temp_dir() . "\n\n"; // Not Coding Standard exit; } InstallUtil::connectToDatabaseWithConnectionString(Yii::app()->db->connectionString, Yii::app()->db->username, Yii::app()->db->password); echo "Auto building database schema...\n"; InstallUtil::dropAllTables(); Yii::app()->user->userModel = InstallUtil::createSuperUser('super', 'super'); $messageLogger = new MessageLogger(); InstallUtil::autoBuildDatabase($messageLogger); $messageLogger->printMessages(); ReadPermissionsOptimizationUtil::rebuild(); assert('RedBeanDatabase::isSetup()'); echo "Saving auto built schema...\n"; $schemaFile = sys_get_temp_dir() . '/autobuilt.sql'; $success = preg_match("/;dbname=([^;]+)/", Yii::app()->db->connectionString, $matches); // Not Coding Standard assert('$success == 1'); $databaseName = $matches[1]; $systemOutput = system('mysqldump -u' . Yii::app()->db->username . ' -p' . Yii::app()->db->password . ' ' . $databaseName . " > {$schemaFile}"); if ($systemOutput != null) { echo 'Dumping schema using system command. Output: ' . $systemOutput . "\n\n"; } InstallUtil::close(); echo "Database closed.\n"; assert('!RedBeanDatabase::isSetup()'); } $suite = new PHPUnit_Framework_TestSuite(); $suite->setName("{$whatToTest} Tests"); if (!$freeze) { self::buildAndAddSuiteFromDirectory($suite, 'Framework', COMMON_ROOT . '/protected/core/tests/unit', $whatToTest, true, false, $includeBenchmarks); } $moduleDirectoryName = COMMON_ROOT . '/protected/modules'; if (is_dir($moduleDirectoryName)) { $moduleNames = scandir($moduleDirectoryName); foreach ($moduleNames as $moduleName) { if ($moduleName != '.' && $moduleName != '..') { $moduleUnitTestDirectoryName = "{$moduleDirectoryName}/{$moduleName}/tests/unit"; self::buildAndAddSuiteFromDirectory($suite, $moduleName, $moduleUnitTestDirectoryName, $whatToTest, $includeUnitTests, $includeWalkthroughs, $includeBenchmarks); } } } if (!$freeze) { self::buildAndAddSuiteFromDirectory($suite, 'Misc', COMMON_ROOT . '/protected/tests/unit', $whatToTest, $includeUnitTests, $includeWalkthroughs, $includeBenchmarks); self::buildAndAddSuiteFromDirectory($suite, 'Commands', COMMON_ROOT . '/protected/commands/tests/unit', $whatToTest, $includeUnitTests, $includeWalkthroughs, $includeBenchmarks); //////////////////////////////////////////////////////////////////////////////// // Temporary - See Readme.txt in the notSupposedToBeHere directory. self::buildAndAddSuiteFromDirectory($suite, 'BadDependencies', COMMON_ROOT . '/protected/tests/unit/notSupposedToBeHere', $whatToTest, $includeUnitTests, $includeWalkthroughs, $includeBenchmarks); //////////////////////////////////////////////////////////////////////////////// } if ($suite->count() == 0) { echo $usage; echo " No tests found for '{$whatToTest}'.\n\n"; exit; } return $suite; }