Add class implementing MessagePack serialization
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MWMessagePackTest.php
1 <?php
2 /**
3 * PHP Unit tests for MWMessagePack
4 * @covers MWMessagePack
5 */
6 class MWMessagePackTest extends MediaWikiTestCase {
7
8 /* @var array Array of test cases, keyed by type. Each type is an array of
9 * (value, expected encoding as hex string). The expected values were
10 * generated using <https://github.com/onlinecity/msgpack-php>, which
11 * includes a serialization function.
12 */
13 public $data = array(
14 'integer' => array(
15 array( 0, '00' ),
16 array( 1, '01' ),
17 array( 5, '05' ),
18 array( -1, 'ff' ),
19 array( -2, 'fe' ),
20 array( 35, '23' ),
21 array( -35, 'd0dd' ),
22 array( 128, 'cc80' ),
23 array( -128, 'd080' ),
24 array( 1000, 'cd03e8' ),
25 array( -1000, 'd1fc18' ),
26 array( 100000, 'ce000186a0' ),
27 array( -100000, 'd2fffe7960' ),
28 array( 10000000000, 'cf00000002540be400' ),
29 array( -10000000000, 'd3fffffffdabf41c00' ),
30 array( -223372036854775807, 'd3fce66c50e2840001' ),
31 array( -9223372036854775807, 'd38000000000000001' ),
32 ),
33 'NULL' => array(
34 array( null, 'c0' ),
35 ),
36 'boolean' => array(
37 array( true, 'c3' ),
38 array( false, 'c2' ),
39 ),
40 'double' => array(
41 array( 0.1, 'cb3fb999999999999a' ),
42 array( 1.1, 'cb3ff199999999999a' ),
43 array( 123.456, 'cb405edd2f1a9fbe77' ),
44 ),
45 'string' => array(
46 array( '', 'a0' ),
47 array( 'foobar', 'a6666f6f626172' ),
48 array(
49 'Lorem ipsum dolor sit amet amet.',
50 'da00204c6f72656d20697073756d20646f6c6f722073697420616d657420616d65742e'
51 ),
52 ),
53 'array' => array(
54 array( array( 'abc', 'def', 'ghi' ), '93a3616263a3646566a3676869' ),
55 array( array( 'one' => 1, 'two' => 2 ), '82a36f6e6501a374776f02' ),
56 ),
57 );
58
59 /**
60 * Verify that values are serialized correctly.
61 * @covers MWMessagePack::pack
62 */
63 public function testMessagePack() {
64 foreach( $this->data as $type => $cases ) {
65 foreach( $cases as $case ) {
66 list( $value, $expected ) = $case;
67 $actual = bin2hex( MWMessagePack::pack( $value ) );
68 $this->assertEquals( $actual, $expected, $type );
69 }
70 }
71 }
72 }