public
Description: Automatic notifications of tests written in Simpletest on Gnome Desktop
Homepage: http://rafael.adm.br/opensource/simpletest-gnome-notify
Clone URL: git://github.com/rafaelp/simpletest-gnome-notify.git
simpletest-gnome-notify / simpletest-gnome-notify.php
100755 114 lines (92 sloc) 3.304 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/php
<?php
 
/*
simpletest-gnome-notify v0.1.1
Rafael Lima (http://rafael.adm.br) at Myfreecomm (http://myfreecomm.com.br)
http://rafael.adm.br/simpletest-gnome-notify
License: http://creativecommons.org/licenses/by/2.5/
 
Dependencies:
 
* Simpletest
http://www.lastcraft.com/simple_test.php
 
* libnotify
sudo apt-get install libnotify-bin
 
* pyinotify (for AutotestDaemon)
sudo apt-get install python-pyinotify
 
*/
 
class SimpletestGnomeNotify {
 
  private $expiration_in_secs = 2;
  private $fail_image = "gtk-dialog-error";
  private $pending_image = "gtk-dialog-warning";
  private $success_image = "gtk-dialog-info";
 
  public function run($path = '.') {
    chdir($path);
 
    /* Before running tests here I had a file which did this job
$command = './tests/runAllTests.php';
exec($command, $return, $status);
*/
 
    try {
      ob_start();
      $test = &new GroupTest('All tests');
      $files = scandir($path.'/tests');
      foreach($files as $file) {
        if(preg_match('/.test.php$/', $file)) {
          $test->addTestFile($path.'/tests/'.$file);
        }
      }
      $test->run(new TextReporter());
      $output = ob_get_contents();
      ob_end_clean();
    }
    catch(Exception $e) {
      $this->expiration_in_secs = 5;
      $this->notify("Error running test", $return[1], $this->fail_image);
      exit;
    }
 
    echo $output;
    $lines = split("\n",$output);
      
    foreach($lines as $line) {
      if(preg_match('/Test cases run: ([0-9]+)\/([0-9]+)/', $line, $matches)) {
        $testcases = $matches[1];
        preg_match('/Passes: ([0-9]+)/', $line, $matches);
        $passes = $matches[1];
        preg_match('/Failures: ([0-9]+)/', $line, $matches);
        $failures = $matches[1];
        preg_match('/Exceptions: ([0-9]+)/', $line, $matches);
        $exceptions = $matches[1];
 
        $examples = $passes+$failures+$exceptions;
      }
    }
 
    if($failures > 0) {
      $this->notify("Tests Failed", $failures."/".$examples.(($failures == 1) ? " test failed" : " tests failed"), $this->fail_image);
    }
    elseif($exceptions > 0) {
      $this->notify("Tests with Exceptions", $exceptions."/".$examples.(($pendings == 1) ? " test is raising exceptions" : " tests are raising exceptions"), $this->pending_image);
    }
    elseif($pendings > 0) {
      $this->notify("Tests Pending", $pendings."/".$examples.(($pendings == 1) ? " test is pending" : " tests are pending"), $this->pending_image);
    }
    else {
      $this->notify("Tests Passed", "All ".$examples." tests passed", $this->success_image);
    }
  }
  
  private function notify($title, $message, $stock_icon) {
    $options = "-t ".($this->expiration_in_secs*1000)." -i ".$stock_icon;
    shell_exec("notify-send ".$options." '".$title."' '".$message."'");
  }
 
}
 
$simpletest_path = $argv[1];
if(empty($simpletest_path)) {
  echo "use ".__FILE__." [simpletest_path]\n";
  exit;
}
$simpletest_path = realpath($simpletest_path);
require_once($simpletest_path.'/unit_tester.php');
require_once($simpletest_path.'/reporter.php');
 
$path = realpath($argv[2]);
if(!file_exists($path.'/tests')) {
  echo "Path does seems to be corrected, if must have a directory called tests inside.\n";
  exit;
}
 
$test = new SimpletestGnomeNotify();
$test->run($path);
 
?>