ValidatesPresenceOfTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. include 'helpers/config.php';
  3. class BookPresence extends ActiveRecord\Model
  4. {
  5. static $table_name = 'books';
  6. static $validates_presence_of = array(
  7. array('name')
  8. );
  9. }
  10. class AuthorPresence extends ActiveRecord\Model
  11. {
  12. static $table_name = 'authors';
  13. static $validates_presence_of = array(
  14. array('some_date')
  15. );
  16. };
  17. class ValidatesPresenceOfTest extends DatabaseTest
  18. {
  19. public function test_presence()
  20. {
  21. $book = new BookPresence(array('name' => 'blah'));
  22. $this->assert_false($book->is_invalid());
  23. }
  24. public function test_presence_on_date_field_is_valid()
  25. {
  26. $author = new AuthorPresence(array('some_date' => '2010-01-01'));
  27. $this->assert_true($author->is_valid());
  28. }
  29. public function test_presence_on_date_field_is_not_valid()
  30. {
  31. $author = new AuthorPresence();
  32. $this->assert_false($author->is_valid());
  33. }
  34. public function test_invalid_null()
  35. {
  36. $book = new BookPresence(array('name' => null));
  37. $this->assert_true($book->is_invalid());
  38. }
  39. public function test_invalid_blank()
  40. {
  41. $book = new BookPresence(array('name' => ''));
  42. $this->assert_true($book->is_invalid());
  43. }
  44. public function test_valid_white_space()
  45. {
  46. $book = new BookPresence(array('name' => ' '));
  47. $this->assert_false($book->is_invalid());
  48. }
  49. public function test_custom_message()
  50. {
  51. BookPresence::$validates_presence_of[0]['message'] = 'is using a custom message.';
  52. $book = new BookPresence(array('name' => null));
  53. $book->is_valid();
  54. $this->assert_equals('is using a custom message.', $book->errors->on('name'));
  55. }
  56. public function test_valid_zero()
  57. {
  58. $book = new BookPresence(array('name' => 0));
  59. $this->assert_true($book->is_valid());
  60. }
  61. };
  62. ?>