redact exception traces and abstract getTrace
[lhc/web/wiklou.git] / tests / phpunit / includes / MWExceptionHandlerTest.php
1 <?php
2 /**
3 * Tests for includes/Exception.php.
4 *
5 * @author Antoine Musso
6 * @copyright Copyright © 2013, Antoine Musso
7 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
8 * @file
9 */
10
11 class MWExceptionHandlerTest extends MediaWikiTestCase {
12
13 /**
14 * @covers MWExceptionHandler::getRedactedTrace
15 */
16 function testGetRedactedTrace() {
17 try {
18 $array = array( 'a', 'b' );
19 $object = new StdClass();
20 self::helperThrowAnException( $array, $object );
21 } catch (Exception $e) {
22 }
23
24 # Make sure our strack trace contains an array and an object passed to
25 # some function in the stacktrace. Else, we can not assert the trace
26 # redaction achieved its job.
27 $trace = $e->getTrace();
28 $hasObject = false;
29 $hasArray = false;
30 foreach ( $trace as $frame ) {
31 if ( ! isset( $frame['args'] ) ) {
32 continue;
33 }
34 foreach ( $frame['args'] as $arg ) {
35 $hasObject = $hasObject || is_object( $arg );
36 $hasArray = $hasArray || is_array( $arg );
37 }
38
39 if( $hasObject && $hasArray ) {
40 break;
41 }
42 }
43 $this->assertTrue( $hasObject,
44 "The stacktrace must have a function having an object has parameter" );
45 $this->assertTrue( $hasArray,
46 "The stacktrace must have a function having an array has parameter" );
47
48 # Now we redact the trace.. and make sure no function arguments are
49 # arrays or objects.
50 $redacted = MWExceptionHandler::getRedactedTrace( $e );
51
52 foreach ( $redacted as $frame ) {
53 if ( ! isset( $frame['args'] ) ) {
54 continue;
55 }
56 foreach ( $frame['args'] as $arg ) {
57 $this->assertNotInternalType( 'array', $arg);
58 $this->assertNotInternalType( 'object', $arg);
59 }
60 }
61 }
62
63 /**
64 * Helper function for testExpandArgumentsInCall
65 *
66 * Pass it an object and an array :-)
67 *
68 * @throws Exception
69 */
70 protected static function helperThrowAnException( $a, $b ) {
71 throw new Exception();
72 }
73 }