ValidatesLengthOfTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. include 'helpers/config.php';
  3. class BookLength extends ActiveRecord\Model
  4. {
  5. static $table = 'books';
  6. static $validates_length_of = array();
  7. }
  8. class BookSize extends ActiveRecord\Model
  9. {
  10. static $table = 'books';
  11. static $validates_size_of = array();
  12. }
  13. class ValidatesLengthOfTest extends DatabaseTest
  14. {
  15. public function set_up($connection_name=null)
  16. {
  17. parent::set_up($connection_name);
  18. BookLength::$validates_length_of[0] = array('name', 'allow_blank' => false, 'allow_null' => false);
  19. }
  20. public function test_within()
  21. {
  22. BookLength::$validates_length_of[0]['within'] = array(1, 5);
  23. $book = new BookLength;
  24. $book->name = '12345';
  25. $book->save();
  26. $this->assert_false($book->errors->is_invalid('name'));
  27. }
  28. public function test_within_error_message()
  29. {
  30. BookLength::$validates_length_of[0]['within'] = array(2,5);
  31. $book = new BookLength();
  32. $book->name = '1';
  33. $book->is_valid();
  34. $this->assert_equals(array('Name is too short (minimum is 2 characters)'),$book->errors->full_messages());
  35. $book->name = '123456';
  36. $book->is_valid();
  37. $this->assert_equals(array('Name is too long (maximum is 5 characters)'),$book->errors->full_messages());
  38. }
  39. public function test_within_custom_error_message()
  40. {
  41. BookLength::$validates_length_of[0]['within'] = array(2,5);
  42. BookLength::$validates_length_of[0]['too_short'] = 'is too short';
  43. BookLength::$validates_length_of[0]['message'] = 'is not between 2 and 5 characters';
  44. $book = new BookLength();
  45. $book->name = '1';
  46. $book->is_valid();
  47. $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
  48. $book->name = '123456';
  49. $book->is_valid();
  50. $this->assert_equals(array('Name is not between 2 and 5 characters'),$book->errors->full_messages());
  51. }
  52. public function test_valid_in()
  53. {
  54. BookLength::$validates_length_of[0]['in'] = array(1, 5);
  55. $book = new BookLength;
  56. $book->name = '12345';
  57. $book->save();
  58. $this->assert_false($book->errors->is_invalid('name'));
  59. }
  60. public function test_aliased_size_of()
  61. {
  62. BookSize::$validates_size_of = BookLength::$validates_length_of;
  63. BookSize::$validates_size_of[0]['within'] = array(1, 5);
  64. $book = new BookSize;
  65. $book->name = '12345';
  66. $book->save();
  67. $this->assert_false($book->errors->is_invalid('name'));
  68. }
  69. public function test_invalid_within_and_in()
  70. {
  71. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  72. $book = new BookLength;
  73. $book->name = 'four';
  74. $book->save();
  75. $this->assert_true($book->errors->is_invalid('name'));
  76. $this->set_up();
  77. BookLength::$validates_length_of[0]['in'] = array(1, 3);
  78. $book = new BookLength;
  79. $book->name = 'four';
  80. $book->save();
  81. $this->assert_true($book->errors->is_invalid('name'));
  82. }
  83. public function test_valid_null()
  84. {
  85. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  86. BookLength::$validates_length_of[0]['allow_null'] = true;
  87. $book = new BookLength;
  88. $book->name = null;
  89. $book->save();
  90. $this->assert_false($book->errors->is_invalid('name'));
  91. }
  92. public function test_valid_blank()
  93. {
  94. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  95. BookLength::$validates_length_of[0]['allow_blank'] = true;
  96. $book = new BookLength;
  97. $book->name = '';
  98. $book->save();
  99. $this->assert_false($book->errors->is_invalid('name'));
  100. }
  101. public function test_invalid_blank()
  102. {
  103. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  104. $book = new BookLength;
  105. $book->name = '';
  106. $book->save();
  107. $this->assert_true($book->errors->is_invalid('name'));
  108. $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
  109. }
  110. public function test_invalid_null_within()
  111. {
  112. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  113. $book = new BookLength;
  114. $book->name = null;
  115. $book->save();
  116. $this->assert_true($book->errors->is_invalid('name'));
  117. $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
  118. }
  119. public function test_invalid_null_minimum()
  120. {
  121. BookLength::$validates_length_of[0]['minimum'] = 1;
  122. $book = new BookLength;
  123. $book->name = null;
  124. $book->save();
  125. $this->assert_true($book->errors->is_invalid('name'));
  126. $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name'));
  127. }
  128. public function test_valid_null_maximum()
  129. {
  130. BookLength::$validates_length_of[0]['maximum'] = 1;
  131. $book = new BookLength;
  132. $book->name = null;
  133. $book->save();
  134. $this->assert_false($book->errors->is_invalid('name'));
  135. }
  136. public function test_float_as_impossible_range_option()
  137. {
  138. BookLength::$validates_length_of[0]['within'] = array(1, 3.6);
  139. $book = new BookLength;
  140. $book->name = '123';
  141. try {
  142. $book->save();
  143. } catch (ActiveRecord\ValidationsArgumentError $e) {
  144. $this->assert_equals('maximum value cannot use a float for length.', $e->getMessage());
  145. }
  146. $this->set_up();
  147. BookLength::$validates_length_of[0]['is'] = 1.8;
  148. $book = new BookLength;
  149. $book->name = '123';
  150. try {
  151. $book->save();
  152. } catch (ActiveRecord\ValidationsArgumentError $e) {
  153. $this->assert_equals('is value cannot use a float for length.', $e->getMessage());
  154. return;
  155. }
  156. $this->fail('An expected exception has not be raised.');
  157. }
  158. public function test_signed_integer_as_impossible_within_option()
  159. {
  160. BookLength::$validates_length_of[0]['within'] = array(-1, 3);
  161. $book = new BookLength;
  162. $book->name = '123';
  163. try {
  164. $book->save();
  165. } catch (ActiveRecord\ValidationsArgumentError $e) {
  166. $this->assert_equals('minimum value cannot use a signed integer.', $e->getMessage());
  167. return;
  168. }
  169. $this->fail('An expected exception has not be raised.');
  170. }
  171. public function test_signed_integer_as_impossible_is_option()
  172. {
  173. BookLength::$validates_length_of[0]['is'] = -8;
  174. $book = new BookLength;
  175. $book->name = '123';
  176. try {
  177. $book->save();
  178. } catch (ActiveRecord\ValidationsArgumentError $e) {
  179. $this->assert_equals('is value cannot use a signed integer.', $e->getMessage());
  180. return;
  181. }
  182. $this->fail('An expected exception has not be raised.');
  183. }
  184. public function test_lack_of_option()
  185. {
  186. try {
  187. $book = new BookLength;
  188. $book->name = null;
  189. $book->save();
  190. } catch (ActiveRecord\ValidationsArgumentError $e) {
  191. $this->assert_equals('Range unspecified. Specify the [within], [maximum], or [is] option.', $e->getMessage());
  192. return;
  193. }
  194. $this->fail('An expected exception has not be raised.');
  195. }
  196. public function test_too_many_options()
  197. {
  198. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  199. BookLength::$validates_length_of[0]['in'] = array(1, 3);
  200. try {
  201. $book = new BookLength;
  202. $book->name = null;
  203. $book->save();
  204. } catch (ActiveRecord\ValidationsArgumentError $e) {
  205. $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
  206. return;
  207. }
  208. $this->fail('An expected exception has not be raised.');
  209. }
  210. public function test_too_many_options_with_different_option_types()
  211. {
  212. BookLength::$validates_length_of[0]['within'] = array(1, 3);
  213. BookLength::$validates_length_of[0]['is'] = 3;
  214. try {
  215. $book = new BookLength;
  216. $book->name = null;
  217. $book->save();
  218. } catch (ActiveRecord\ValidationsArgumentError $e) {
  219. $this->assert_equals('Too many range options specified. Choose only one.', $e->getMessage());
  220. return;
  221. }
  222. $this->fail('An expected exception has not be raised.');
  223. }
  224. /**
  225. * @expectedException ActiveRecord\ValidationsArgumentError
  226. */
  227. public function test_with_option_as_non_numeric()
  228. {
  229. BookLength::$validates_length_of[0]['with'] = array('test');
  230. $book = new BookLength;
  231. $book->name = null;
  232. $book->save();
  233. }
  234. /**
  235. * @expectedException ActiveRecord\ValidationsArgumentError
  236. */
  237. public function test_with_option_as_non_numeric_non_array()
  238. {
  239. BookLength::$validates_length_of[0]['with'] = 'test';
  240. $book = new BookLength;
  241. $book->name = null;
  242. $book->save();
  243. }
  244. public function test_validates_length_of_maximum()
  245. {
  246. BookLength::$validates_length_of[0] = array('name', 'maximum' => 10);
  247. $book = new BookLength(array('name' => '12345678901'));
  248. $book->is_valid();
  249. $this->assert_equals(array("Name is too long (maximum is 10 characters)"),$book->errors->full_messages());
  250. }
  251. public function test_validates_length_of_minimum()
  252. {
  253. BookLength::$validates_length_of[0] = array('name', 'minimum' => 2);
  254. $book = new BookLength(array('name' => '1'));
  255. $book->is_valid();
  256. $this->assert_equals(array("Name is too short (minimum is 2 characters)"),$book->errors->full_messages());
  257. }
  258. public function test_validates_length_of_min_max_custom_message()
  259. {
  260. BookLength::$validates_length_of[0] = array('name', 'maximum' => 10, 'message' => 'is far too long');
  261. $book = new BookLength(array('name' => '12345678901'));
  262. $book->is_valid();
  263. $this->assert_equals(array("Name is far too long"),$book->errors->full_messages());
  264. BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'message' => 'is far too short');
  265. $book = new BookLength(array('name' => '123456789'));
  266. $book->is_valid();
  267. $this->assert_equals(array("Name is far too short"),$book->errors->full_messages());
  268. }
  269. public function test_validates_length_of_min_max_custom_message_overridden()
  270. {
  271. BookLength::$validates_length_of[0] = array('name', 'minimum' => 10, 'too_short' => 'is too short', 'message' => 'is custom message');
  272. $book = new BookLength(array('name' => '123456789'));
  273. $book->is_valid();
  274. $this->assert_equals(array("Name is custom message"),$book->errors->full_messages());
  275. }
  276. public function test_validates_length_of_is()
  277. {
  278. BookLength::$validates_length_of[0] = array('name', 'is' => 2);
  279. $book = new BookLength(array('name' => '123'));
  280. $book->is_valid();
  281. $this->assert_equals(array("Name is the wrong length (should be 2 characters)"),$book->errors->full_messages());
  282. }
  283. };
  284. ?>