Skip to content

Instantly share code, notes, and snippets.

@lsolesen
Created December 10, 2010 18:40
Show Gist options
  • Save lsolesen/736585 to your computer and use it in GitHub Desktop.
Save lsolesen/736585 to your computer and use it in GitHub Desktop.
Testlistener for phpunit notifying on error
<?php
class TestListener implements PHPUnit_Framework_TestListener
{
protected $errors;
protected $error_count;
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
$this->error_count++;
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
$this->error_count++;
}
public function addIncompleteTest(PHPUnit_Framework_Test $test,Exception $e,$time)
{
}
public function addSkippedTest(PHPUnit_Framework_Test $test,Exception $e,$time)
{
}
public function startTest(PHPUnit_Framework_Test $test)
{
}
public function endTest(PHPUnit_Framework_Test $test, $time)
{
}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
public function __destruct()
{
exec('svn info ' . dirname(__FILE__) . '/../', $output);
$msg = 'Build had ' . $this->error_count . ' errors. Please fix them' . "\n\n";
$msg .= implode("\n", $output);
if ($this->error_count > 0){
$this->notifo('project', 'Build failed', $msg, 'http://example.org', 'user', 'api_secret');
}
}
function notifo($label, $title, $msg, $uri, $user, $pass)
{
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Authorization: Basic ".base64_encode($user.":".$pass)."\r\n".
"Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array('label' => $label, 'title' => $title, 'msg' => $msg, 'uri' => $uri))
)
);
$context = stream_context_create($opts);
file_get_contents('https://api.notifo.com/v1/send_notification', false, $context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment