Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

a start at tests, borrowed from Scott Woods

  • Loading branch information...
commit 570573fef0516697d151089dcb059d44352c78b8 1 parent 08f853a
@rich authored
Showing with 214 additions and 7 deletions.
  1. +17 −7 Services/Hoptoad.php
  2. +77 −0 test/hoptoad_2_0.xsd
  3. +120 −0 test/test.php
View
24 Services/Hoptoad.php
@@ -152,6 +152,21 @@ public function exceptionHandler($exception)
}
/**
+ * 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
*
* @param mixed $error_class
@@ -160,18 +175,13 @@ public function exceptionHandler($exception)
* @param string $line
* @param array $trace
* @param string $environment
- *
+ *
* @author Rich Cavanaugh
* @todo Handle response (e.g. errors)
*/
function notify($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;
+ $this->setParamsForNotify($error_class, $message, $file, $line, $trace, $component);
$url = "http://hoptoadapp.com/notifier_api/v2/notices";
$headers = array(
View
77 test/hoptoad_2_0.xsd
@@ -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>
+
View
120 test/test.php
@@ -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.");
+ }
+ }
+
+}
+?>
+
Please sign in to comment.
Something went wrong with that request. Please try again.