1<?php
2
3namespace Wikimedia\ParamValidator\TypeDef;
4
5use Wikimedia\Message\DataMessageValue;
6use Wikimedia\ParamValidator\ParamValidator;
7use Wikimedia\ParamValidator\SimpleCallbacks;
8use Wikimedia\ParamValidator\ValidationException;
9
10/**
11 * @covers \Wikimedia\ParamValidator\TypeDef\BooleanDef
12 */
13class BooleanDefTest extends TypeDefTestCase {
14
15	protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
16		return new BooleanDef( $callbacks, $options );
17	}
18
19	public function provideValidate() {
20		foreach ( [
21			[ BooleanDef::$TRUEVALS, true ],
22			[ BooleanDef::$FALSEVALS, false ],
23			[ [ '' ], false ],
24		] as list( $vals, $expect ) ) {
25			foreach ( $vals as $v ) {
26				yield "Value '$v'" => [ $v, $expect ];
27				$v2 = ucfirst( $v );
28				if ( $v2 !== $v ) {
29					yield "Value '$v2'" => [ $v2, $expect ];
30				}
31				$v3 = strtoupper( $v );
32				if ( $v3 !== $v2 ) {
33					yield "Value '$v3'" => [ $v3, $expect ];
34				}
35			}
36		}
37
38		yield "Value '2'" => [ 2, new ValidationException(
39			DataMessageValue::new( 'paramvalidator-badbool', [], 'badbool' ),
40			'test', '2', []
41		) ];
42
43		yield "Value 'foobar'" => [ 'foobar', new ValidationException(
44			DataMessageValue::new( 'paramvalidator-badbool', [], 'badbool' ),
45			'test', 'foobar', []
46		) ];
47	}
48
49	public function provideStringifyValue() {
50		return [
51			[ true, 'true' ],
52			[ false, 'false' ],
53		];
54	}
55
56	public function provideGetInfo() {
57		return [
58			'Basic test' => [
59				[],
60				[],
61				[
62					// phpcs:ignore Generic.Files.LineLength.TooLong
63					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-boolean"><text>1</text></message>',
64				],
65			],
66			'Multi-valued' => [
67				[ ParamValidator::PARAM_ISMULTI => true ],
68				[],
69				[
70					// phpcs:ignore Generic.Files.LineLength.TooLong
71					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-boolean"><text>2</text></message>',
72				],
73			],
74		];
75	}
76
77}
78