Skip to content

Commit

Permalink
a start at tests, borrowed from Scott Woods
Browse files Browse the repository at this point in the history
  • Loading branch information
rich committed Jan 24, 2010
1 parent 08f853a commit 570573f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Services/Hoptoad.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ public function exceptionHandler($exception)
$this->notify(get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTrace()); $this->notify(get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTrace());
} }


/**
* Set the values to be used for the next notice sent to Hoptoad
* @return void
* @author Rich Cavanaugh
**/
public function setParamsForNotify($error_class, $message, $file, $line, $trace, $component=NULL)
{
$this->error_class = $error_class;
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->trace = $trace;
$this->component = $component;
}

/** /**
* Pass the error and environment data on to Hoptoad * Pass the error and environment data on to Hoptoad
* *
Expand All @@ -160,18 +175,13 @@ public function exceptionHandler($exception)
* @param string $line * @param string $line
* @param array $trace * @param array $trace
* @param string $environment * @param string $environment
* *
* @author Rich Cavanaugh * @author Rich Cavanaugh
* @todo Handle response (e.g. errors) * @todo Handle response (e.g. errors)
*/ */
function notify($error_class, $message, $file, $line, $trace, $component=NULL) function notify($error_class, $message, $file, $line, $trace, $component=NULL)
{ {
$this->error_class = $error_class; $this->setParamsForNotify($error_class, $message, $file, $line, $trace, $component);
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->trace = $trace;
$this->component = $component;


$url = "http://hoptoadapp.com/notifier_api/v2/notices"; $url = "http://hoptoadapp.com/notifier_api/v2/notices";
$headers = array( $headers = array(
Expand Down
77 changes: 77 additions & 0 deletions test/hoptoad_2_0.xsd
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="notice">
<xs:complexType>
<xs:all>
<xs:element name="api-key" type="xs:string"/>
<xs:element name="notifier" type="notifier"/>
<xs:element name="error" type="error"/>
<xs:element name="request" type="request" minOccurs="0"/>
<xs:element name="server-environment" type="serverEnvironment"/>
</xs:all>
<xs:attribute name="version" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<xs:complexType name="notifier">
<xs:all>
<xs:element name="name" type="xs:string"/>
<xs:element name="version" type="xs:string"/>
<xs:element name="url" type="xs:string"/>
</xs:all>
</xs:complexType>

<xs:complexType name="error">
<xs:all>
<xs:element name="class" type="xs:string"/>
<xs:element name="message" type="xs:string" minOccurs="0"/>
<xs:element name="backtrace" type="backtrace"/>
</xs:all>
</xs:complexType>

<xs:complexType name="backtrace">
<xs:sequence>
<xs:element name="line" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="file" type="xs:string" use="required"/>
<xs:attribute name="number" type="xs:string" use="required"/>
<xs:attribute name="method" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="request">
<xs:all>
<xs:element name="url" type="xs:string"/>
<xs:element name="component" type="xs:string"/>
<xs:element name="action" type="xs:string" minOccurs="0"/>
<xs:element name="params" type="varList" minOccurs="0"/>
<xs:element name="session" type="varList" minOccurs="0"/>
<xs:element name="cgi-data" type="varList" minOccurs="0"/>
</xs:all>
</xs:complexType>

<xs:complexType name="varList">
<xs:sequence>
<xs:element name="var" type="var" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="var" mixed="true">
<xs:sequence>
<xs:element name="var" type="var" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="key" type="xs:string" use="required"/>
</xs:complexType>

<xs:complexType name="serverEnvironment">
<xs:sequence>
<xs:element name="project-root" type="xs:string" minOccurs="0"/>
<xs:element name="environment-name" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

120 changes: 120 additions & 0 deletions test/test.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,120 @@

<?php
require_once 'PHPUnit/Framework.php';
require_once 'Services/Hoptoad.php';

$_SERVER = array(
'HTTP_HOST' => 'localhost',
'REQUEST_URI' => '/example.php',
'HTTP_REFERER' => 'http://localhost/reports/something',
);

$_SESSION = array(
'var1' => 'val1',
'var2' => 'val2',
);

$_GET = array(
'get1' => 'val1',
'get2' => 'val2',
);

$_POST = array(
'post1' => 'val3',
'post2' => 'val4',
);

$_REQUEST = array_merge($_GET, $_POST);

class HoptoadTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->hoptoad = new Services_Hoptoad('myAPIKey', 'production', 'pear', false, 2);

$trace = array(
array(
'class' => 'Hoptoad',
'file' => 'file.php',
'line' => 23,
'function' => 'foo',
),
array(
'class' => 'Foo',
'file' => 'foo.php',
'line' => 242,
'function' => 'foo',
),
array(
'class' => 'Bar',
'file' => 'bar.php',
'line' => 42,
'function' => 'bar',
),
);
$this->hoptoad->setParamsForNotify('ERROR', 'Something went wrong', 'foo', 23, $trace);
}

public function testRequestURI()
{
// check protocol support
$this->assertEquals('http://localhost/example.php', $this->hoptoad->request_uri());
$_SERVER['SERVER_PORT'] = 443;
$this->assertEquals('https://localhost/example.php', $this->hoptoad->request_uri());
$_SERVER['SERVER_PORT'] = 80;

// Check query string support.
$_SERVER['QUERY_STRING'] = 'commit=true';
$this->assertEquals('http://localhost/example.php?commit=true', $this->hoptoad->request_uri());
$_SERVER['QUERY_STRING'] = '';
}

public function testXMLBacktrace()
{
$expected_xml = <<<XML
<root>
<backtrace>
<line file="foo" number="23"/>
<line file="file.php" number="23" method="foo"/>
<line file="foo.php" number="242" method="foo"/>
<line file="bar.php" number="42" method="bar"/>
</backtrace>
</root>
XML;
$doc = new SimpleXMLElement('<root />');
$this->hoptoad->addXmlBacktrace($doc);
$this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
}

public function testXMLParams()
{
$expected_xml = <<<XML
<root>
<params>
<var key="get1">val1</var>
<var key="get2">val2</var>
<var key="post1">val3</var>
<var key="post2">val4</var>
</params>
</root>
XML;
$doc = new SimpleXMLElement('<root />');
$this->hoptoad->addXmlVars($doc, 'params', $_REQUEST);
$this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
}

public function testNotificationBody()
{
$xmllint = popen('xmllint --noout --schema test/hoptoad_2_0.xsd - 2> /dev/null', 'w');
if ($xmllint) {
fwrite($xmllint, $this->hoptoad->buildXmlNotice());
$status = pclose($xmllint);
$this->assertEquals(0, $status, "XML output did not validate against schema.");
} else {
$this->fail("Couldn't run xmllint command.");
}
}

}
?>

0 comments on commit 570573f

Please sign in to comment.