FormTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. */
  43. public function testValidator()
  44. {
  45. $form = new Form();
  46. $validator = $form->validator();
  47. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  48. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  49. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  50. $this->assertSame($validator, $form->validator($validator));
  51. $this->assertSame($validator, $form->validator());
  52. }
  53. /**
  54. * Test validate method.
  55. *
  56. * @return void
  57. */
  58. public function testValidate()
  59. {
  60. $form = new Form();
  61. $form->validator()
  62. ->add('email', 'format', ['rule' => 'email'])
  63. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  64. $data = [
  65. 'email' => 'rong',
  66. 'body' => 'too short'
  67. ];
  68. $this->assertFalse($form->validate($data));
  69. $this->assertCount(2, $form->errors());
  70. $data = [
  71. 'email' => 'test@example.com',
  72. 'body' => 'Some content goes here'
  73. ];
  74. $this->assertTrue($form->validate($data));
  75. $this->assertCount(0, $form->errors());
  76. }
  77. /**
  78. * Test the errors methods.
  79. *
  80. * @return void
  81. */
  82. public function testErrors()
  83. {
  84. $form = new Form();
  85. $form->validator()
  86. ->add('email', 'format', [
  87. 'message' => 'Must be a valid email',
  88. 'rule' => 'email'
  89. ])
  90. ->add('body', 'length', [
  91. 'message' => 'Must be so long',
  92. 'rule' => ['minLength', 12],
  93. ]);
  94. $data = [
  95. 'email' => 'rong',
  96. 'body' => 'too short'
  97. ];
  98. $form->validate($data);
  99. $errors = $form->errors();
  100. $this->assertCount(2, $errors);
  101. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  102. $this->assertEquals('Must be so long', $errors['body']['length']);
  103. }
  104. /**
  105. * Test _execute is skipped on validation failure.
  106. *
  107. * @return void
  108. */
  109. public function testExecuteInvalid()
  110. {
  111. $form = $this->getMockBuilder('Cake\Form\Form')
  112. ->setMethods(['_execute'])
  113. ->getMock();
  114. $form->validator()
  115. ->add('email', 'format', ['rule' => 'email']);
  116. $data = [
  117. 'email' => 'rong'
  118. ];
  119. $form->expects($this->never())
  120. ->method('_execute');
  121. $this->assertFalse($form->execute($data));
  122. }
  123. /**
  124. * test execute() when data is valid.
  125. *
  126. * @return void
  127. */
  128. public function testExecuteValid()
  129. {
  130. $form = $this->getMockBuilder('Cake\Form\Form')
  131. ->setMethods(['_execute'])
  132. ->getMock();
  133. $form->validator()
  134. ->add('email', 'format', ['rule' => 'email']);
  135. $data = [
  136. 'email' => 'test@example.com'
  137. ];
  138. $form->expects($this->once())
  139. ->method('_execute')
  140. ->with($data)
  141. ->will($this->returnValue(true));
  142. $this->assertTrue($form->execute($data));
  143. }
  144. /**
  145. * test __debugInfo
  146. *
  147. * @return void
  148. */
  149. public function testDebugInfo()
  150. {
  151. $form = new Form();
  152. $result = $form->__debugInfo();
  153. $this->assertArrayHasKey('_schema', $result);
  154. $this->assertArrayHasKey('_errors', $result);
  155. $this->assertArrayHasKey('_validator', $result);
  156. }
  157. }