FormTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * @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 validate method.
  58. *
  59. * @return void
  60. */
  61. public function testValidate()
  62. {
  63. $form = new Form();
  64. $form->getValidator()
  65. ->add('email', 'format', ['rule' => 'email'])
  66. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  67. $data = [
  68. 'email' => 'rong',
  69. 'body' => 'too short'
  70. ];
  71. $this->assertFalse($form->validate($data));
  72. $this->assertCount(2, $form->errors());
  73. $data = [
  74. 'email' => 'test@example.com',
  75. 'body' => 'Some content goes here'
  76. ];
  77. $this->assertTrue($form->validate($data));
  78. $this->assertCount(0, $form->errors());
  79. }
  80. /**
  81. * Test the errors methods.
  82. *
  83. * @return void
  84. */
  85. public function testErrors()
  86. {
  87. $form = new Form();
  88. $form->getValidator()
  89. ->add('email', 'format', [
  90. 'message' => 'Must be a valid email',
  91. 'rule' => 'email'
  92. ])
  93. ->add('body', 'length', [
  94. 'message' => 'Must be so long',
  95. 'rule' => ['minLength', 12],
  96. ]);
  97. $data = [
  98. 'email' => 'rong',
  99. 'body' => 'too short'
  100. ];
  101. $form->validate($data);
  102. $errors = $form->errors();
  103. $this->assertCount(2, $errors);
  104. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  105. $this->assertEquals('Must be so long', $errors['body']['length']);
  106. }
  107. /**
  108. * Test setErrors()
  109. *
  110. * @return void
  111. */
  112. public function testSetErrors()
  113. {
  114. $form = new Form();
  115. $expected = [
  116. 'field_name' => ['rule_name' => 'message']
  117. ];
  118. $form->setErrors($expected);
  119. $this->assertSame($expected, $form->errors());
  120. }
  121. /**
  122. * Test _execute is skipped on validation failure.
  123. *
  124. * @return void
  125. */
  126. public function testExecuteInvalid()
  127. {
  128. $form = $this->getMockBuilder('Cake\Form\Form')
  129. ->setMethods(['_execute'])
  130. ->getMock();
  131. $form->getValidator()
  132. ->add('email', 'format', ['rule' => 'email']);
  133. $data = [
  134. 'email' => 'rong'
  135. ];
  136. $form->expects($this->never())
  137. ->method('_execute');
  138. $this->assertFalse($form->execute($data));
  139. }
  140. /**
  141. * test execute() when data is valid.
  142. *
  143. * @return void
  144. */
  145. public function testExecuteValid()
  146. {
  147. $form = $this->getMockBuilder('Cake\Form\Form')
  148. ->setMethods(['_execute'])
  149. ->getMock();
  150. $form->getValidator()
  151. ->add('email', 'format', ['rule' => 'email']);
  152. $data = [
  153. 'email' => 'test@example.com'
  154. ];
  155. $form->expects($this->once())
  156. ->method('_execute')
  157. ->with($data)
  158. ->will($this->returnValue(true));
  159. $this->assertTrue($form->execute($data));
  160. }
  161. /**
  162. * test __debugInfo
  163. *
  164. * @return void
  165. */
  166. public function testDebugInfo()
  167. {
  168. $form = new Form();
  169. $result = $form->__debugInfo();
  170. $this->assertArrayHasKey('_schema', $result);
  171. $this->assertArrayHasKey('_errors', $result);
  172. $this->assertArrayHasKey('_validator', $result);
  173. }
  174. }