summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/json/ServicesJsonTest.php
blob: 56dc64880250e4727e9eee21a5e7d9ac1cbe36ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/* 
 * Test cases for our Services_Json library. Requires PHP json support as well,
 * so we can compare output
 */
class ServicesJsonTest extends MediaWikiTestCase {
	/**
	 * Test to make sure core json_encode() and our Services_Json()->encode()
	 * produce the same output
	 *
	 * @dataProvider provideValuesToEncode
	 */
	public function testJsonEncode( $input, $desc ) {
		if ( !function_exists( 'json_encode' ) ) {
			$this->markTestIncomplete( 'No PHP json support, unable to test' );
			return;
		} elseif ( strtolower( json_encode( "\xf0\xa0\x80\x80" ) ) != '"\ud840\udc00"' ) {
			$this->markTestIncomplete( 'Have buggy PHP json support, unable to test' );
			return;
		} else {
			$jsonObj = new Services_JSON();
			$this->assertEquals(
				$jsonObj->encode( $input ),
				json_encode( $input ),
				$desc
			);
		}
	}

	/**
	 * Test to make sure core json_decode() and our Services_Json()->decode()
	 * produce the same output
	 *
	 * @dataProvider provideValuesToDecode
	 */
	public function testJsonDecode( $input, $desc ) {
		if ( !function_exists( 'json_decode' ) ) {
			$this->markTestIncomplete( 'No PHP json support, unable to test' );
			return;
		} else {
			$jsonObj = new Services_JSON();
			$this->assertEquals(
				$jsonObj->decode( $input ),
				json_decode( $input ),
				$desc
			);
		}
	}

	function provideValuesToEncode() {
		$obj = new stdClass();
		$obj->property = 'value';
		$obj->property2 = null;
		$obj->property3 = 1.234;
		return array(
			array( 1, 'basic integer' ),
			array( -1, 'negative integer' ),
			array( 1.1, 'basic float' ),
			array( true, 'basic bool true' ),
			array( false, 'basic bool false' ),
			array( 'some string', 'basic string test' ),
			array( "some string\nwith newline", 'newline string test' ),
			array( '♥ü', 'unicode string test' ),
			array( array( 'some', 'string', 'values' ), 'basic array of strings' ),
			array( array( 'key1' => 'val1', 'key2' => 'val2' ), 'array with string keys' ),
			array( array( 1 => 'val1', 3 => 'val2', '2' => 'val3' ), 'out of order numbered array test' ),
			array( array(), 'empty array test' ),
			array( $obj, 'basic object test' ),
			array( new stdClass, 'empty object test' ),
			array( null, 'null test' ),
		);
	}

	function provideValuesToDecode() {
		return array(
			array( '1', 'basic integer' ),
			array( '-1', 'negative integer' ),
			array( '1.1', 'basic float' ),
			array( '1.1e1', 'scientific float' ),
			array( 'true', 'basic bool true' ),
			array( 'false', 'basic bool false' ),
			array( '"some string"', 'basic string test' ),
			array( '"some string\nwith newline"', 'newline string test' ),
			array( '"♥ü"', 'unicode character string test' ),
			array( '"\u2665"', 'unicode \\u string test' ),
			array( '["some","string","values"]', 'basic array of strings' ),
			array( '[]', 'empty array test' ),
			array( '{"key":"value"}', 'Basic key => value test' ),
			array( '{}', 'empty object test' ),
			array( 'null', 'null test' ),
		);
	}
}