/test/test.php

https://github.com/mmayorov/php-hoptoad-notifier · PHP · 120 lines · 105 code · 13 blank · 2 comment · 2 complexity · 1e700d19853040305328c2f525842fe8 MD5 · raw file

  1. <?php
  2. require_once 'PHPUnit/Framework.php';
  3. require_once 'Services/Hoptoad.php';
  4. $_SERVER = array(
  5. 'HTTP_HOST' => 'localhost',
  6. 'REQUEST_URI' => '/example.php',
  7. 'HTTP_REFERER' => 'http://localhost/reports/something',
  8. );
  9. $_SESSION = array(
  10. 'var1' => 'val1',
  11. 'var2' => 'val2',
  12. );
  13. $_GET = array(
  14. 'get1' => 'val1',
  15. 'get2' => 'val2',
  16. );
  17. $_POST = array(
  18. 'post1' => 'val3',
  19. 'post2' => 'val4',
  20. );
  21. $_REQUEST = array_merge($_GET, $_POST);
  22. class HoptoadTest extends PHPUnit_Framework_TestCase
  23. {
  24. protected function setUp()
  25. {
  26. $this->hoptoad = new Services_Hoptoad('myAPIKey', 'production', 'pear', false, 2);
  27. $trace = array(
  28. array(
  29. 'class' => 'Hoptoad',
  30. 'file' => 'file.php',
  31. 'line' => 23,
  32. 'function' => 'foo',
  33. ),
  34. array(
  35. 'class' => 'Foo',
  36. 'file' => 'foo.php',
  37. 'line' => 242,
  38. 'function' => 'foo',
  39. ),
  40. array(
  41. 'class' => 'Bar',
  42. 'file' => 'bar.php',
  43. 'line' => 42,
  44. 'function' => 'bar',
  45. ),
  46. );
  47. $this->hoptoad->setParamsForNotify('ERROR', 'Something went wrong', 'foo', 23, $trace);
  48. }
  49. public function testRequestURI()
  50. {
  51. // check protocol support
  52. $this->assertEquals('http://localhost/example.php', $this->hoptoad->request_uri());
  53. $_SERVER['SERVER_PORT'] = 443;
  54. $this->assertEquals('https://localhost/example.php', $this->hoptoad->request_uri());
  55. $_SERVER['SERVER_PORT'] = 80;
  56. // Check query string support.
  57. $_SERVER['QUERY_STRING'] = 'commit=true';
  58. $this->assertEquals('http://localhost/example.php?commit=true', $this->hoptoad->request_uri());
  59. $_SERVER['QUERY_STRING'] = '';
  60. }
  61. public function testXMLBacktrace()
  62. {
  63. $expected_xml = <<<XML
  64. <root>
  65. <backtrace>
  66. <line file="foo" number="23"/>
  67. <line file="file.php" number="23" method="foo"/>
  68. <line file="foo.php" number="242" method="foo"/>
  69. <line file="bar.php" number="42" method="bar"/>
  70. </backtrace>
  71. </root>
  72. XML;
  73. $doc = new SimpleXMLElement('<root />');
  74. $this->hoptoad->addXmlBacktrace($doc);
  75. $this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
  76. }
  77. public function testXMLParams()
  78. {
  79. $expected_xml = <<<XML
  80. <root>
  81. <params>
  82. <var key="get1">val1</var>
  83. <var key="get2">val2</var>
  84. <var key="post1">val3</var>
  85. <var key="post2">val4</var>
  86. </params>
  87. </root>
  88. XML;
  89. $doc = new SimpleXMLElement('<root />');
  90. $this->hoptoad->addXmlVars($doc, 'params', $_REQUEST);
  91. $this->assertXmlStringEqualsXmlString($expected_xml, $doc->asXML());
  92. }
  93. public function testNotificationBody()
  94. {
  95. $xmllint = popen('xmllint --noout --schema test/hoptoad_2_0.xsd - 2> /dev/null', 'w');
  96. if ($xmllint) {
  97. fwrite($xmllint, $this->hoptoad->buildXmlNotice());
  98. $status = pclose($xmllint);
  99. $this->assertEquals(0, $status, "XML output did not validate against schema.");
  100. } else {
  101. $this->fail("Couldn't run xmllint command.");
  102. }
  103. }
  104. }
  105. ?>