a tiny mvc framework for php using php-activerecord
at v1 112 lines 3.1 kB view raw
1<?php 2include 'helpers/config.php'; 3 4class BookFormat extends ActiveRecord\Model 5{ 6 static $table = 'books'; 7 static $validates_format_of = array( 8 array('name') 9 ); 10}; 11 12class ValidatesFormatOfTest extends DatabaseTest 13{ 14 public function set_up($connection_name=null) 15 { 16 parent::set_up($connection_name); 17 BookFormat::$validates_format_of[0] = array('name'); 18 } 19 20 public function test_format() 21 { 22 BookFormat::$validates_format_of[0]['with'] = '/^[a-z\W]*$/'; 23 $book = new BookFormat(array('author_id' => 1, 'name' => 'testing reg')); 24 $book->save(); 25 $this->assert_false($book->errors->is_invalid('name')); 26 27 BookFormat::$validates_format_of[0]['with'] = '/[0-9]/'; 28 $book = new BookFormat(array('author_id' => 1, 'name' => 12)); 29 $book->save(); 30 $this->assert_false($book->errors->is_invalid('name')); 31 } 32 33 public function test_invalid_null() 34 { 35 BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/'; 36 $book = new BookFormat; 37 $book->name = null; 38 $book->save(); 39 $this->assert_true($book->errors->is_invalid('name')); 40 } 41 42 public function test_invalid_blank() 43 { 44 BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/'; 45 $book = new BookFormat; 46 $book->name = ''; 47 $book->save(); 48 $this->assert_true($book->errors->is_invalid('name')); 49 } 50 51 public function test_valid_blank_andallow_blank() 52 { 53 BookFormat::$validates_format_of[0]['allow_blank'] = true; 54 BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/'; 55 $book = new BookFormat(array('author_id' => 1, 'name' => '')); 56 $book->save(); 57 $this->assert_false($book->errors->is_invalid('name')); 58 } 59 60 public function test_valid_null_and_allow_null() 61 { 62 BookFormat::$validates_format_of[0]['allow_null'] = true; 63 BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/'; 64 $book = new BookFormat(); 65 $book->author_id = 1; 66 $book->name = null; 67 $book->save(); 68 $this->assert_false($book->errors->is_invalid('name')); 69 } 70 71 /** 72 * @expectedException ActiveRecord\ValidationsArgumentError 73 */ 74 public function test_invalid_lack_of_with_key() 75 { 76 $book = new BookFormat; 77 $book->name = null; 78 $book->save(); 79 } 80 81 /** 82 * @expectedException ActiveRecord\ValidationsArgumentError 83 */ 84 public function test_invalid_with_expression_as_non_string() 85 { 86 BookFormat::$validates_format_of[0]['with'] = array('test'); 87 $book = new BookFormat; 88 $book->name = null; 89 $book->save(); 90 } 91 92 public function test_invalid_with_expression_as_non_regexp() 93 { 94 BookFormat::$validates_format_of[0]['with'] = 'blah'; 95 $book = new BookFormat; 96 $book->name = 'blah'; 97 $book->save(); 98 $this->assert_true($book->errors->is_invalid('name')); 99 } 100 101 public function test_custom_message() 102 { 103 BookFormat::$validates_format_of[0]['message'] = 'is using a custom message.'; 104 BookFormat::$validates_format_of[0]['with'] = '/[^0-9]/'; 105 106 $book = new BookFormat; 107 $book->name = null; 108 $book->save(); 109 $this->assert_equals('is using a custom message.', $book->errors->on('name')); 110 } 111}; 112?>