Debian Codegraph @

tests/phpunit/integration/includes/HTMLForm/HTMLFormFieldTestCase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\HTMLForm;
4
5 use InvalidArgumentException;
6 use MediaWiki\HTMLForm\HTMLForm;
7 use MediaWiki\HTMLForm\HTMLFormField;
8 use MediaWiki\Language\RawMessage;
9 use MediaWikiIntegrationTestCase;
10
11 abstract class HTMLFormFieldTestCase extends MediaWikiIntegrationTestCase {
12 /** @var string|null */
13 protected $className = null;
14
15 /**
16 * Augment assertEquals a little bit by stripping newlines and tabs from $expected.
17 * This allows $expected to be expressed as a heredoc string.
18 *
19 * @param string $expected HTML
20 * @param string $actual HTML
21 * @param string $msg Optional message
22 */
23 private function assertHTMLEqualStrippingWhitespace( $expected, $actual, $msg = '' ) {
24 $this->assertEquals(
25 str_replace( [ "\n", "\t" ], '', $expected ),
26 str_replace( [ "\n", "\t" ], '', $actual ),
27 $msg
28 );
29 }
30
31 public function constructField( array $params ): HTMLFormField {
32 if ( $this->className === null ) {
33 throw new InvalidArgumentException(
34 'HTMLFormFieldTestCase subclass ' . __CLASS__ . ' must override $className or constructField()'
35 );
36 }
37
38 $form = $this->createMock( HTMLForm::class );
39 $form->method( 'getLanguage' )
40 ->willReturnCallback(
41 fn () => $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' )
42 );
43 $form->method( 'msg' )
44 ->willReturnCallback( static fn ( ...$args ) => new RawMessage( ...$args ) );
45
46 return new $this->className( $params + [
47 'parent' => $form,
48 'name' => 'testfield'
49 ] );
50 }
51
52 public static function provideInputHtml() {
53 return [];
54 }
55
56 /**
57 * @dataProvider provideInputHtml
58 */
59 public function testGetInputHtml( $params, $value, $expected ) {
60 $field = $this->constructField( $params );
61 $this->assertHTMLEqualStrippingWhitespace( $expected, $field->getInputHtml( $value ) );
62 }
63
64 public static function provideInputOOUI() {
65 return [];
66 }
67
68 /**
69 * @dataProvider provideInputOOUI
70 */
71 public function testGetInputOOUI( $params, $value, $expected ) {
72 \OOUI\Theme::setSingleton( new \OOUI\BlankTheme() );
73
74 $field = $this->constructField( $params );
75 $this->assertHTMLEqualStrippingWhitespace( $expected, $field->getInputOOUI( $value ) );
76 }
77
78 public static function provideInputCodex() {
79 return [];
80 }
81
82 /**
83 * @dataProvider provideInputCodex
84 */
85 public function testGetInputCodex( $params, $value, $hasError, $expected ) {
86 $field = $this->constructField( $params );
87 $this->assertHTMLEqualStrippingWhitespace( $expected, $field->getInputCodex( $value, $hasError ) );
88 }
89
90 }