rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / XmlSelectTest.php
blob52e20bdb99d1071e892dbeeaa6cb3d9b3f9c34b8
1 <?php
3 /**
4 * @group Xml
5 */
6 class XmlSelectTest extends MediaWikiTestCase {
8 /**
9 * @var XmlSelect
11 protected $select;
13 protected function setUp() {
14 parent::setUp();
15 $this->select = new XmlSelect();
18 protected function tearDown() {
19 parent::tearDown();
20 $this->select = null;
23 /**
24 * @covers XmlSelect::__construct
26 public function testConstructWithoutParameters() {
27 $this->assertEquals( '<select></select>', $this->select->getHTML() );
30 /**
31 * Parameters are $name (false), $id (false), $default (false)
32 * @dataProvider provideConstructionParameters
33 * @covers XmlSelect::__construct
35 public function testConstructParameters( $name, $id, $default, $expected ) {
36 $this->select = new XmlSelect( $name, $id, $default );
37 $this->assertEquals( $expected, $this->select->getHTML() );
40 /**
41 * Provide parameters for testConstructParameters() which use three
42 * parameters:
43 * - $name (default: false)
44 * - $id (default: false)
45 * - $default (default: false)
46 * Provides a fourth parameters representing the expected HTML output
48 public static function provideConstructionParameters() {
49 return [
50 /**
51 * Values are set following a 3-bit Gray code where two successive
52 * values differ by only one value.
53 * See https://en.wikipedia.org/wiki/Gray_code
55 # $name $id $default
56 [ false, false, false, '<select></select>' ],
57 [ false, false, 'foo', '<select></select>' ],
58 [ false, 'id', 'foo', '<select id="id"></select>' ],
59 [ false, 'id', false, '<select id="id"></select>' ],
60 [ 'name', 'id', false, '<select name="name" id="id"></select>' ],
61 [ 'name', 'id', 'foo', '<select name="name" id="id"></select>' ],
62 [ 'name', false, 'foo', '<select name="name"></select>' ],
63 [ 'name', false, false, '<select name="name"></select>' ],
67 /**
68 * @covers XmlSelect::addOption
70 public function testAddOption() {
71 $this->select->addOption( 'foo' );
72 $this->assertEquals(
73 '<select><option value="foo">foo</option></select>',
74 $this->select->getHTML()
78 /**
79 * @covers XmlSelect::addOption
81 public function testAddOptionWithDefault() {
82 $this->select->addOption( 'foo', true );
83 $this->assertEquals(
84 '<select><option value="1">foo</option></select>',
85 $this->select->getHTML()
89 /**
90 * @covers XmlSelect::addOption
92 public function testAddOptionWithFalse() {
93 $this->select->addOption( 'foo', false );
94 $this->assertEquals(
95 '<select><option value="foo">foo</option></select>',
96 $this->select->getHTML()
101 * @covers XmlSelect::addOption
103 public function testAddOptionWithValueZero() {
104 $this->select->addOption( 'foo', 0 );
105 $this->assertEquals(
106 '<select><option value="0">foo</option></select>',
107 $this->select->getHTML()
112 * @covers XmlSelect::setDefault
114 public function testSetDefault() {
115 $this->select->setDefault( 'bar1' );
116 $this->select->addOption( 'foo1' );
117 $this->select->addOption( 'bar1' );
118 $this->select->addOption( 'foo2' );
119 $this->assertEquals(
120 '<select><option value="foo1">foo1</option>' . "\n" .
121 '<option value="bar1" selected="">bar1</option>' . "\n" .
122 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
126 * Adding default later on should set the correct selection or
127 * raise an exception.
128 * To handle this, we need to render the options in getHtml()
129 * @covers XmlSelect::setDefault
131 public function testSetDefaultAfterAddingOptions() {
132 $this->select->addOption( 'foo1' );
133 $this->select->addOption( 'bar1' );
134 $this->select->addOption( 'foo2' );
135 $this->select->setDefault( 'bar1' ); # setting default after adding options
136 $this->assertEquals(
137 '<select><option value="foo1">foo1</option>' . "\n" .
138 '<option value="bar1" selected="">bar1</option>' . "\n" .
139 '<option value="foo2">foo2</option></select>', $this->select->getHTML() );
143 * @covers XmlSelect::setAttribute
144 * @covers XmlSelect::getAttribute
146 public function testGetAttributes() {
147 # create some attributes
148 $this->select->setAttribute( 'dummy', 0x777 );
149 $this->select->setAttribute( 'string', 'euro €' );
150 $this->select->setAttribute( 1911, 'razor' );
152 # verify we can retrieve them
153 $this->assertEquals(
154 $this->select->getAttribute( 'dummy' ),
155 0x777
157 $this->assertEquals(
158 $this->select->getAttribute( 'string' ),
159 'euro €'
161 $this->assertEquals(
162 $this->select->getAttribute( 1911 ),
163 'razor'
166 # inexistent keys should give us 'null'
167 $this->assertEquals(
168 $this->select->getAttribute( 'I DO NOT EXIT' ),
169 null
172 # verify string / integer
173 $this->assertEquals(
174 $this->select->getAttribute( '1911' ),
175 'razor'
177 $this->assertEquals(
178 $this->select->getAttribute( 'dummy' ),
179 0x777