FormTest.php 5.2 KB

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