ValidatesFormatOfTest.php 3.0 KB

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