a tiny mvc framework for php using php-activerecord
at v1 158 lines 4.4 kB view raw
1<?php 2include 'helpers/config.php'; 3 4class BookExclusion extends ActiveRecord\Model 5{ 6 static $table = 'books'; 7 public static $validates_exclusion_of = array( 8 array('name', 'in' => array('blah', 'alpha', 'bravo')) 9 ); 10}; 11 12class BookInclusion extends ActiveRecord\Model 13{ 14 static $table = 'books'; 15 public static $validates_inclusion_of = array( 16 array('name', 'in' => array('blah', 'tanker', 'shark')) 17 ); 18}; 19 20class ValidatesInclusionAndExclusionOfTest extends DatabaseTest 21{ 22 public function set_up($connection_name=null) 23 { 24 parent::set_up($connection_name); 25 BookInclusion::$validates_inclusion_of[0] = array('name', 'in' => array('blah', 'tanker', 'shark')); 26 BookExclusion::$validates_exclusion_of[0] = array('name', 'in' => array('blah', 'alpha', 'bravo')); 27 } 28 29 public function test_inclusion() 30 { 31 $book = new BookInclusion; 32 $book->name = 'blah'; 33 $book->save(); 34 $this->assert_false($book->errors->is_invalid('name')); 35 } 36 37 public function test_exclusion() 38 { 39 $book = new BookExclusion; 40 $book->name = 'blahh'; 41 $book->save(); 42 $this->assert_false($book->errors->is_invalid('name')); 43 } 44 45 public function test_invalid_inclusion() 46 { 47 $book = new BookInclusion; 48 $book->name = 'thanker'; 49 $book->save(); 50 $this->assert_true($book->errors->is_invalid('name')); 51 $book->name = 'alpha '; 52 $book->save(); 53 $this->assert_true($book->errors->is_invalid('name')); 54 } 55 56 public function test_invalid_exclusion() 57 { 58 $book = new BookExclusion; 59 $book->name = 'alpha'; 60 $book->save(); 61 $this->assert_true($book->errors->is_invalid('name')); 62 63 $book = new BookExclusion; 64 $book->name = 'bravo'; 65 $book->save(); 66 $this->assert_true($book->errors->is_invalid('name')); 67 } 68 69 public function test_inclusion_with_numeric() 70 { 71 BookInclusion::$validates_inclusion_of[0]['in']= array(0, 1, 2); 72 $book = new BookInclusion; 73 $book->name = 2; 74 $book->save(); 75 $this->assert_false($book->errors->is_invalid('name')); 76 } 77 78 public function test_inclusion_with_boolean() 79 { 80 BookInclusion::$validates_inclusion_of[0]['in']= array(true); 81 $book = new BookInclusion; 82 $book->name = true; 83 $book->save(); 84 $this->assert_false($book->errors->is_invalid('name')); 85 } 86 87 public function test_inclusion_with_null() 88 { 89 BookInclusion::$validates_inclusion_of[0]['in']= array(null); 90 $book = new BookInclusion; 91 $book->name = null; 92 $book->save(); 93 $this->assert_false($book->errors->is_invalid('name')); 94 } 95 96 public function test_invalid_inclusion_with_numeric() 97 { 98 BookInclusion::$validates_inclusion_of[0]['in']= array(0, 1, 2); 99 $book = new BookInclusion; 100 $book->name = 5; 101 $book->save(); 102 $this->assert_true($book->errors->is_invalid('name')); 103 } 104 105 public function tes_inclusion_within_option() 106 { 107 BookInclusion::$validates_inclusion_of[0] = array('name', 'within' => array('okay')); 108 $book = new BookInclusion; 109 $book->name = 'okay'; 110 $book->save(); 111 $this->assert_false($book->errors->is_invalid('name')); 112 } 113 114 public function tes_inclusion_scalar_value() 115 { 116 BookInclusion::$validates_inclusion_of[0] = array('name', 'within' => 'okay'); 117 $book = new BookInclusion; 118 $book->name = 'okay'; 119 $book->save(); 120 $this->assert_false($book->errors->is_invalid('name')); 121 } 122 123 public function test_valid_null() 124 { 125 BookInclusion::$validates_inclusion_of[0]['allow_null'] = true; 126 $book = new BookInclusion; 127 $book->name = null; 128 $book->save(); 129 $this->assert_false($book->errors->is_invalid('name')); 130 } 131 132 public function test_valid_blank() 133 { 134 BookInclusion::$validates_inclusion_of[0]['allow_blank'] = true; 135 $book = new BookInclusion; 136 $book->name = ''; 137 $book->save(); 138 $this->assert_false($book->errors->is_invalid('name')); 139 } 140 141 public function test_custom_message() 142 { 143 $msg = 'is using a custom message.'; 144 BookInclusion::$validates_inclusion_of[0]['message'] = $msg; 145 BookExclusion::$validates_exclusion_of[0]['message'] = $msg; 146 147 $book = new BookInclusion; 148 $book->name = 'not included'; 149 $book->save(); 150 $this->assert_equals('is using a custom message.', $book->errors->on('name')); 151 $book = new BookExclusion; 152 $book->name = 'bravo'; 153 $book->save(); 154 $this->assert_equals('is using a custom message.', $book->errors->on('name')); 155 } 156 157}; 158?>