PrettyPageHandlerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. */
  6. namespace Whoops\Handler;
  7. use Whoops\TestCase;
  8. use Whoops\Handler\PrettyPageHandler;
  9. use RuntimeException;
  10. use InvalidArgumentException;
  11. class PrettyPageHandlerTest extends TestCase
  12. {
  13. /**
  14. * @return Whoops\Handler\JsonResponseHandler
  15. */
  16. private function getHandler()
  17. {
  18. return new PrettyPageHandler;
  19. }
  20. /**
  21. * @return RuntimeException
  22. */
  23. public function getException()
  24. {
  25. return new RuntimeException;
  26. }
  27. /**
  28. * Test that PrettyPageHandle handles the template without
  29. * any errors.
  30. * @covers Whoops\Handler\PrettyPageHandler::handle
  31. */
  32. public function testHandleWithoutErrors()
  33. {
  34. $run = $this->getRunInstance();
  35. $handler = $this->getHandler();
  36. $run->pushHandler($handler);
  37. ob_start();
  38. $run->handleException($this->getException());
  39. ob_get_clean();
  40. }
  41. /**
  42. * @covers Whoops\Handler\PrettyPageHandler::setPageTitle
  43. * @covers Whoops\Handler\PrettyPageHandler::getPageTitle
  44. */
  45. public function testGetSetPageTitle()
  46. {
  47. $title = 'My Cool Error Handler';
  48. $handler = $this->getHandler();
  49. $handler->setPageTitle($title);
  50. $this->assertEquals($title, $handler->getPagetitle());
  51. }
  52. /**
  53. * @covers Whoops\Handler\PrettyPageHandler::setResourcesPath
  54. * @covers Whoops\Handler\PrettyPageHandler::getResourcesPath
  55. */
  56. public function testGetSetResourcesPath()
  57. {
  58. $path = __DIR__; // guaranteed to be valid!
  59. $handler = $this->getHandler();
  60. $handler->setResourcesPath($path);
  61. $this->assertEquals($path, $handler->getResourcesPath());
  62. }
  63. /**
  64. * @covers Whoops\Handler\PrettyPageHandler::setResourcesPath
  65. * @expectedException InvalidArgumentException
  66. */
  67. public function testSetInvalidResourcesPath()
  68. {
  69. $path = __DIR__ . '/ZIMBABWE'; // guaranteed to be invalid!
  70. $this->getHandler()->setResourcesPath($path);
  71. }
  72. /**
  73. * @covers Whoops\Handler\PrettyPageHandler::getDataTables
  74. * @covers Whoops\Handler\PrettyPageHandler::addDataTable
  75. */
  76. public function testGetSetDataTables()
  77. {
  78. $handler = $this->getHandler();
  79. // should have no tables by default:
  80. $this->assertEmpty($handler->getDataTables());
  81. $tableOne = array(
  82. 'ice' => 'cream',
  83. 'ice-ice' => 'baby'
  84. );
  85. $tableTwo = array(
  86. 'dolan' =>'pls',
  87. 'time' => time()
  88. );
  89. $handler->addDataTable('table 1', $tableOne);
  90. $handler->addDataTable('table 2', $tableTwo);
  91. // should contain both tables:
  92. $tables = $handler->getDataTables();
  93. $this->assertCount(2, $tables);
  94. $this->assertEquals($tableOne, $tables['table 1']);
  95. $this->assertEquals($tableTwo, $tables['table 2']);
  96. // should contain only table 1
  97. $this->assertEquals($tableOne, $handler->getDataTables('table 1'));
  98. // should return an empty table:
  99. $this->assertEmpty($handler->getDataTables('ZIMBABWE!'));
  100. }
  101. /**
  102. * @covers Whoops\Handler\PrettyPageHandler::getDataTables
  103. * @covers Whoops\Handler\PrettyPageHandler::addDataTableCallback
  104. */
  105. public function testSetCallbackDataTables()
  106. {
  107. $handler = $this->getHandler();
  108. $this->assertEmpty($handler->getDataTables());
  109. $table1 = function() {
  110. return array(
  111. 'hammer' => 'time',
  112. 'foo' => 'bar',
  113. );
  114. };
  115. $expected1 = array('hammer' => 'time', 'foo' => 'bar');
  116. $table2 = function() use ($expected1) {
  117. return array(
  118. 'another' => 'table',
  119. 'this' => $expected1,
  120. );
  121. };
  122. $expected2 = array('another' => 'table', 'this' => $expected1);
  123. $table3 = create_function('', 'return array("oh my" => "how times have changed!");');
  124. $expected3 = array('oh my' => 'how times have changed!');
  125. // Sanity check, make sure expected values really are correct.
  126. $this->assertSame($expected1, $table1());
  127. $this->assertSame($expected2, $table2());
  128. $this->assertSame($expected3, $table3());
  129. $handler->addDataTableCallback('table1', $table1);
  130. $handler->addDataTableCallback('table2', $table2);
  131. $handler->addDataTableCallback('table3', $table3);
  132. $tables = $handler->getDataTables();
  133. $this->assertCount(3, $tables);
  134. // Supplied callable is wrapped in a closure
  135. $this->assertInstanceOf('Closure', $tables['table1']);
  136. $this->assertInstanceOf('Closure', $tables['table2']);
  137. $this->assertInstanceOf('Closure', $tables['table3']);
  138. // Run each wrapped callable and check results against expected output.
  139. $this->assertEquals($expected1, $tables['table1']());
  140. $this->assertEquals($expected2, $tables['table2']());
  141. $this->assertEquals($expected3, $tables['table3']());
  142. $this->assertSame($tables['table1'], $handler->getDataTables('table1'));
  143. $this->assertSame($expected1, call_user_func($handler->getDataTables('table1')));
  144. }
  145. /**
  146. * @covers Whoops\Handler\PrettyPageHandler::setEditor
  147. * @covers Whoops\Handler\PrettyPageHandler::getEditorHref
  148. */
  149. public function testSetEditorSimple()
  150. {
  151. $handler = $this->getHandler();
  152. $handler->setEditor('sublime');
  153. $this->assertEquals(
  154. $handler->getEditorHref('/foo/bar.php', 10),
  155. 'subl://open?url=file://%2Ffoo%2Fbar.php&line=10'
  156. );
  157. $this->assertEquals(
  158. $handler->getEditorHref('/foo/with space?.php', 2324),
  159. 'subl://open?url=file://%2Ffoo%2Fwith%20space%3F.php&line=2324'
  160. );
  161. $this->assertEquals(
  162. $handler->getEditorHref('/foo/bar/with-dash.php', 0),
  163. 'subl://open?url=file://%2Ffoo%2Fbar%2Fwith-dash.php&line=0'
  164. );
  165. }
  166. /**
  167. * @covers Whoops\Handler\PrettyPageHandler::setEditor
  168. * @covers Whoops\Handler\PrettyPageHandler::getEditorHref
  169. */
  170. public function testSetEditorCallable()
  171. {
  172. $handler = $this->getHandler();
  173. $handler->setEditor(function($file, $line) {
  174. $file = rawurlencode($file);
  175. $line = rawurlencode($line);
  176. return "http://google.com/search/?q=$file:$line";
  177. });
  178. $this->assertEquals(
  179. $handler->getEditorHref('/foo/bar.php', 10),
  180. 'http://google.com/search/?q=%2Ffoo%2Fbar.php:10'
  181. );
  182. }
  183. /**
  184. * @covers Whoops\Handler\PrettyPageHandler::setEditor
  185. * @covers Whoops\Handler\PrettyPageHandler::addEditor
  186. * @covers Whoops\Handler\PrettyPageHandler::getEditorHref
  187. */
  188. public function testAddEditor()
  189. {
  190. $handler = $this->getHandler();
  191. $handler->addEditor('test-editor', function($file, $line) {
  192. return "cool beans $file:$line";
  193. });
  194. $handler->setEditor('test-editor');
  195. $this->assertEquals(
  196. $handler->getEditorHref('hello', 20),
  197. 'cool beans hello:20'
  198. );
  199. }
  200. public function testEditorXdebug()
  201. {
  202. if (!extension_loaded('xdebug')) {
  203. $this->markTestSkipped('xdebug is not available');
  204. }
  205. $originalValue = ini_get('xdebug.file_link_format');
  206. $handler = $this->getHandler();
  207. $handler->setEditor('xdebug');
  208. ini_set('xdebug.file_link_format', '%f:%l');
  209. $this->assertEquals(
  210. '/foo/bar.php:10',
  211. $handler->getEditorHref('/foo/bar.php', 10)
  212. );
  213. ini_set('xdebug.file_link_format', 'subl://open?url=%f&line=%l');
  214. // xdebug doesn't do any URL encoded, matching that behaviour.
  215. $this->assertEquals(
  216. 'subl://open?url=/foo/with space?.php&line=2324',
  217. $handler->getEditorHref('/foo/with space?.php', 2324)
  218. );
  219. ini_set('xdebug.file_link_format', $originalValue);
  220. }
  221. }