ValidationSetTest.php 5.3 KB

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