Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[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 $refvar = 'value';
18 try {
19 $array = array( 'a', 'b' );
20 $object = new StdClass();
21 self::helperThrowAnException( $array, $object, $refvar );
22 } catch (Exception $e) {
23 }
24
25 # Make sure our strack trace contains an array and an object passed to
26 # some function in the stacktrace. Else, we can not assert the trace
27 # redaction achieved its job.
28 $trace = $e->getTrace();
29 $hasObject = false;
30 $hasArray = false;
31 foreach ( $trace as $frame ) {
32 if ( ! isset( $frame['args'] ) ) {
33 continue;
34 }
35 foreach ( $frame['args'] as $arg ) {
36 $hasObject = $hasObject || is_object( $arg );
37 $hasArray = $hasArray || is_array( $arg );
38 }
39
40 if( $hasObject && $hasArray ) {
41 break;
42 }
43 }
44 $this->assertTrue( $hasObject,
45 "The stacktrace must have a function having an object has parameter" );
46 $this->assertTrue( $hasArray,
47 "The stacktrace must have a function having an array has parameter" );
48
49 # Now we redact the trace.. and make sure no function arguments are
50 # arrays or objects.
51 $redacted = MWExceptionHandler::getRedactedTrace( $e );
52
53 foreach ( $redacted as $frame ) {
54 if ( ! isset( $frame['args'] ) ) {
55 continue;
56 }
57 foreach ( $frame['args'] as $arg ) {
58 $this->assertNotInternalType( 'array', $arg);
59 $this->assertNotInternalType( 'object', $arg);
60 }
61 }
62
63 $this->assertEquals( 'value', $refvar, 'Ensuring reference variable wasn\'t changed' );
64 }
65
66 /**
67 * Helper function for testExpandArgumentsInCall
68 *
69 * Pass it an object and an array, and something by reference :-)
70 *
71 * @throws Exception
72 */
73 protected static function helperThrowAnException( $a, $b, &$c ) {
74 throw new Exception();
75 }
76 }