]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/FormOptionsTest.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.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         private function assertGuessInt( $data ) {
46                 $this->guess( FormOptions::INT, $data );
47         }
48         private function assertGuessFloat( $data ) {
49                 $this->guess( FormOptions::FLOAT, $data );
50         }
51         private function assertGuessString( $data ) {
52                 $this->guess( FormOptions::STRING, $data );
53         }
54         private function assertGuessArray( $data ) {
55                 $this->guess( FormOptions::ARR, $data );
56         }
57
58         /** Generic helper */
59         private function guess( $expected, $data ) {
60                 $this->assertEquals(
61                         $expected,
62                         FormOptions::guessType( $data )
63                 );
64         }
65         /* @} */
66
67         /**
68          * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
69          * @covers FormOptions::guessType
70          */
71         public function testGuessTypeDetection() {
72                 $this->assertGuessBoolean( true );
73                 $this->assertGuessBoolean( false );
74
75                 $this->assertGuessInt( 0 );
76                 $this->assertGuessInt( -5 );
77                 $this->assertGuessInt( 5 );
78                 $this->assertGuessInt( 0x0F );
79
80                 $this->assertGuessFloat( 0.0 );
81                 $this->assertGuessFloat( 1.5 );
82                 $this->assertGuessFloat( 1e3 );
83
84                 $this->assertGuessString( 'true' );
85                 $this->assertGuessString( 'false' );
86                 $this->assertGuessString( '5' );
87                 $this->assertGuessString( '0' );
88                 $this->assertGuessString( '1.5' );
89
90                 $this->assertGuessArray( [ 'foo' ] );
91         }
92
93         /**
94          * @expectedException MWException
95          * @covers FormOptions::guessType
96          */
97         public function testGuessTypeOnNullThrowException() {
98                 $this->object->guessType( null );
99         }
100 }