summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/FormOptionsTest.php
blob: 665fa390171d2f6ea3cc04d656c518ff59d66985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
 * This file host two test case classes for the MediaWiki FormOptions class:
 *  - FormOptionsInitializationTest : tests initialization of the class.
 *  - FormOptionsTest : tests methods an on instance
 *
 * The split let us take advantage of setting up a fixture for the methods
 * tests.
 */

/**
 * Test class for FormOptions methods.
 * Generated by PHPUnit on 2011-02-28 at 20:46:27.
 *
 * Copyright © 2011, Antoine Musso
 *
 * @author Antoine Musso
 */
class FormOptionsTest extends MediaWikiTestCase {
	/**
	 * @var FormOptions
	 */
	protected $object;

	/**
	 * Instanciates a FormOptions object to play with.
	 * FormOptions::add() is tested by the class FormOptionsInitializationTest
	 * so we assume the function is well tested already an use it to create
	 * the fixture.
	 */
	protected function setUp() {
		parent::setUp();
		$this->object = new FormOptions;
		$this->object->add( 'string1', 'string one' );
		$this->object->add( 'string2', 'string two' );
		$this->object->add( 'integer', 0 );
		$this->object->add( 'float', 0.0 );
		$this->object->add( 'intnull', 0, FormOptions::INTNULL );
	}

	/** Helpers for testGuessType() */
	/* @{ */
	private function assertGuessBoolean( $data ) {
		$this->guess( FormOptions::BOOL, $data );
	}
	private function assertGuessInt( $data ) {
		$this->guess( FormOptions::INT, $data );
	}
	private function assertGuessFloat( $data ) {
		$this->guess( FormOptions::FLOAT, $data );
	}
	private function assertGuessString( $data ) {
		$this->guess( FormOptions::STRING, $data );
	}

	/** Generic helper */
	private function guess( $expected, $data ) {
		$this->assertEquals(
			$expected,
			FormOptions::guessType( $data )
		);
	}
	/* @} */

	/**
	 * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
	 * @covers FormOptions::guessType
	 */
	public function testGuessTypeDetection() {
		$this->assertGuessBoolean( true );
		$this->assertGuessBoolean( false );

		$this->assertGuessInt( 0 );
		$this->assertGuessInt( -5 );
		$this->assertGuessInt( 5 );
		$this->assertGuessInt( 0x0F );

		$this->assertGuessFloat( 0.0 );
		$this->assertGuessFloat( 1.5 );
		$this->assertGuessFloat( 1e3 );

		$this->assertGuessString( 'true' );
		$this->assertGuessString( 'false' );
		$this->assertGuessString( '5' );
		$this->assertGuessString( '0' );
		$this->assertGuessString( '1.5' );
	}

	/**
	 * @expectedException MWException
	 * @covers FormOptions::guessType
	 */
	public function testGuessTypeOnArrayThrowException() {
		$this->object->guessType( array( 'foo' ) );
	}
	/**
	 * @expectedException MWException
	 * @covers FormOptions::guessType
	 */
	public function testGuessTypeOnNullThrowException() {
		$this->object->guessType( null );
	}
}