ValidationSetTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * ValidationSetTest file
  4. *
  5. * CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license infValidationation, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  14. * @since 2.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Validation;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\Validation\ValidationRule;
  20. use Cake\Validation\ValidationSet;
  21. use PHPUnit\Framework\Error\Deprecated;
  22. /**
  23. * ValidationSetTest
  24. */
  25. class ValidationSetTest extends TestCase
  26. {
  27. /**
  28. * testGetRule method
  29. *
  30. * @return void
  31. */
  32. public function testGetRule()
  33. {
  34. $field = new ValidationSet();
  35. $field->add('notBlank', ['rule' => 'notBlank', 'message' => 'Can not be empty']);
  36. $result = $field->rule('notBlank');
  37. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result);
  38. $expected = new ValidationRule(['rule' => 'notBlank', 'message' => 'Can not be empty']);
  39. $this->assertEquals($expected, $result);
  40. }
  41. /**
  42. * testGetRules method
  43. *
  44. * @return void
  45. */
  46. public function testGetRules()
  47. {
  48. $field = new ValidationSet();
  49. $field->add('notBlank', ['rule' => 'notBlank', 'message' => 'Can not be empty']);
  50. $result = $field->rules();
  51. $this->assertEquals(['notBlank'], array_keys($result));
  52. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result['notBlank']);
  53. }
  54. /**
  55. * Tests getting a rule from the set using array access
  56. *
  57. * @return void
  58. */
  59. public function testArrayAccessGet()
  60. {
  61. $set = (new ValidationSet())
  62. ->add('notBlank', ['rule' => 'notBlank'])
  63. ->add('numeric', ['rule' => 'numeric'])
  64. ->add('other', ['rule' => 'email']);
  65. $rule = $set['notBlank'];
  66. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  67. $this->assertEquals(new ValidationRule(['rule' => 'notBlank']), $rule);
  68. $rule = $set['numeric'];
  69. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  70. $this->assertEquals(new ValidationRule(['rule' => 'numeric']), $rule);
  71. $rule = $set['other'];
  72. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  73. $this->assertEquals(new ValidationRule(['rule' => 'email']), $rule);
  74. }
  75. /**
  76. * Tests checking a rule from the set using array access
  77. *
  78. * @return void
  79. */
  80. public function testArrayAccessExists()
  81. {
  82. $set = (new ValidationSet())
  83. ->add('notBlank', ['rule' => 'notBlank'])
  84. ->add('numeric', ['rule' => 'numeric'])
  85. ->add('other', ['rule' => 'email']);
  86. $this->assertArrayHasKey('notBlank', $set);
  87. $this->assertArrayHasKey('numeric', $set);
  88. $this->assertArrayHasKey('other', $set);
  89. $this->assertArrayNotHasKey('fail', $set);
  90. }
  91. /**
  92. * Tests setting a rule in the set using array access
  93. *
  94. * @return void
  95. */
  96. public function testArrayAccessSet()
  97. {
  98. $set = (new ValidationSet())
  99. ->add('notBlank', ['rule' => 'notBlank']);
  100. $this->assertArrayNotHasKey('other', $set);
  101. $set['other'] = ['rule' => 'email'];
  102. $rule = $set['other'];
  103. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  104. $this->assertEquals(new ValidationRule(['rule' => 'email']), $rule);
  105. }
  106. /**
  107. * Tests unseting a rule from the set using array access
  108. *
  109. * @return void
  110. */
  111. public function testArrayAccessUnset()
  112. {
  113. $set = (new ValidationSet())
  114. ->add('notBlank', ['rule' => 'notBlank'])
  115. ->add('numeric', ['rule' => 'numeric'])
  116. ->add('other', ['rule' => 'email']);
  117. unset($set['notBlank']);
  118. $this->assertArrayNotHasKey('notBlank', $set);
  119. unset($set['numeric']);
  120. $this->assertArrayNotHasKey('numeric', $set);
  121. unset($set['other']);
  122. $this->assertArrayNotHasKey('other', $set);
  123. }
  124. /**
  125. * Tests it is possible to iterate a validation set object
  126. *
  127. * @return void
  128. */
  129. public function testIterator()
  130. {
  131. $set = (new ValidationSet())
  132. ->add('notBlank', ['rule' => 'notBlank'])
  133. ->add('numeric', ['rule' => 'numeric'])
  134. ->add('other', ['rule' => 'email']);
  135. $i = 0;
  136. foreach ($set as $name => $rule) {
  137. if ($i === 0) {
  138. $this->assertEquals('notBlank', $name);
  139. }
  140. if ($i === 1) {
  141. $this->assertEquals('numeric', $name);
  142. }
  143. if ($i === 2) {
  144. $this->assertEquals('other', $name);
  145. }
  146. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  147. $i++;
  148. }
  149. $this->assertEquals(3, $i);
  150. }
  151. /**
  152. * Tests countable interface
  153. *
  154. * @return void
  155. */
  156. public function testCount()
  157. {
  158. $set = (new ValidationSet())
  159. ->add('notBlank', ['rule' => 'notBlank'])
  160. ->add('numeric', ['rule' => 'numeric'])
  161. ->add('other', ['rule' => 'email']);
  162. $this->assertCount(3, $set);
  163. unset($set['other']);
  164. $this->assertCount(2, $set);
  165. }
  166. /**
  167. * Test removeRule method
  168. *
  169. * @return void
  170. */
  171. public function testRemoveRule()
  172. {
  173. $set = (new ValidationSet())
  174. ->add('notBlank', ['rule' => 'notBlank'])
  175. ->add('numeric', ['rule' => 'numeric'])
  176. ->add('other', ['rule' => 'email']);
  177. $this->assertArrayHasKey('notBlank', $set);
  178. $set->remove('notBlank');
  179. $this->assertArrayNotHasKey('notBlank', $set);
  180. $this->assertArrayHasKey('numeric', $set);
  181. $set->remove('numeric');
  182. $this->assertArrayNotHasKey('numeric', $set);
  183. $this->assertArrayHasKey('other', $set);
  184. $set->remove('other');
  185. $this->assertArrayNotHasKey('other', $set);
  186. }
  187. /**
  188. * Test requirePresence and isPresenceRequired methods
  189. *
  190. * @return void
  191. */
  192. public function testRequirePresence()
  193. {
  194. $set = new ValidationSet();
  195. $this->assertFalse($set->isPresenceRequired());
  196. $set->requirePresence(true);
  197. $this->assertTrue($set->isPresenceRequired());
  198. $set->requirePresence(false);
  199. $this->assertFalse($set->isPresenceRequired());
  200. }
  201. /**
  202. * Test isPresenceRequired deprecated setter
  203. *
  204. * @group deprecated
  205. * @return void
  206. */
  207. public function testRequirePresenceDeprecated()
  208. {
  209. $this->deprecated(function () {
  210. $set = new ValidationSet();
  211. $this->assertFalse($set->isPresenceRequired());
  212. $set->isPresenceRequired(true);
  213. $this->assertTrue($set->isPresenceRequired());
  214. $set->isPresenceRequired(false);
  215. $this->assertFalse($set->isPresenceRequired());
  216. });
  217. }
  218. /**
  219. * Test isPresenceRequired method deprecation
  220. *
  221. * @group deprecated
  222. * @return void
  223. */
  224. public function testIsPresenceRequiredDeprecation()
  225. {
  226. $this->expectException(Deprecated::class);
  227. $this->expectExceptionMessage('ValidationSet::isPresenceRequired() is deprecated as a setter. Use ValidationSet::requirePresence() instead.');
  228. $set = new ValidationSet();
  229. $set->isPresenceRequired(true);
  230. }
  231. /**
  232. * Test allowEmpty and isEmptyAllowed methods
  233. *
  234. * @return void
  235. */
  236. public function testAllowEmpty()
  237. {
  238. $set = new ValidationSet();
  239. $this->assertFalse($set->isEmptyAllowed());
  240. $set->allowEmpty(true);
  241. $this->assertTrue($set->isEmptyAllowed());
  242. $set->allowEmpty(false);
  243. $this->assertFalse($set->isEmptyAllowed());
  244. }
  245. /**
  246. * Test isEmptyAllowed deprecated setter
  247. *
  248. * @group deprecated
  249. * @return void
  250. */
  251. public function testAllowEmptyDeprecated()
  252. {
  253. $this->deprecated(function () {
  254. $set = new ValidationSet();
  255. $this->assertFalse($set->isEmptyAllowed());
  256. $set->isEmptyAllowed(true);
  257. $this->assertTrue($set->isEmptyAllowed());
  258. $set->isEmptyAllowed(false);
  259. $this->assertFalse($set->isEmptyAllowed());
  260. });
  261. }
  262. /**
  263. * Test isEmptyAllowed method deprecation
  264. *
  265. * @group deprecated
  266. * @return void
  267. */
  268. public function testIsEmptyAllowedDeprecation()
  269. {
  270. $this->expectException(Deprecated::class);
  271. $this->expectExceptionMessage('ValidationSet::isEmptyAllowed() is deprecated as a setter. Use ValidationSet::allowEmpty() instead.');
  272. $set = new ValidationSet();
  273. $set->isEmptyAllowed(true);
  274. }
  275. }