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