wiki.techinc.nl/tests/phpunit/unit/includes/FormOptionsTest.php

106 lines
2.6 KiB
PHP
Raw Normal View History

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.
*
* Copyright © 2011, Antoine Musso
2011-02-28 20:58:34 +00:00
*
* @author Antoine Musso
2011-02-28 20:58:34 +00:00
*/
class FormOptionsTest extends \MediaWikiUnitTestCase {
2011-02-28 20:58:34 +00:00
/**
* @var FormOptions
*/
protected $object;
/**
* 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.
*/
protected function setUp() : void {
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' );
$this->object->add( 'integer', 0 );
$this->object->add( 'float', 0.0 );
$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 );
}
2011-02-28 20:58:34 +00:00
private function assertGuessInt( $data ) {
$this->guess( FormOptions::INT, $data );
}
private function assertGuessFloat( $data ) {
$this->guess( FormOptions::FLOAT, $data );
}
2011-02-28 20:58:34 +00:00
private function assertGuessString( $data ) {
$this->guess( FormOptions::STRING, $data );
}
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 )
);
}
2011-02-28 20:58:34 +00:00
/* @} */
/**
* Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
* @covers FormOptions::guessType
2011-02-28 20:58:34 +00:00
*/
public function testGuessTypeDetection() {
$this->assertGuessBoolean( true );
2011-02-28 20:58:34 +00:00
$this->assertGuessBoolean( false );
$this->assertGuessInt( 0 );
$this->assertGuessInt( -5 );
$this->assertGuessInt( 5 );
2011-02-28 20:58:34 +00:00
$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' );
2011-02-28 20:58:34 +00:00
$this->assertGuessArray( [ 'foo' ] );
2011-02-28 20:58:34 +00:00
}
2011-02-28 20:58:34 +00:00
/**
* @covers FormOptions::guessType
2011-02-28 20:58:34 +00:00
*/
public function testGuessTypeOnNullThrowException() {
$this->expectException( MWException::class );
$this->object->guessType( null );
2011-02-28 20:58:34 +00:00
}
}