FormTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Form;
  16. use Cake\Form\Form;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Form test case.
  20. */
  21. class FormTest extends TestCase
  22. {
  23. /**
  24. * Test schema()
  25. *
  26. * @return void
  27. */
  28. public function testSchema()
  29. {
  30. $form = new Form();
  31. $schema = $form->schema();
  32. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  33. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  34. $schema = $this->getMockBuilder('Cake\Form\Schema')->getMock();
  35. $this->assertSame($schema, $form->schema($schema));
  36. $this->assertSame($schema, $form->schema());
  37. }
  38. /**
  39. * Test validator()
  40. *
  41. * @return void
  42. * @group deprecated
  43. */
  44. public function testValidator()
  45. {
  46. $this->deprecated(function () {
  47. $form = new Form();
  48. $validator = $form->validator();
  49. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  50. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  51. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  52. $this->assertSame($validator, $form->validator($validator));
  53. $this->assertSame($validator, $form->validator());
  54. });
  55. }
  56. /**
  57. * Test setValidator()
  58. *
  59. * @return void
  60. */
  61. public function testSetValidator()
  62. {
  63. $form = new Form();
  64. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  65. $form->setValidator($validator);
  66. $this->assertSame($validator, $form->getValidator());
  67. }
  68. /**
  69. * Test validate method.
  70. *
  71. * @return void
  72. */
  73. public function testValidate()
  74. {
  75. $form = new Form();
  76. $form->getValidator()
  77. ->add('email', 'format', ['rule' => 'email'])
  78. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  79. $data = [
  80. 'email' => 'rong',
  81. 'body' => 'too short'
  82. ];
  83. $this->assertFalse($form->validate($data));
  84. $this->assertCount(2, $form->errors());
  85. $data = [
  86. 'email' => 'test@example.com',
  87. 'body' => 'Some content goes here'
  88. ];
  89. $this->assertTrue($form->validate($data));
  90. $this->assertCount(0, $form->errors());
  91. }
  92. /**
  93. * Test the errors methods.
  94. *
  95. * @return void
  96. */
  97. public function testErrors()
  98. {
  99. $form = new Form();
  100. $form->getValidator()
  101. ->add('email', 'format', [
  102. 'message' => 'Must be a valid email',
  103. 'rule' => 'email'
  104. ])
  105. ->add('body', 'length', [
  106. 'message' => 'Must be so long',
  107. 'rule' => ['minLength', 12],
  108. ]);
  109. $data = [
  110. 'email' => 'rong',
  111. 'body' => 'too short'
  112. ];
  113. $form->validate($data);
  114. $errors = $form->errors();
  115. $this->assertCount(2, $errors);
  116. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  117. $this->assertEquals('Must be so long', $errors['body']['length']);
  118. }
  119. /**
  120. * Test setErrors()
  121. *
  122. * @return void
  123. */
  124. public function testSetErrors()
  125. {
  126. $form = new Form();
  127. $expected = [
  128. 'field_name' => ['rule_name' => 'message']
  129. ];
  130. $form->setErrors($expected);
  131. $this->assertSame($expected, $form->errors());
  132. }
  133. /**
  134. * Test _execute is skipped on validation failure.
  135. *
  136. * @return void
  137. */
  138. public function testExecuteInvalid()
  139. {
  140. $form = $this->getMockBuilder('Cake\Form\Form')
  141. ->setMethods(['_execute'])
  142. ->getMock();
  143. $form->getValidator()
  144. ->add('email', 'format', ['rule' => 'email']);
  145. $data = [
  146. 'email' => 'rong'
  147. ];
  148. $form->expects($this->never())
  149. ->method('_execute');
  150. $this->assertFalse($form->execute($data));
  151. }
  152. /**
  153. * test execute() when data is valid.
  154. *
  155. * @return void
  156. */
  157. public function testExecuteValid()
  158. {
  159. $form = $this->getMockBuilder('Cake\Form\Form')
  160. ->setMethods(['_execute'])
  161. ->getMock();
  162. $form->getValidator()
  163. ->add('email', 'format', ['rule' => 'email']);
  164. $data = [
  165. 'email' => 'test@example.com'
  166. ];
  167. $form->expects($this->once())
  168. ->method('_execute')
  169. ->with($data)
  170. ->will($this->returnValue(true));
  171. $this->assertTrue($form->execute($data));
  172. }
  173. /**
  174. * test __debugInfo
  175. *
  176. * @return void
  177. */
  178. public function testDebugInfo()
  179. {
  180. $form = new Form();
  181. $result = $form->__debugInfo();
  182. $this->assertArrayHasKey('_schema', $result);
  183. $this->assertArrayHasKey('_errors', $result);
  184. $this->assertArrayHasKey('_validator', $result);
  185. }
  186. }