Debian Codegraph @

tests/phpunit/unit/includes/libs/Message/ScalarParamTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Message;
4
5 use InvalidArgumentException;
6 use MediaWiki\Json\JsonCodec;
7 use MediaWikiUnitTestCase;
8 use stdClass;
9 use Wikimedia\Message\MessageValue;
10 use Wikimedia\Message\ParamType;
11 use Wikimedia\Message\ScalarParam;
12
13 /**
14 * @covers \Wikimedia\Message\ScalarParam
15 */
16 class ScalarParamTest extends MediaWikiUnitTestCase {
17 use MessageSerializationTestTrait;
18
19 /**
20 * Overrides SerializationTestTrait::getClassToTest
21 * @return string
22 */
23 public static function getClassToTest(): string {
24 return ScalarParam::class;
25 }
26
27 public static function provideConstruct() {
28 return [
29 'num' => [
30 [ ParamType::NUM, 1, ],
31 '<num>1</num>',
32 ],
33 'plain' => [
34 [ ParamType::PLAINTEXT, 'foo & bar', ],
35 '<plaintext>foo &amp; bar</plaintext>',
36 ],
37 'text' => [
38 [ ParamType::TEXT, new MessageValue( 'key' ), ],
39 '<text><message key="key"></message></text>',
40 ],
41 'T377912' => [
42 [ ParamType::PLAINTEXT, T377912TestCase::class ],
43 '<plaintext>' . T377912TestCase::class . '</plaintext>',
44 ]
45 ];
46 }
47
48 /** @dataProvider provideConstruct */
49 public function testSerialize( $args, $_ ) {
50 [ $type, $value ] = $args;
51 $codec = new JsonCodec;
52 $obj = new ScalarParam( $type, $value );
53
54 $serialized = $codec->serialize( $obj );
55 $newObj = $codec->deserialize( $serialized );
56
57 // XXX: would be nice to have a proper ::equals() method.
58 $this->assertEquals( $obj->dump(), $newObj->dump() );
59 }
60
61 /** @dataProvider provideConstruct */
62 public function testConstruct( $args, $expected ) {
63 [ $type, $value ] = $args;
64 $mp = new ScalarParam( $type, $value );
65 $this->assertSame( $type, $mp->getType() );
66 $this->assertSame( $value, $mp->getValue() );
67 $this->assertSame( $expected, $mp->dump() );
68 }
69
70 public function testConstruct_badType() {
71 $this->expectException( InvalidArgumentException::class );
72 $this->expectExceptionMessage(
73 'ParamType::LIST cannot be used with ScalarParam; use ListParam instead'
74 );
75 new ScalarParam( ParamType::LIST, [] );
76 }
77
78 public function testConstruct_badTypeConst() {
79 $this->expectException( InvalidArgumentException::class );
80 $this->expectExceptionMessage( '$type must be one of the ParamType constants' );
81 new ScalarParam( 'invalid', '' );
82 }
83
84 public function testConstruct_badValueNULL() {
85 $this->expectDeprecationAndContinue(
86 '/Using null as message parameter was deprecated/'
87 );
88 new ScalarParam( ParamType::TEXT, null );
89 }
90
91 public function testConstruct_badValueClass() {
92 $this->expectException( InvalidArgumentException::class );
93 $this->expectExceptionMessage(
94 'Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got stdClass'
95 );
96 new ScalarParam( ParamType::TEXT, new stdClass );
97 }
98
99 }