FormOptionsTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * Test class for FormOptions methods.
  12. *
  13. * Copyright © 2011, Antoine Musso
  14. *
  15. * @author Antoine Musso
  16. */
  17. class FormOptionsTest extends MediaWikiTestCase {
  18. /**
  19. * @var FormOptions
  20. */
  21. protected $object;
  22. /**
  23. * Instanciates a FormOptions object to play with.
  24. * FormOptions::add() is tested by the class FormOptionsInitializationTest
  25. * so we assume the function is well tested already an use it to create
  26. * the fixture.
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->object = new FormOptions;
  31. $this->object->add( 'string1', 'string one' );
  32. $this->object->add( 'string2', 'string two' );
  33. $this->object->add( 'integer', 0 );
  34. $this->object->add( 'float', 0.0 );
  35. $this->object->add( 'intnull', 0, FormOptions::INTNULL );
  36. }
  37. /** Helpers for testGuessType() */
  38. /* @{ */
  39. private function assertGuessBoolean( $data ) {
  40. $this->guess( FormOptions::BOOL, $data );
  41. }
  42. private function assertGuessInt( $data ) {
  43. $this->guess( FormOptions::INT, $data );
  44. }
  45. private function assertGuessFloat( $data ) {
  46. $this->guess( FormOptions::FLOAT, $data );
  47. }
  48. private function assertGuessString( $data ) {
  49. $this->guess( FormOptions::STRING, $data );
  50. }
  51. private function assertGuessArray( $data ) {
  52. $this->guess( FormOptions::ARR, $data );
  53. }
  54. /** Generic helper */
  55. private function guess( $expected, $data ) {
  56. $this->assertEquals(
  57. $expected,
  58. FormOptions::guessType( $data )
  59. );
  60. }
  61. /* @} */
  62. /**
  63. * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
  64. * @covers FormOptions::guessType
  65. */
  66. public function testGuessTypeDetection() {
  67. $this->assertGuessBoolean( true );
  68. $this->assertGuessBoolean( false );
  69. $this->assertGuessInt( 0 );
  70. $this->assertGuessInt( -5 );
  71. $this->assertGuessInt( 5 );
  72. $this->assertGuessInt( 0x0F );
  73. $this->assertGuessFloat( 0.0 );
  74. $this->assertGuessFloat( 1.5 );
  75. $this->assertGuessFloat( 1e3 );
  76. $this->assertGuessString( 'true' );
  77. $this->assertGuessString( 'false' );
  78. $this->assertGuessString( '5' );
  79. $this->assertGuessString( '0' );
  80. $this->assertGuessString( '1.5' );
  81. $this->assertGuessArray( [ 'foo' ] );
  82. }
  83. /**
  84. * @expectedException MWException
  85. * @covers FormOptions::guessType
  86. */
  87. public function testGuessTypeOnNullThrowException() {
  88. $this->object->guessType( null );
  89. }
  90. }