File: ScalarParamTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (99 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download
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
94
95
96
97
98
99
<?php

namespace Wikimedia\Tests\Message;

use InvalidArgumentException;
use MediaWiki\Json\JsonCodec;
use MediaWikiUnitTestCase;
use stdClass;
use Wikimedia\Message\MessageValue;
use Wikimedia\Message\ParamType;
use Wikimedia\Message\ScalarParam;

/**
 * @covers \Wikimedia\Message\ScalarParam
 */
class ScalarParamTest extends MediaWikiUnitTestCase {
	use MessageSerializationTestTrait;

	/**
	 * Overrides SerializationTestTrait::getClassToTest
	 * @return string
	 */
	public static function getClassToTest(): string {
		return ScalarParam::class;
	}

	public static function provideConstruct() {
		return [
			'num' => [
				[ ParamType::NUM, 1, ],
				'<num>1</num>',
			],
			'plain' => [
				[ ParamType::PLAINTEXT, 'foo & bar', ],
				'<plaintext>foo &amp; bar</plaintext>',
			],
			'text' => [
				[ ParamType::TEXT, new MessageValue( 'key' ), ],
				'<text><message key="key"></message></text>',
			],
			'T377912' => [
				[ ParamType::PLAINTEXT, T377912TestCase::class ],
				'<plaintext>' . T377912TestCase::class . '</plaintext>',
			]
		];
	}

	/** @dataProvider provideConstruct */
	public function testSerialize( $args, $_ ) {
		[ $type, $value ] = $args;
		$codec = new JsonCodec;
		$obj = new ScalarParam( $type, $value );

		$serialized = $codec->serialize( $obj );
		$newObj = $codec->deserialize( $serialized );

		// XXX: would be nice to have a proper ::equals() method.
		$this->assertEquals( $obj->dump(), $newObj->dump() );
	}

	/** @dataProvider provideConstruct */
	public function testConstruct( $args, $expected ) {
		[ $type, $value ] = $args;
		$mp = new ScalarParam( $type, $value );
		$this->assertSame( $type, $mp->getType() );
		$this->assertSame( $value, $mp->getValue() );
		$this->assertSame( $expected, $mp->dump() );
	}

	public function testConstruct_badType() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage(
			'ParamType::LIST cannot be used with ScalarParam; use ListParam instead'
		);
		new ScalarParam( ParamType::LIST, [] );
	}

	public function testConstruct_badTypeConst() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( '$type must be one of the ParamType constants' );
		new ScalarParam( 'invalid', '' );
	}

	public function testConstruct_badValueNULL() {
		$this->expectDeprecationAndContinue(
			'/Using null as message parameter was deprecated/'
		);
		new ScalarParam( ParamType::TEXT, null );
	}

	public function testConstruct_badValueClass() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage(
			'Scalar parameter must be a string, number, Stringable, or MessageSpecifier; got stdClass'
		);
		new ScalarParam( ParamType::TEXT, new stdClass );
	}

}