Merge "Follow-up Id0021594: Remove extra code for redlink suggestions"
[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 * Generated by PHPUnit on 2011-02-28 at 20:46:27.
14 *
15 * Copyright © 2011, Antoine Musso
16 *
17 * @author Antoine Musso
18 */
19 class FormOptionsTest extends MediaWikiTestCase {
20 /**
21 * @var FormOptions
22 */
23 protected $object;
24
25 /**
26 * Instanciates a FormOptions object to play with.
27 * FormOptions::add() is tested by the class FormOptionsInitializationTest
28 * so we assume the function is well tested already an use it to create
29 * the fixture.
30 */
31 protected function setUp() {
32 parent::setUp();
33 $this->object = new FormOptions;
34 $this->object->add( 'string1', 'string one' );
35 $this->object->add( 'string2', 'string two' );
36 $this->object->add( 'integer', 0 );
37 $this->object->add( 'float', 0.0 );
38 $this->object->add( 'intnull', 0, FormOptions::INTNULL );
39 }
40
41 /** Helpers for testGuessType() */
42 /* @{ */
43 private function assertGuessBoolean( $data ) {
44 $this->guess( FormOptions::BOOL, $data );
45 }
46 private function assertGuessInt( $data ) {
47 $this->guess( FormOptions::INT, $data );
48 }
49 private function assertGuessFloat( $data ) {
50 $this->guess( FormOptions::FLOAT, $data );
51 }
52 private function assertGuessString( $data ) {
53 $this->guess( FormOptions::STRING, $data );
54 }
55 private function assertGuessArray( $data ) {
56 $this->guess( FormOptions::ARR, $data );
57 }
58
59 /** Generic helper */
60 private function guess( $expected, $data ) {
61 $this->assertEquals(
62 $expected,
63 FormOptions::guessType( $data )
64 );
65 }
66 /* @} */
67
68 /**
69 * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
70 * @covers FormOptions::guessType
71 */
72 public function testGuessTypeDetection() {
73 $this->assertGuessBoolean( true );
74 $this->assertGuessBoolean( false );
75
76 $this->assertGuessInt( 0 );
77 $this->assertGuessInt( -5 );
78 $this->assertGuessInt( 5 );
79 $this->assertGuessInt( 0x0F );
80
81 $this->assertGuessFloat( 0.0 );
82 $this->assertGuessFloat( 1.5 );
83 $this->assertGuessFloat( 1e3 );
84
85 $this->assertGuessString( 'true' );
86 $this->assertGuessString( 'false' );
87 $this->assertGuessString( '5' );
88 $this->assertGuessString( '0' );
89 $this->assertGuessString( '1.5' );
90
91 $this->assertGuessArray( [ 'foo' ] );
92 }
93
94 /**
95 * @expectedException MWException
96 * @covers FormOptions::guessType
97 */
98 public function testGuessTypeOnNullThrowException() {
99 $this->object->guessType( null );
100 }
101 }