In FileBackend:
[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 $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( 'intnull', 0, FormOptions::INTNULL );
37 }
38
39 /** Helpers for testGuessType() */
40 /* @{ */
41 private function assertGuessBoolean( $data ) {
42 $this->guess( FormOptions::BOOL, $data );
43 }
44 private function assertGuessInt( $data ) {
45 $this->guess( FormOptions::INT, $data );
46 }
47 private function assertGuessString( $data ) {
48 $this->guess( FormOptions::STRING, $data );
49 }
50
51 /** Generic helper */
52 private function guess( $expected, $data ) {
53 $this->assertEquals(
54 $expected,
55 FormOptions::guessType( $data )
56 );
57 }
58 /* @} */
59
60 /**
61 * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
62 */
63 public function testGuessTypeDetection() {
64 $this->assertGuessBoolean( true );
65 $this->assertGuessBoolean( false );
66
67 $this->assertGuessInt( 0 );
68 $this->assertGuessInt( -5 );
69 $this->assertGuessInt( 5 );
70 $this->assertGuessInt( 0x0F );
71
72 $this->assertGuessString( 'true' );
73 $this->assertGuessString( 'false' );
74 $this->assertGuessString( '5' );
75 $this->assertGuessString( '0' );
76 }
77
78 /**
79 * @expectedException MWException
80 */
81 public function testGuessTypeOnArrayThrowException() {
82 $this->object->guessType( array( 'foo' ) );
83 }
84 /**
85 * @expectedException MWException
86 */
87 public function testGuessTypeOnNullThrowException() {
88 $this->object->guessType( null );
89 }
90 }