/Debug/tests/debug_test.php

https://github.com/Yannix/zetacomponents · PHP · 314 lines · 229 code · 49 blank · 36 comment · 0 complexity · 80bd27c9e1098f62a4260cb2b1b9eb12 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Debug
  25. * @subpackage Tests
  26. */
  27. require_once 'test_classes.php';
  28. function testErrorHandler( $errno, $errstr, $errfile, $errline )
  29. {
  30. ezcDebug::debugHandler( $errno, $errstr, $errfile, $errline );
  31. return true;
  32. }
  33. /**
  34. * @package Debug
  35. * @subpackage Tests
  36. */
  37. class ezcDebugTest extends ezcTestCase
  38. {
  39. private $dbg;
  40. protected function setUp()
  41. {
  42. $this->dbg = ezcDebug::getInstance();
  43. $this->dbg->reset();
  44. $this->dbg->setOutputFormatter( new TestReporter() );
  45. }
  46. protected function tearDown()
  47. {
  48. restore_error_handler();
  49. }
  50. public function testGetAccessSuccess()
  51. {
  52. $this->assertEquals(
  53. new ezcDebugOptions(),
  54. $this->dbg->options,
  55. 'Property $options does not have proper default value.'
  56. );
  57. }
  58. public function testGetAccessFailures()
  59. {
  60. try
  61. {
  62. echo $this->dbg->foobar;
  63. $this->fail( 'Exception not thrown on get access to unknown property $foobar.' );
  64. }
  65. catch ( ezcBasePropertyNotFoundException $e )
  66. {}
  67. }
  68. public function testSetAccessSuccess()
  69. {
  70. $this->assertSetProperty(
  71. $this->dbg,
  72. 'options',
  73. array( new ezcDebugOptions(), )
  74. );
  75. }
  76. public function testSetAccessFailure()
  77. {
  78. $this->assertSetPropertyFails(
  79. $this->dbg,
  80. 'options',
  81. array( null, true, 23, 23.42, 'foobar', array(), new stdClass )
  82. );
  83. try
  84. {
  85. $this->dbg->foo = 23;
  86. $this->fail( 'ezcBasePropertyNotFoundException not throwen on set access to non-existent property.' );
  87. }
  88. catch ( ezcBasePropertyNotFoundException $e ) {}
  89. }
  90. public function testIssetAccessSuccess()
  91. {
  92. $this->assertTrue(
  93. isset( $this->dbg->options ),
  94. 'Property $options does not seem to be set.'
  95. );
  96. }
  97. public function testIssetAccessFailure()
  98. {
  99. $this->assertFalse(
  100. isset( $this->dbg->foobar ),
  101. 'Property $foobar seems to be set.'
  102. );
  103. }
  104. // Messages are already tested in DebugMemoryWriterTest.
  105. // Quick test if the basics work.
  106. public function testSimpleMessage()
  107. {
  108. $dbg = $this->dbg;
  109. $dbg->log("Running testSimpleMessage", 0, array("source" => "src", "category" => "cat") );
  110. $struct = $dbg->generateOutput();
  111. $this->assertEquals(1, count( $struct[0] ) );
  112. $this->assertEquals("Running testSimpleMessage", $struct[0][0]->message );
  113. }
  114. public function testMultipleMessages()
  115. {
  116. $dbg = $this->dbg;
  117. $dbg->log("msg1", 0, array("source" => "src", "category" => "cat") );
  118. $dbg->log("msg2", 0, array("source" => "src", "category" => "cat") );
  119. $dbg->log("msg3", 1, array("source" => "src", "category" => "cat") );
  120. $struct = $dbg->generateOutput();
  121. $this->assertEquals(3, count( $struct[0] ) );
  122. $this->assertEquals("msg1", $struct[0][0]->message );
  123. $this->assertEquals("msg2", $struct[0][1]->message );
  124. $this->assertEquals("msg3", $struct[0][2]->message );
  125. }
  126. // Timer is already tested in DebugTimerTest.
  127. // Quick test if the basics work.
  128. public function testTimers()
  129. {
  130. $dbg = $this->dbg;
  131. $dbg->startTimer("a", "c");
  132. $dbg->stopTimer("a");
  133. $struct = $dbg->generateOutput();
  134. $this->assertEquals(1, count( $struct[1] ) );
  135. $this->assertEquals("a", $struct[1][0]->name );
  136. $this->assertEquals("c", $struct[1][0]->group );
  137. }
  138. public function testTimersSwitch()
  139. {
  140. $dbg = $this->dbg;
  141. $dbg->startTimer("a", "c");
  142. $dbg->switchTimer( "b", "a" );
  143. $dbg->stopTimer("b");
  144. $struct = $dbg->generateOutput();
  145. $this->assertEquals(1, count( $struct[1] ) );
  146. $this->assertEquals("a", $struct[1][0]->name );
  147. $this->assertEquals("c", $struct[1][0]->group );
  148. }
  149. public function testDefaultTimers()
  150. {
  151. $dbg = $this->dbg;
  152. $dbg->startTimer("a");
  153. $dbg->stopTimer("a");
  154. $struct = $dbg->generateOutput();
  155. $this->assertEquals(1, count( $struct[1] ) );
  156. $this->assertEquals("a", $struct[1][0]->name );
  157. $this->assertEquals("", $struct[1][0]->group );
  158. }
  159. public function testDefaultSourceAndCategory()
  160. {
  161. $dbg = $this->dbg;
  162. $dbg->log("bla", 1);
  163. $struct = $dbg->generateOutput();
  164. $this->assertEquals(1, count( $struct[0] ) );
  165. $this->assertEquals("default", $struct[0][0]->category );
  166. // Changing the default source from the log.
  167. $dbg->getEventLog()->source = "bla";
  168. $dbg->log("bla", 1);
  169. $struct = $dbg->generateOutput();
  170. $this->assertEquals(2, count( $struct[0] ) );
  171. $this->assertEquals("bla", $struct[0][1]->source );
  172. }
  173. public function testIndependentFromEventLog()
  174. {
  175. $dbg = $this->dbg;
  176. $dbg->log("bla", 1);
  177. ezcLog::getInstance()->setMapper( new MyFakeMapper() );
  178. $struct = $dbg->generateOutput();
  179. $this->assertEquals(1, count( $struct[0] ) );
  180. $this->assertEquals("default", $struct[0][0]->category );
  181. $dbg->log("bla", 1);
  182. $struct = $dbg->generateOutput();
  183. $this->assertEquals(2, count( $struct[0] ) );
  184. }
  185. public function testLogStackTrace()
  186. {
  187. $dbg = $this->dbg;
  188. $dbg->log( "bla", 1, array(), true );
  189. ezcLog::getInstance()->setMapper( new MyFakeMapper() );
  190. $struct = $dbg->generateOutput();
  191. $this->assertInstanceOf(
  192. 'ezcDebugStacktraceIterator',
  193. $struct[0][0]->stackTrace
  194. );
  195. }
  196. public function testDebugErrorHandler()
  197. {
  198. $beforeTime = time();
  199. set_error_handler( 'testErrorHandler' );
  200. trigger_error( '[Paynet, templates] Cannot load template', E_USER_WARNING );
  201. restore_error_handler();
  202. $afterTime = time();
  203. $struct = $this->dbg->generateOutput();
  204. // Local spefics
  205. $this->assertGreaterThanOrEqual(
  206. $beforeTime,
  207. $struct[0][0]->datetime
  208. );
  209. $this->assertLessThanOrEqual(
  210. $afterTime,
  211. $struct[0][0]->datetime
  212. );
  213. // Unify results
  214. $struct[0][0]->datetime = null;
  215. $fakeStruct = array(
  216. array(
  217. new ezcDebugStructure(),
  218. ),
  219. array()
  220. );
  221. $fakeStruct[0][0]->message = 'Cannot load template';
  222. $fakeStruct[0][0]->severity = 1;
  223. $fakeStruct[0][0]->source = 'Paynet';
  224. $fakeStruct[0][0]->category = 'templates';
  225. $fakeStruct[0][0]->datetime = null;
  226. $fakeStruct[0][0]->verbosity = false;
  227. $fakeStruct[0][0]->file = __FILE__;
  228. $fakeStruct[0][0]->line = 221;
  229. $this->assertEquals(
  230. $fakeStruct,
  231. $struct
  232. );
  233. }
  234. public function testDebugStructureToString()
  235. {
  236. $struct = new ezcDebugStructure();
  237. $struct->message = 'Cannot load template';
  238. $struct->severity = 1;
  239. $struct->source = 'Paynet';
  240. $struct->category = 'templates';
  241. $struct->datetime = time();
  242. $struct->verbosity = false;
  243. $struct->file = __FILE__;
  244. $struct->line = 232;
  245. $fakeRes = <<<EOT
  246. message => {$struct->message}
  247. severity => {$struct->severity}
  248. source => {$struct->source}
  249. category => {$struct->category}
  250. datetime => {$struct->datetime}
  251. verbosity => {$struct->verbosity}
  252. file => {$struct->file}
  253. line => {$struct->line}
  254. EOT;
  255. $this->assertEquals(
  256. $fakeRes,
  257. $struct->toString()
  258. );
  259. }
  260. public static function suite()
  261. {
  262. return new PHPUnit_Framework_TestSuite("ezcDebugTest");
  263. }
  264. }
  265. ?>