a tiny mvc framework for php using php-activerecord
at master 166 lines 4.6 kB view raw
1<?php 2include 'helpers/config.php'; 3 4class BookNumericality extends ActiveRecord\Model 5{ 6 static $table_name = 'books'; 7 8 static $validates_numericality_of = array( 9 array('name') 10 ); 11} 12 13class ValidatesNumericalityOfTest extends DatabaseTest 14{ 15 static $NULL = array(null); 16 static $BLANK = array("", " ", " \t \r \n"); 17 static $FLOAT_STRINGS = array('0.0','+0.0','-0.0','10.0','10.5','-10.5','-0.0001','-090.1'); 18 static $INTEGER_STRINGS = array('0', '+0', '-0', '10', '+10', '-10', '0090', '-090'); 19 static $FLOATS = array(0.0, 10.0, 10.5, -10.5, -0.0001); 20 static $INTEGERS = array(0, 10, -10); 21 static $JUNK = array("not a number", "42 not a number", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"); 22 23 public function set_up($connection_name=null) 24 { 25 parent::set_up($connection_name); 26 BookNumericality::$validates_numericality_of = array( 27 array('numeric_test') 28 ); 29 } 30 31 private function assert_validity($value, $boolean, $msg=null) 32 { 33 $book = new BookNumericality; 34 $book->numeric_test = $value; 35 36 if ($boolean == 'valid') 37 { 38 $this->assert_true($book->save()); 39 $this->assert_false($book->errors->is_invalid('numeric_test')); 40 } 41 else 42 { 43 $this->assert_false($book->save()); 44 $this->assert_true($book->errors->is_invalid('numeric_test')); 45 46 if (!is_null($msg)) 47 $this->assert_same($msg, $book->errors->on('numeric_test')); 48 } 49 } 50 51 private function assert_invalid($values, $msg=null) 52 { 53 foreach ($values as $value) 54 $this->assert_validity($value, 'invalid', $msg); 55 } 56 57 private function assert_valid($values, $msg=null) 58 { 59 foreach ($values as $value) 60 $this->assert_validity($value, 'valid', $msg); 61 } 62 63 public function test_numericality() 64 { 65 //$this->assert_invalid(array("0xdeadbeef")); 66 67 $this->assert_valid(array_merge(self::$FLOATS, self::$INTEGERS)); 68 $this->assert_invalid(array_merge(self::$NULL, self::$BLANK, self::$JUNK)); 69 } 70 71 public function test_not_anumber() 72 { 73 $this->assert_invalid(array('blah'), 'is not a number'); 74 } 75 76 public function test_invalid_null() 77 { 78 $this->assert_invalid(array(null)); 79 } 80 81 public function test_invalid_blank() 82 { 83 $this->assert_invalid(array(' ', ' '), 'is not a number'); 84 } 85 86 public function test_invalid_whitespace() 87 { 88 $this->assert_invalid(array('')); 89 } 90 91 public function test_valid_null() 92 { 93 BookNumericality::$validates_numericality_of[0]['allow_null'] = true; 94 $this->assert_valid(array(null)); 95 } 96 97 public function test_only_integer() 98 { 99 BookNumericality::$validates_numericality_of[0]['only_integer'] = true; 100 101 $this->assert_valid(array(1, '1')); 102 $this->assert_invalid(array(1.5, '1.5')); 103 } 104 105 public function test_only_integer_matching_does_not_ignore_other_options() 106 { 107 BookNumericality::$validates_numericality_of[0]['only_integer'] = true; 108 BookNumericality::$validates_numericality_of[0]['greater_than'] = 0; 109 110 $this->assert_invalid(array(-1,'-1')); 111 } 112 113 public function test_greater_than() 114 { 115 BookNumericality::$validates_numericality_of[0]['greater_than'] = 5; 116 117 $this->assert_valid(array(6, '7')); 118 $this->assert_invalid(array(5, '5'), 'must be greater than 5'); 119 } 120 121 public function test_greater_than_or_equal_to() 122 { 123 BookNumericality::$validates_numericality_of[0]['greater_than_or_equal_to'] = 5; 124 125 $this->assert_valid(array(5, 5.1, '5.1')); 126 $this->assert_invalid(array(-50, 4.9, '4.9','-5.1')); 127 } 128 129 public function test_less_than() 130 { 131 BookNumericality::$validates_numericality_of[0]['less_than'] = 5; 132 133 $this->assert_valid(array(4.9, -1, 0, '-5')); 134 $this->assert_invalid(array(5, '5'), 'must be less than 5'); 135 } 136 137 public function test_less_than_or_equal_to() 138 { 139 BookNumericality::$validates_numericality_of[0]['less_than_or_equal_to'] = 5; 140 141 $this->assert_valid(array(5, -1, 0, 4.9, '-5')); 142 $this->assert_invalid(array('8', 5.1), 'must be less than or equal to 5'); 143 } 144 145 public function test_greater_than_less_than_and_even() 146 { 147 BookNumericality::$validates_numericality_of[0] = array('numeric_test', 'greater_than' => 1, 'less_than' => 4, 'even' => true); 148 149 $this->assert_valid(array(2)); 150 $this->assert_invalid(array(1,3,4)); 151 } 152 153 public function test_custom_message() 154 { 155 BookNumericality::$validates_numericality_of = array( 156 array('numeric_test', 'message' => 'Hello') 157 ); 158 $book = new BookNumericality(array('numeric_test' => 'NaN')); 159 $book->is_valid(); 160 $this->assert_equals(array('Numeric test Hello'),$book->errors->full_messages()); 161 } 162}; 163 164array_merge(ValidatesNumericalityOfTest::$INTEGERS, ValidatesNumericalityOfTest::$INTEGER_STRINGS); 165array_merge(ValidatesNumericalityOfTest::$FLOATS, ValidatesNumericalityOfTest::$FLOAT_STRINGS); 166?>