[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/t/ -> test_validation.php (source)

   1  <?php
   2  require_once ('init.php');
   3  
   4  class Mouse extends GP_Thing {
   5      var $table_basename = 'mice';
   6      var $field_names = array( 'id', 'name', 'rating', 'cat_id' );
   7      var $non_updatable_attributes = array( 'id', );
   8  
   9  	function restrict_fields( $project ) {
  10          $project->name_should_not_be('empty');
  11          $project->rating_should_be('positive_int');
  12      }
  13  
  14  	function normalize_fields( $args ) {
  15          $args = (array)$args;
  16          if ( isset( $args['cat_id'] ) ) {
  17              $args['cat_id'] = $this->force_false_to_null( $args['cat_id'] );
  18          }
  19          return $args;
  20      }
  21  }
  22  
  23  $gpdb->mice = '';
  24  
  25  
  26  class GP_Test_Validation extends GP_UnitTestCase {
  27  	function test_basic() {
  28          $mickey = new Mouse( array( 'id' => 5, 'name' => 'Mickey', 'rating' => 11, 'cat_id' => 1, ) );
  29          $this->assertEquals( true, $mickey->validate() );
  30          $minnie = new Mouse( array( 'id' => 5, 'name' => '', 'rating' => 11, 'cat_id' => 1, ) );
  31          $this->assertEquals( false, $minnie->validate() );
  32      }
  33  
  34  	function test_is_int() {
  35         $callback = GP_Validators::get( 'int' );
  36         $f = $callback['positive'];
  37         $this->assertEquals( true, $f('0') );
  38         $this->assertEquals( true, $f('1') );
  39         $this->assertEquals( true, $f('-1') );
  40         $this->assertEquals( true, $f('514') );
  41         $this->assertEquals( true, $f('-514') );
  42         $this->assertEquals( false, $f('aaa1aaa') );
  43         $this->assertEquals( false, $f('2.3') );
  44         $this->assertEquals( false, $f('aaa1') );
  45         $this->assertEquals( false, $f('1aaa') );
  46      }
  47  
  48  	function test_between() {
  49         $callback = GP_Validators::get( 'between' );
  50         $f = $callback['positive'];
  51         $this->assertEquals( true, $f( 0, -1, 2 ) );
  52      }
  53  
  54  }


Generated: Tue May 21 03:59:56 2013 Hosted by follow the white rabbit.