Merge "Revert "Add type hint against LinkTarget""
[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
56 /** Generic helper */
57 private function guess( $expected, $data ) {
58 $this->assertEquals(
59 $expected,
60 FormOptions::guessType( $data )
61 );
62 }
63 /* @} */
64
65 /**
66 * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
67 * @covers FormOptions::guessType
68 */
69 public function testGuessTypeDetection() {
70 $this->assertGuessBoolean( true );
71 $this->assertGuessBoolean( false );
72
73 $this->assertGuessInt( 0 );
74 $this->assertGuessInt( -5 );
75 $this->assertGuessInt( 5 );
76 $this->assertGuessInt( 0x0F );
77
78 $this->assertGuessFloat( 0.0 );
79 $this->assertGuessFloat( 1.5 );
80 $this->assertGuessFloat( 1e3 );
81
82 $this->assertGuessString( 'true' );
83 $this->assertGuessString( 'false' );
84 $this->assertGuessString( '5' );
85 $this->assertGuessString( '0' );
86 $this->assertGuessString( '1.5' );
87 }
88
89 /**
90 * @expectedException MWException
91 * @covers FormOptions::guessType
92 */
93 public function testGuessTypeOnArrayThrowException() {
94 $this->object->guessType( [ 'foo' ] );
95 }
96 /**
97 * @expectedException MWException
98 * @covers FormOptions::guessType
99 */
100 public function testGuessTypeOnNullThrowException() {
101 $this->object->guessType( null );
102 }
103 }