FormTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. use Cake\Validation\Validator;
  19. use TestApp\Form\AppForm;
  20. use TestApp\Form\FormSchema;
  21. /**
  22. * Form test case.
  23. */
  24. class FormTest extends TestCase
  25. {
  26. /**
  27. * Test schema()
  28. *
  29. * @return void
  30. */
  31. public function testSchema()
  32. {
  33. $form = new Form();
  34. $schema = $form->schema();
  35. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  36. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  37. $schema = $this->getMockBuilder('Cake\Form\Schema')->getMock();
  38. $this->assertSame($schema, $form->schema($schema));
  39. $this->assertSame($schema, $form->schema());
  40. $form = new AppForm();
  41. $this->assertInstanceOf(FormSchema::class, $form->schema());
  42. }
  43. /**
  44. * Test validator()
  45. *
  46. * @return void
  47. * @group deprecated
  48. */
  49. public function testValidator()
  50. {
  51. $this->deprecated(function () {
  52. $form = new Form();
  53. $validator = $form->validator();
  54. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  55. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  56. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  57. $this->assertSame($validator, $form->validator($validator));
  58. $this->assertSame($validator, $form->validator());
  59. });
  60. }
  61. /**
  62. * Test getValidator()
  63. *
  64. * @return void
  65. */
  66. public function testGetValidator()
  67. {
  68. $form = $this->getMockBuilder(Form::class)
  69. ->setMethods(['buildValidator'])
  70. ->getMock();
  71. $form->expects($this->once())
  72. ->method('buildValidator');
  73. $this->assertInstanceof(Validator::class, $form->getValidator());
  74. }
  75. /**
  76. * Test setValidator()
  77. *
  78. * @return void
  79. */
  80. public function testSetValidator()
  81. {
  82. $form = new Form();
  83. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  84. $form->setValidator('default', $validator);
  85. $this->assertSame($validator, $form->getValidator());
  86. }
  87. /**
  88. * Test validate method.
  89. *
  90. * @return void
  91. */
  92. public function testValidate()
  93. {
  94. $form = new Form();
  95. $form->getValidator()
  96. ->add('email', 'format', ['rule' => 'email'])
  97. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  98. $data = [
  99. 'email' => 'rong',
  100. 'body' => 'too short'
  101. ];
  102. $this->assertFalse($form->validate($data));
  103. $this->assertCount(2, $form->errors());
  104. $data = [
  105. 'email' => 'test@example.com',
  106. 'body' => 'Some content goes here'
  107. ];
  108. $this->assertTrue($form->validate($data));
  109. $this->assertCount(0, $form->errors());
  110. }
  111. /**
  112. * Test the errors methods.
  113. *
  114. * @return void
  115. */
  116. public function testErrors()
  117. {
  118. $form = new Form();
  119. $form->getValidator()
  120. ->add('email', 'format', [
  121. 'message' => 'Must be a valid email',
  122. 'rule' => 'email'
  123. ])
  124. ->add('body', 'length', [
  125. 'message' => 'Must be so long',
  126. 'rule' => ['minLength', 12],
  127. ]);
  128. $data = [
  129. 'email' => 'rong',
  130. 'body' => 'too short'
  131. ];
  132. $form->validate($data);
  133. $errors = $form->errors();
  134. $this->assertCount(2, $errors);
  135. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  136. $this->assertEquals('Must be so long', $errors['body']['length']);
  137. }
  138. /**
  139. * Test setErrors()
  140. *
  141. * @return void
  142. */
  143. public function testSetErrors()
  144. {
  145. $form = new Form();
  146. $expected = [
  147. 'field_name' => ['rule_name' => 'message']
  148. ];
  149. $form->setErrors($expected);
  150. $this->assertSame($expected, $form->errors());
  151. }
  152. /**
  153. * Test _execute is skipped on validation failure.
  154. *
  155. * @return void
  156. */
  157. public function testExecuteInvalid()
  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' => 'rong'
  166. ];
  167. $form->expects($this->never())
  168. ->method('_execute');
  169. $this->assertFalse($form->execute($data));
  170. }
  171. /**
  172. * test execute() when data is valid.
  173. *
  174. * @return void
  175. */
  176. public function testExecuteValid()
  177. {
  178. $form = $this->getMockBuilder('Cake\Form\Form')
  179. ->setMethods(['_execute'])
  180. ->getMock();
  181. $form->getValidator()
  182. ->add('email', 'format', ['rule' => 'email']);
  183. $data = [
  184. 'email' => 'test@example.com'
  185. ];
  186. $form->expects($this->once())
  187. ->method('_execute')
  188. ->with($data)
  189. ->will($this->returnValue(true));
  190. $this->assertTrue($form->execute($data));
  191. }
  192. /**
  193. * test __debugInfo
  194. *
  195. * @return void
  196. */
  197. public function testDebugInfo()
  198. {
  199. $form = new Form();
  200. $result = $form->__debugInfo();
  201. $this->assertArrayHasKey('_schema', $result);
  202. $this->assertArrayHasKey('_errors', $result);
  203. $this->assertArrayHasKey('_validator', $result);
  204. }
  205. }