ValidationSetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * ValidationSetTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  14. * @since 2.2.0
  15. * @license http://www.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. */
  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('notEmpty', ['rule' => 'notEmpty', 'message' => 'Can not be empty']);
  36. $result = $field->rule('notEmpty');
  37. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result);
  38. $expected = new ValidationRule(['rule' => 'notEmpty', '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('notEmpty', ['rule' => 'notEmpty', 'message' => 'Can not be empty']);
  50. $result = $field->rules();
  51. $this->assertEquals(['notEmpty'], array_keys($result));
  52. $this->assertInstanceOf('Cake\Validation\ValidationRule', $result['notEmpty']);
  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('notEmpty', ['rule' => 'notEmpty'])
  63. ->add('numeric', ['rule' => 'numeric'])
  64. ->add('other', ['rule' => 'email']);
  65. $rule = $set['notEmpty'];
  66. $this->assertInstanceOf('Cake\Validation\ValidationRule', $rule);
  67. $this->assertEquals(new ValidationRule(['rule' => 'notEmpty']), $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('notEmpty', ['rule' => 'notEmpty'])
  84. ->add('numeric', ['rule' => 'numeric'])
  85. ->add('other', ['rule' => 'email']);
  86. $this->assertTrue(isset($set['notEmpty']));
  87. $this->assertTrue(isset($set['numeric']));
  88. $this->assertTrue(isset($set['other']));
  89. $this->assertFalse(isset($set['fail']));
  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('notEmpty', ['rule' => 'notEmpty']);
  100. $this->assertFalse(isset($set['other']));
  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('notEmpty', ['rule' => 'notEmpty'])
  115. ->add('numeric', ['rule' => 'numeric'])
  116. ->add('other', ['rule' => 'email']);
  117. unset($set['notEmpty']);
  118. $this->assertFalse(isset($set['notEmpty']));
  119. unset($set['numeric']);
  120. $this->assertFalse(isset($set['numeric']));
  121. unset($set['other']);
  122. $this->assertFalse(isset($set['other']));
  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('notEmpty', ['rule' => 'notEmpty'])
  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('notEmpty', $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('notEmpty', ['rule' => 'notEmpty'])
  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('title', [
  174. '_validatePresent' => true,
  175. 'notEmpty' => ['rule' => 'notEmpty'],
  176. 'numeric' => ['rule' => 'numeric'],
  177. 'other' => ['rule' => ['other', 1]],
  178. ]);
  179. $set->remove('notEmpty');
  180. $this->assertFalse(isset($set['notEmpty']));
  181. $set->remove('numeric');
  182. $this->assertFalse(isset($set['numeric']));
  183. $set->remove('other');
  184. $this->assertFalse(isset($set['other']));
  185. }
  186. }