a tiny mvc framework for php using php-activerecord
at v1 166 lines 5.1 kB view raw
1<?php 2include 'helpers/config.php'; 3 4use ActiveRecord as AR; 5 6class BookValidations extends ActiveRecord\Model 7{ 8 static $table_name = 'books'; 9 static $alias_attribute = array('name_alias' => 'name', 'x' => 'secondary_author_id'); 10 static $validates_presence_of = array(); 11 static $validates_uniqueness_of = array(); 12 static $custom_validator_error_msg = 'failed custom validation'; 13 14 // fired for every validation - but only used for custom validation test 15 public function validate() 16 { 17 if ($this->name == 'test_custom_validation') 18 $this->errors->add('name', self::$custom_validator_error_msg); 19 } 20} 21 22class ValidationsTest extends DatabaseTest 23{ 24 public function set_up($connection_name=null) 25 { 26 parent::set_up($connection_name); 27 28 BookValidations::$validates_presence_of[0] = 'name'; 29 BookValidations::$validates_uniqueness_of[0] = 'name'; 30 } 31 32 public function test_is_valid_invokes_validations() 33 { 34 $book = new Book; 35 $this->assert_true(empty($book->errors)); 36 $book->is_valid(); 37 $this->assert_false(empty($book->errors)); 38 } 39 40 public function test_is_valid_returns_true_if_no_validations_exist() 41 { 42 $book = new Book; 43 $this->assert_true($book->is_valid()); 44 } 45 46 public function test_is_valid_returns_false_if_failed_validations() 47 { 48 $book = new BookValidations; 49 $this->assert_false($book->is_valid()); 50 } 51 52 public function test_is_invalid() 53 { 54 $book = new Book(); 55 $this->assert_false($book->is_invalid()); 56 } 57 58 public function test_is_invalid_is_true() 59 { 60 $book = new BookValidations(); 61 $this->assert_true($book->is_invalid()); 62 } 63 64 public function test_is_iterable() 65 { 66 $book = new BookValidations(); 67 $book->is_valid(); 68 69 foreach ($book->errors as $name => $message) 70 $this->assert_equals("Name can't be blank",$message); 71 } 72 73 public function test_full_messages() 74 { 75 $book = new BookValidations(); 76 $book->is_valid(); 77 78 $this->assert_equals(array("Name can't be blank"),array_values($book->errors->full_messages(array('hash' => true)))); 79 } 80 81 public function test_to_array() 82 { 83 $book = new BookValidations(); 84 $book->is_valid(); 85 86 $this->assert_equals(array("name" => array("Name can't be blank")), $book->errors->to_array()); 87 } 88 89 public function test_toString() 90 { 91 $book = new BookValidations(); 92 $book->is_valid(); 93 $book->errors->add('secondary_author_id', "is invalid"); 94 95 $this->assert_equals("Name can't be blank\nSecondary author id is invalid", (string) $book->errors); 96 } 97 98 public function test_validates_uniqueness_of() 99 { 100 BookValidations::create(array('name' => 'bob')); 101 $book = BookValidations::create(array('name' => 'bob')); 102 103 $this->assert_equals(array("Name must be unique"),$book->errors->full_messages()); 104 $this->assert_equals(1,BookValidations::count(array('conditions' => "name='bob'"))); 105 } 106 107 public function test_validates_uniqueness_of_excludes_self() 108 { 109 $book = BookValidations::first(); 110 $this->assert_equals(true,$book->is_valid()); 111 } 112 113 public function test_validates_uniqueness_of_with_multiple_fields() 114 { 115 BookValidations::$validates_uniqueness_of[0] = array(array('name','special')); 116 $book1 = BookValidations::first(); 117 $book2 = new BookValidations(array('name' => $book1->name, 'special' => $book1->special+1)); 118 $this->assert_true($book2->is_valid()); 119 } 120 121 public function test_validates_uniqueness_of_with_multiple_fields_is_not_unique() 122 { 123 BookValidations::$validates_uniqueness_of[0] = array(array('name','special')); 124 $book1 = BookValidations::first(); 125 $book2 = new BookValidations(array('name' => $book1->name, 'special' => $book1->special)); 126 $this->assert_false($book2->is_valid()); 127 $this->assert_equals(array('Name and special must be unique'),$book2->errors->full_messages()); 128 } 129 130 public function test_validates_uniqueness_of_works_with_alias_attribute() 131 { 132 BookValidations::$validates_uniqueness_of[0] = array(array('name_alias','x')); 133 $book = BookValidations::create(array('name_alias' => 'Another Book', 'x' => 2)); 134 $this->assert_false($book->is_valid()); 135 $this->assert_equals(array('Name alias and x must be unique'), $book->errors->full_messages()); 136 } 137 138 public function test_get_validation_rules() 139 { 140 $validators = BookValidations::first()->get_validation_rules(); 141 $this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name'])); 142 } 143 144 public function test_model_is_nulled_out_to_prevent_memory_leak() 145 { 146 $book = new BookValidations(); 147 $book->is_valid(); 148 $this->assert_true(strpos(serialize($book->errors),'model";N;') !== false); 149 } 150 151 public function test_validations_takes_strings() 152 { 153 BookValidations::$validates_presence_of = array('numeric_test', array('special'), 'name'); 154 $book = new BookValidations(array('numeric_test' => 1, 'special' => 1)); 155 $this->assert_false($book->is_valid()); 156 } 157 158 public function test_gh131_custom_validation() 159 { 160 $book = new BookValidations(array('name' => 'test_custom_validation')); 161 $book->save(); 162 $this->assert_true($book->errors->is_invalid('name')); 163 $this->assert_equals(BookValidations::$custom_validator_error_msg, $book->errors->on('name')); 164 } 165}; 166?>