public function Test_of_validatesLengthOf() { $Person = new TestPerson(); $Person->city = 'Vilanova i la Geltrí'; $Person->validatesLengthOf("city", array("maximum" => 5, 'message' => "less than %d if you don't mind")); $this->assertEqual($Person->getErrorsOn('city'), "less than 5 if you don't mind"); $Person->clearErrors(); $Person->city = 'Carlet'; $Person->validatesLengthOf("city", array("maximum" => 10)); $this->assertFalse($Person->getErrorsOn('city')); $Person->clearErrors(); $Person->city = ''; $Person->validatesLengthOf("city", array("maximum" => 10, 'allow_null' => true, 'message' => "less than %d if you don't mind")); $this->assertFalse($Person->getErrorsOn('city')); $Person->clearErrors(); $Person->score = 101; $Person->validatesLengthOf("score", array("within" => array(1, 100))); $this->assertEqual($Person->getErrorsOn('score'), sprintf($Person->getDefaultErrorMessageFor('too_long'), 100)); $Person->clearErrors(); $Person->score = -5; $Person->validatesLengthOf("score", array("within" => array(1, 100))); $this->assertEqual($Person->getErrorsOn('score'), sprintf($Person->getDefaultErrorMessageFor('too_short'), 1)); $Person->clearErrors(); $Person->score = 25; $Person->validatesLengthOf("score", array("within" => array(1, 100))); $this->assertFalse($Person->getErrorsOn('score')); $Person->clearErrors(); $Person->state = 'CA'; $Person->validatesLengthOf("state", array("in" => array(5, 20), "too_long" => "pick a shorter name", "too_short" => "pick a longer name")); $this->assertEqual($Person->getErrorsOn('state'), "pick a longer name"); $Person->clearErrors(); $Person->state = 'Barcelona'; $Person->validatesLengthOf("state", array("in" => array(2, 5), "too_long" => "pick a shorter name", "too_short" => "pick a longer name")); $this->assertEqual($Person->getErrorsOn('state'), "pick a shorter name"); $Person->clearErrors(); $Person->state = 'Valencia'; $Person->validatesLengthOf("state", array("in" => array(5, 20), "too_long" => "pick a shorter name", "too_short" => "pick a longer name")); $this->assertFalse($Person->getErrorsOn('state')); $Person->clearErrors(); $Person->subscriptions = array(); $Person->validatesLengthOf("subscriptions", array("minimum" => 4, "too_short" => "you need to select at least 4 subscriptions")); $this->assertEqual($Person->getErrorsOn('subscriptions'), "you need to select at least 4 subscriptions"); $Person->clearErrors(); $Person->subscriptions = array('php architect'); $Person->validatesLengthOf("subscriptions", array("minimum" => 4, "too_short" => "you need to select at least 4 subscriptions")); $this->assertEqual($Person->getErrorsOn('subscriptions'), "you need to select at least 4 subscriptions"); $Person->clearErrors(); $Person->subscriptions = array('php architect', 'computer world', 'wired', 'slashdot'); $Person->validatesLengthOf("subscriptions", array("minimum" => 4, "too_short" => "you need to select at least 4 subscriptions")); $this->assertFalse($Person->getErrorsOn('subscriptions')); $Person->clearErrors(); $Person->validatesLengthOf("country", array("is" => 2, "message" => "must be %d characters long as specified on ISO 3166")); $this->assertEqual($Person->getErrorsOn('country'), "must be 2 characters long as specified on ISO 3166"); $Person->clearErrors(); $Person->country = ''; $Person->validatesLengthOf("country", array("is" => 2, "message" => "must be %d characters long as specified on ISO 3166")); $this->assertEqual($Person->getErrorsOn('country'), "must be 2 characters long as specified on ISO 3166"); $Person->clearErrors(); $Person->country = 2; $Person->validatesLengthOf("country", array("is" => 2, "message" => "must be %d characters long as specified on ISO 3166")); $this->assertFalse($Person->getErrorsOn('country')); $Person->clearErrors(); $Person->country = 'ES'; $Person->validatesLengthOf("country", array("is" => 2, "message" => "must be %d characters long as specified on ISO 3166")); $this->assertFalse($Person->getErrorsOn('country')); }