123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- include 'helpers/config.php';
- class BookLength extends ActiveRecord\Model
- {
- static $table = 'books';
- static $validates_length_of = array();
- }
- class BookSize extends ActiveRecord\Model
- {
- static $table = 'books';
- static $validates_size_of = array();
- }
- class ValidatesLengthOfTest extends DatabaseTest
- {
- public function set_up($connection_name=null)
- {
- parent::set_up($connection_name);
- BookLength::$validates_length_of[0] = array('name', 'allow_blank' => false, 'allow_null' => false);
- }
-
- public function test_within()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 5);
- $book = new BookLength;
- $book->name = '12345';
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_within_error_message()
- {
- BookLength::$validates_length_of[0]['within'] = array(2,5);
- $book = new BookLength();
- $book->name = '1';
- $book->is_valid();
- $this->assert_equals(array('Name is too short (minimum is 2 characters)'),$book->errors->full_messages());
- $book->name = '123456';
- $book->is_valid();
- $this->assert_equals(array('Name is too long (maximum is 5 characters)'),$book->errors->full_messages());
- }
- public function test_within_custom_error_message()
- {
- BookLength::$validates_length_of[0]['within'] = array(2,5);
- BookLength::$validates_length_of[0]['too_short'] = 'is too short';
- BookLength::$validates_length_of[0]['message'] = 'is not between 2 and 5 characters';
- $book = new BookLength();
- $book->name = '1';
- $book->is_valid();
- $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
- $book->name = '123456';
- $book->is_valid();
- $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
- }
-
- public function test_valid_in()
- {
- BookLength::$validates_length_of[0]['in'] = array(1, 5);
- $book = new BookLength;
- $book->name = '12345';
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_aliased_size_of()
- {
- BookSize::$validates_size_of = BookLength::$validates_length_of;
- BookSize::$validates_size_of[0]['within'] = array(1, 5);
- $book = new BookSize;
- $book->name = '12345';
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_invalid_within_and_in()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- $book = new BookLength;
- $book->name = 'four';
- $book->save();
- $this->assert_true($book->errors->is_invalid('name'));
- $this->set_up();
- BookLength::$validates_length_of[0]['in'] = array(1, 3);
- $book = new BookLength;
- $book->name = 'four';
- $book->save();
- $this->assert_true($book->errors->is_invalid('name'));
- }
- public function test_valid_null()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- BookLength::$validates_length_of[0]['allow_null'] = true;
- $book = new BookLength;
- $book->name = null;
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_valid_blank()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- BookLength::$validates_length_of[0]['allow_blank'] = true;
- $book = new BookLength;
- $book->name = '';
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_invalid_blank()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- $book = new BookLength;
- $book->name = '';
- $book->save();
- $this->assert_true($book->errors->is_invalid('name'));
- $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
- }
- public function test_invalid_null_within()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- $book = new BookLength;
- $book->name = null;
- $book->save();
- $this->assert_true($book->errors->is_invalid('name'));
- $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
- }
-
- public function test_invalid_null_minimum()
- {
- BookLength::$validates_length_of[0]['minimum'] = 1;
- $book = new BookLength;
- $book->name = null;
- $book->save();
- $this->assert_true($book->errors->is_invalid('name'));
- $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
-
- }
-
- public function test_valid_null_maximum()
- {
- BookLength::$validates_length_of[0]['maximum'] = 1;
- $book = new BookLength;
- $book->name = null;
- $book->save();
- $this->assert_false($book->errors->is_invalid('name'));
- }
- public function test_float_as_impossible_range_option()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3.6);
- $book = new BookLength;
- $book->name = '123';
- try {
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('maximum value cannot use a float for length.', $e->getMessage());
- }
- $this->set_up();
- BookLength::$validates_length_of[0]['is'] = 1.8;
- $book = new BookLength;
- $book->name = '123';
- try {
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('is value cannot use a float for length.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- public function test_signed_integer_as_impossible_within_option()
- {
- BookLength::$validates_length_of[0]['within'] = array(-1, 3);
- $book = new BookLength;
- $book->name = '123';
- try {
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('minimum value cannot use a signed integer.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- public function test_signed_integer_as_impossible_is_option()
- {
- BookLength::$validates_length_of[0]['is'] = -8;
- $book = new BookLength;
- $book->name = '123';
- try {
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('is value cannot use a signed integer.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- public function test_lack_of_option()
- {
- try {
- $book = new BookLength;
- $book->name = null;
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('Range unspecified. Specify the [within], [maximum], or [is] option.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- public function test_too_many_options()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- BookLength::$validates_length_of[0]['in'] = array(1, 3);
- try {
- $book = new BookLength;
- $book->name = null;
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- public function test_too_many_options_with_different_option_types()
- {
- BookLength::$validates_length_of[0]['within'] = array(1, 3);
- BookLength::$validates_length_of[0]['is'] = 3;
- try {
- $book = new BookLength;
- $book->name = null;
- $book->save();
- } catch (ActiveRecord\ValidationsArgumentError $e) {
- $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not be raised.');
- }
- /**
- * @expectedException ActiveRecord\ValidationsArgumentError
- */
- public function test_with_option_as_non_numeric()
- {
- BookLength::$validates_length_of[0]['with'] = array('test');
- $book = new BookLength;
- $book->name = null;
- $book->save();
- }
- /**
- * @expectedException ActiveRecord\ValidationsArgumentError
- */
- public function test_with_option_as_non_numeric_non_array()
- {
- BookLength::$validates_length_of[0]['with'] = 'test';
- $book = new BookLength;
- $book->name = null;
- $book->save();
- }
- public function test_validates_length_of_maximum()
- {
- BookLength::$validates_length_of[0] = array('name', 'maximum' => 10);
- $book = new BookLength(array('name' => '12345678901'));
- $book->is_valid();
- $this->assert_equals(array("Name is too long (maximum is 10 characters)"),$book->errors->full_messages());
- }
- public function test_validates_length_of_minimum()
- {
- BookLength::$validates_length_of[0] = array('name', 'minimum' => 2);
- $book = new BookLength(array('name' => '1'));
- $book->is_valid();
- $this->assert_equals(array("Name is too short (minimum is 2 characters)"),$book->errors->full_messages());
- }
-
- public function test_validates_length_of_min_max_custom_message()
- {
- BookLength::$validates_length_of[0] = array('name', 'maximum' => 10, 'message' => 'is far too long');
- $book = new BookLength(array('name' => '12345678901'));
- $book->is_valid();
- $this->assert_equals(array("Name is far too long"),$book->errors->full_messages());
- BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'message' => 'is far too short');
- $book = new BookLength(array('name' => '123456789'));
- $book->is_valid();
- $this->assert_equals(array("Name is far too short"),$book->errors->full_messages());
- }
-
- public function test_validates_length_of_min_max_custom_message_overridden()
- {
- BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'too_short' => 'is too short', 'message' => 'is custom message');
- $book = new BookLength(array('name' => '123456789'));
- $book->is_valid();
- $this->assert_equals(array("Name is custom message"),$book->errors->full_messages());
- }
- public function test_validates_length_of_is()
- {
- BookLength::$validates_length_of[0] = array('name', 'is' => 2);
- $book = new BookLength(array('name' => '123'));
- $book->is_valid();
- $this->assert_equals(array("Name is the wrong length (should be 2 characters)"),$book->errors->full_messages());
- }
- };
- ?>
|