Merge branch 'maint/7.0'
[ninja.git] / modules / test / controllers / ninja_unit_test.php
blob0515024a488ad507bcf366c3d1e169bd6c48e92a
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Unit_Test controller.
4 */
5 class Ninja_unit_test_Controller extends Ninja_Controller {
6 /**
7 * Run unit tests and show results!
8 */
9 public function index()
11 new Ninja_Unit_Test;
14 private function prereq() {
15 if(date::utc_offset() != 3600 * (1 + date("I"))) {
16 echo "Aborting: Report tests require CET as timezone (date.timezone = \"CET\" in php.ini, for example). Got ".date::utc_offset()."\n";
17 exit(1);
21 /**
22 * Run report tests
24 public function reports()
26 $this->prereq();
28 $test_results = array();
29 $config = Kohana::config('database.default');
30 if (isset($config['connection'])) {
31 $connection = $config['connection'];
32 if (isset($connection['database']) && $connection['database'])
33 $db_name = $connection['database'];
34 if (isset($connection['user']) && $connection['user'])
35 $db_user = $connection['user'];
36 if (isset($connection['pass']) && $connection['pass'])
37 $db_pass = $connection['pass'];
38 if (isset($connection['host']) && $connection['host'])
39 $db_host = $connection['host'];
40 if (isset($connection['type']) && $connection['type'])
41 $db_type = $connection['type'];
43 $importer = '$(rpm --eval %{_libdir})/merlin/import --nagios-cfg=/tmp/ninja-test/nagios.cfg';
44 $test_file = array();
45 $argv = isset($argv) ? $argv : $GLOBALS['argv'];
46 $argc = isset($argc) ? $argc : $GLOBALS['argc'];
47 for ($i = 1; $i < $argc; $i++) {
48 switch ($argv[$i]) {
49 case '--importer':
50 $importer = $argv[$i + 1];
51 break;
52 case '--db-name':
53 $db_name = $argv[$i + 1];
54 break;
55 case '--db-user':
56 $db_user = $argv[$i + 1];
57 break;
58 case '--db-pass':
59 $db_user = $argv[$i + 1];
60 break;
62 if (substr($argv[$i], 0, 2) == '--') {
63 $i++;
64 continue;
67 if (is_file($argv[$i])) {
68 echo "Adding $argv[$i] to testfiles\n";
69 $test_file[] = $argv[$i];
72 $passed = 0;
73 $failed = 0;
74 foreach ($test_file as $tfile) {
75 $test = new Ninja_Reports_Test($tfile);
76 $test->importer = $importer;
77 $test->db_name = $db_name;
78 $test->db_user = $db_user;
79 $test->db_pass = $db_pass;
80 $test->db_type = $db_type;
81 $test->db_host = $db_host;
82 if ($test->run_test_series() === -1) {
83 echo " $tfile : '$test->description' failed to run\n";
84 exit(1);
86 $all[] = $test;
87 $passed += $test->passed;
88 $failed += $test->failed;
90 if ($failed) {
91 echo "Failed test-cases:\n";
92 foreach ($all as $test) {
93 if (!empty($test->failed))
94 echo " " . $test->test_file . ", '" . $test->description . "'\n";
97 if (empty($all)) {
98 echo "Error: No tests found\n";
99 $failed += 1;
100 } else {
101 echo "$passed/".($passed+$failed)." tests passed.".($failed==0?" Hooray!":"")."\n";
103 if ($failed)
104 exit(1);
105 exit(0); //removing this causes ninja to print a 302 to tac
110 Drops all tables in Database() which "appear to be"
111 test data.
113 public function wipe_tables()
115 $db = Database::instance();
116 $tlist = $db->list_tables();
117 if( !count($tlist) ) {
118 throw new Exception("Query failed!");
120 foreach( $tlist as $table ) {
121 #echo "Checking table $table...\n";
122 if (strlen($table) >= 40) {
123 echo "Dropping table $table...\n";
124 if (!$db->query("drop table $table")) {
125 echo "Failed to drop table $table:" . $db->error_message() . "\n";