ValidationSetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  22. * ValidationSetTest
  23. */
  24. class ValidationSetTest extends TestCase
  25. {
  26. /**
  27. * testGetRule method
  28. *
  29. * @return void
  30. */
  31. public function testGetRule()
  32. {
  33. $field = new ValidationSet;
  34. $field->add('notBlank', ['rule' => 'notBlank', 'message' => 'Can not be empty']);
  35. $result = $field->rule('notBlank');
  36. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result);
  37. $expected = new ValidationRule(['rule' => 'notBlank', 'message' => 'Can not be empty']);
  38. $this->assertEquals($expected, $result);
  39. }
  40. /**
  41. * testGetRules method
  42. *
  43. * @return void
  44. */
  45. public function testGetRules()
  46. {
  47. $field = new ValidationSet;
  48. $field->add('notBlank', ['rule' => 'notBlank', 'message' => 'Can not be empty']);
  49. $result = $field->rules();
  50. $this->assertEquals(['notBlank'], array_keys($result));
  51. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result['notBlank']);
  52. }
  53. /**
  54. * Tests getting a rule from the set using array access
  55. *
  56. * @return void
  57. */
  58. public function testArrayAccessGet()
  59. {
  60. $set = (new ValidationSet)
  61. ->add('notBlank', ['rule' => 'notBlank'])
  62. ->add('numeric', ['rule' => 'numeric'])
  63. ->add('other', ['rule' => 'email']);
  64. $rule = $set['notBlank'];
  65. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  66. $this->assertEquals(new ValidationRule(['rule' => 'notBlank']), $rule);
  67. $rule = $set['numeric'];
  68. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  69. $this->assertEquals(new ValidationRule(['rule' => 'numeric']), $rule);
  70. $rule = $set['other'];
  71. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  72. $this->assertEquals(new ValidationRule(['rule' => 'email']), $rule);
  73. }
  74. /**
  75. * Tests checking a rule from the set using array access
  76. *
  77. * @return void
  78. */
  79. public function testArrayAccessExists()
  80. {
  81. $set = (new ValidationSet)
  82. ->add('notBlank', ['rule' => 'notBlank'])
  83. ->add('numeric', ['rule' => 'numeric'])
  84. ->add('other', ['rule' => 'email']);
  85. $this->assertTrue(isset($set['notBlank']));
  86. $this->assertTrue(isset($set['numeric']));
  87. $this->assertTrue(isset($set['other']));
  88. $this->assertFalse(isset($set['fail']));
  89. }
  90. /**
  91. * Tests setting a rule in the set using array access
  92. *
  93. * @return void
  94. */
  95. public function testArrayAccessSet()
  96. {
  97. $set = (new ValidationSet)
  98. ->add('notBlank', ['rule' => 'notBlank']);
  99. $this->assertFalse(isset($set['other']));
  100. $set['other'] = ['rule' => 'email'];
  101. $rule = $set['other'];
  102. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  103. $this->assertEquals(new ValidationRule(['rule' => 'email']), $rule);
  104. }
  105. /**
  106. * Tests unseting a rule from the set using array access
  107. *
  108. * @return void
  109. */
  110. public function testArrayAccessUnset()
  111. {
  112. $set = (new ValidationSet)
  113. ->add('notBlank', ['rule' => 'notBlank'])
  114. ->add('numeric', ['rule' => 'numeric'])
  115. ->add('other', ['rule' => 'email']);
  116. unset($set['notBlank']);
  117. $this->assertFalse(isset($set['notBlank']));
  118. unset($set['numeric']);
  119. $this->assertFalse(isset($set['numeric']));
  120. unset($set['other']);
  121. $this->assertFalse(isset($set['other']));
  122. }
  123. /**
  124. * Tests it is possible to iterate a validation set object
  125. *
  126. * @return void
  127. */
  128. public function testIterator()
  129. {
  130. $set = (new ValidationSet)
  131. ->add('notBlank', ['rule' => 'notBlank'])
  132. ->add('numeric', ['rule' => 'numeric'])
  133. ->add('other', ['rule' => 'email']);
  134. $i = 0;
  135. foreach ($set as $name => $rule) {
  136. if ($i === 0) {
  137. $this->assertEquals('notBlank', $name);
  138. }
  139. if ($i === 1) {
  140. $this->assertEquals('numeric', $name);
  141. }
  142. if ($i === 2) {
  143. $this->assertEquals('other', $name);
  144. }
  145. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  146. $i++;
  147. }
  148. $this->assertEquals(3, $i);
  149. }
  150. /**
  151. * Tests countable interface
  152. *
  153. * @return void
  154. */
  155. public function testCount()
  156. {
  157. $set = (new ValidationSet)
  158. ->add('notBlank', ['rule' => 'notBlank'])
  159. ->add('numeric', ['rule' => 'numeric'])
  160. ->add('other', ['rule' => 'email']);
  161. $this->assertCount(3, $set);
  162. unset($set['other']);
  163. $this->assertCount(2, $set);
  164. }
  165. /**
  166. * Test removeRule method
  167. *
  168. * @return void
  169. */
  170. public function testRemoveRule()
  171. {
  172. $set = new ValidationSet('title', [
  173. '_validatePresent' => true,
  174. 'notBlank' => ['rule' => 'notBlank'],
  175. 'numeric' => ['rule' => 'numeric'],
  176. 'other' => ['rule' => ['other', 1]],
  177. ]);
  178. $set->remove('notBlank');
  179. $this->assertFalse(isset($set['notBlank']));
  180. $set->remove('numeric');
  181. $this->assertFalse(isset($set['numeric']));
  182. $set->remove('other');
  183. $this->assertFalse(isset($set['other']));
  184. }
  185. }