2011-02-28 20:58:34 +00:00
|
|
|
<?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.
|
|
|
|
|
*
|
2011-10-24 09:08:13 +00:00
|
|
|
* Copyright © 2011, Antoine Musso
|
2011-02-28 20:58:34 +00:00
|
|
|
*
|
2011-10-24 09:08:13 +00:00
|
|
|
* @author Antoine Musso
|
2011-02-28 20:58:34 +00:00
|
|
|
*/
|
2019-06-30 13:23:53 +00:00
|
|
|
class FormOptionsTest extends \MediaWikiUnitTestCase {
|
2011-02-28 20:58:34 +00:00
|
|
|
/**
|
|
|
|
|
* @var FormOptions
|
|
|
|
|
*/
|
|
|
|
|
protected $object;
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-23 17:02:36 +00:00
|
|
|
* Instanciates a FormOptions object to play with.
|
2011-02-28 20:58:34 +00:00
|
|
|
* FormOptions::add() is tested by the class FormOptionsInitializationTest
|
|
|
|
|
* so we assume the function is well tested already an use it to create
|
|
|
|
|
* the fixture.
|
|
|
|
|
*/
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function setUp() : void {
|
2012-10-23 17:02:36 +00:00
|
|
|
parent::setUp();
|
2011-02-28 20:58:34 +00:00
|
|
|
$this->object = new FormOptions;
|
|
|
|
|
$this->object->add( 'string1', 'string one' );
|
|
|
|
|
$this->object->add( 'string2', 'string two' );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->object->add( 'integer', 0 );
|
2013-10-12 15:34:15 +00:00
|
|
|
$this->object->add( 'float', 0.0 );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->object->add( 'intnull', 0, FormOptions::INTNULL );
|
2011-02-28 20:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Helpers for testGuessType() */
|
|
|
|
|
/* @{ */
|
|
|
|
|
private function assertGuessBoolean( $data ) {
|
|
|
|
|
$this->guess( FormOptions::BOOL, $data );
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2011-02-28 20:58:34 +00:00
|
|
|
private function assertGuessInt( $data ) {
|
|
|
|
|
$this->guess( FormOptions::INT, $data );
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2013-10-12 15:34:15 +00:00
|
|
|
private function assertGuessFloat( $data ) {
|
|
|
|
|
$this->guess( FormOptions::FLOAT, $data );
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2011-02-28 20:58:34 +00:00
|
|
|
private function assertGuessString( $data ) {
|
|
|
|
|
$this->guess( FormOptions::STRING, $data );
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2016-05-11 13:42:12 +00:00
|
|
|
private function assertGuessArray( $data ) {
|
|
|
|
|
$this->guess( FormOptions::ARR, $data );
|
|
|
|
|
}
|
2011-02-28 20:58:34 +00:00
|
|
|
|
|
|
|
|
/** Generic helper */
|
|
|
|
|
private function guess( $expected, $data ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
|
|
|
|
FormOptions::guessType( $data )
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2011-02-28 20:58:34 +00:00
|
|
|
/* @} */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers FormOptions::guessType
|
2011-02-28 20:58:34 +00:00
|
|
|
*/
|
|
|
|
|
public function testGuessTypeDetection() {
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertGuessBoolean( true );
|
2011-02-28 20:58:34 +00:00
|
|
|
$this->assertGuessBoolean( false );
|
|
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertGuessInt( 0 );
|
|
|
|
|
$this->assertGuessInt( -5 );
|
|
|
|
|
$this->assertGuessInt( 5 );
|
2011-02-28 20:58:34 +00:00
|
|
|
$this->assertGuessInt( 0x0F );
|
|
|
|
|
|
2013-10-12 15:34:15 +00:00
|
|
|
$this->assertGuessFloat( 0.0 );
|
|
|
|
|
$this->assertGuessFloat( 1.5 );
|
|
|
|
|
$this->assertGuessFloat( 1e3 );
|
|
|
|
|
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertGuessString( 'true' );
|
|
|
|
|
$this->assertGuessString( 'false' );
|
|
|
|
|
$this->assertGuessString( '5' );
|
|
|
|
|
$this->assertGuessString( '0' );
|
2013-10-12 15:34:15 +00:00
|
|
|
$this->assertGuessString( '1.5' );
|
2011-02-28 20:58:34 +00:00
|
|
|
|
2016-05-11 13:42:12 +00:00
|
|
|
$this->assertGuessArray( [ 'foo' ] );
|
2011-02-28 20:58:34 +00:00
|
|
|
}
|
2016-05-11 13:42:12 +00:00
|
|
|
|
2011-02-28 20:58:34 +00:00
|
|
|
/**
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers FormOptions::guessType
|
2011-02-28 20:58:34 +00:00
|
|
|
*/
|
|
|
|
|
public function testGuessTypeOnNullThrowException() {
|
2019-10-05 05:21:11 +00:00
|
|
|
$this->expectException( MWException::class );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->object->guessType( null );
|
2011-02-28 20:58:34 +00:00
|
|
|
}
|
|
|
|
|
}
|