Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[lhc/web/wiklou.git] / tests / phpunit / includes / FormOptionsTest.php
1 <?php
2 /**
3 * This file host two test case classes for the MediaWiki FormOptions class:
4 * - FormOptionsInitializationTest : tests initialization of the class.
5 * - FormOptionsTest : tests methods an on instance
6 *
7 * The split let us take advantage of setting up a fixture for the methods
8 * tests.
9 */
10
11 /**
12 * Test class for FormOptions methods.
13 *
14 * Copyright © 2011, Antoine Musso
15 *
16 * @author Antoine Musso
17 */
18 class FormOptionsTest extends MediaWikiTestCase {
19 /**
20 * @var FormOptions
21 */
22 protected $object;
23
24 /**
25 * Instanciates a FormOptions object to play with.
26 * FormOptions::add() is tested by the class FormOptionsInitializationTest
27 * so we assume the function is well tested already an use it to create
28 * the fixture.
29 */
30 protected function setUp() {
31 parent::setUp();
32 $this->object = new FormOptions;
33 $this->object->add( 'string1', 'string one' );
34 $this->object->add( 'string2', 'string two' );
35 $this->object->add( 'integer', 0 );
36 $this->object->add( 'float', 0.0 );
37 $this->object->add( 'intnull', 0, FormOptions::INTNULL );
38 }
39
40 /** Helpers for testGuessType() */
41 /* @{ */
42 private function assertGuessBoolean( $data ) {
43 $this->guess( FormOptions::BOOL, $data );
44 }
45
46 private function assertGuessInt( $data ) {
47 $this->guess( FormOptions::INT, $data );
48 }
49
50 private function assertGuessFloat( $data ) {
51 $this->guess( FormOptions::FLOAT, $data );
52 }
53
54 private function assertGuessString( $data ) {
55 $this->guess( FormOptions::STRING, $data );
56 }
57
58 private function assertGuessArray( $data ) {
59 $this->guess( FormOptions::ARR, $data );
60 }
61
62 /** Generic helper */
63 private function guess( $expected, $data ) {
64 $this->assertEquals(
65 $expected,
66 FormOptions::guessType( $data )
67 );
68 }
69
70 /* @} */
71
72 /**
73 * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
74 * @covers FormOptions::guessType
75 */
76 public function testGuessTypeDetection() {
77 $this->assertGuessBoolean( true );
78 $this->assertGuessBoolean( false );
79
80 $this->assertGuessInt( 0 );
81 $this->assertGuessInt( -5 );
82 $this->assertGuessInt( 5 );
83 $this->assertGuessInt( 0x0F );
84
85 $this->assertGuessFloat( 0.0 );
86 $this->assertGuessFloat( 1.5 );
87 $this->assertGuessFloat( 1e3 );
88
89 $this->assertGuessString( 'true' );
90 $this->assertGuessString( 'false' );
91 $this->assertGuessString( '5' );
92 $this->assertGuessString( '0' );
93 $this->assertGuessString( '1.5' );
94
95 $this->assertGuessArray( [ 'foo' ] );
96 }
97
98 /**
99 * @expectedException MWException
100 * @covers FormOptions::guessType
101 */
102 public function testGuessTypeOnNullThrowException() {
103 $this->object->guessType( null );
104 }
105 }