FormTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Test schema()
  24. *
  25. * @return void
  26. */
  27. public function testSchema() {
  28. $form = new Form();
  29. $schema = $form->schema();
  30. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  31. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  32. $schema = $this->getMock('Cake\Form\Schema');
  33. $this->assertSame($schema, $form->schema($schema));
  34. $this->assertSame($schema, $form->schema());
  35. }
  36. /**
  37. * Test validator()
  38. *
  39. * @return void
  40. */
  41. public function testValidator() {
  42. $form = new Form();
  43. $validator = $form->validator();
  44. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  45. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  46. $validator = $this->getMock('Cake\Validation\Validator');
  47. $this->assertSame($validator, $form->validator($validator));
  48. $this->assertSame($validator, $form->validator());
  49. }
  50. /**
  51. * Test isValid method.
  52. *
  53. * @return void
  54. */
  55. public function testIsValid() {
  56. $form = new Form();
  57. $form->validator()
  58. ->add('email', 'format', ['rule' => 'email'])
  59. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  60. $data = [
  61. 'email' => 'rong',
  62. 'body' => 'too short'
  63. ];
  64. $this->assertFalse($form->isValid($data));
  65. $this->assertCount(2, $form->errors());
  66. $data = [
  67. 'email' => 'test@example.com',
  68. 'body' => 'Some content goes here'
  69. ];
  70. $this->assertTrue($form->isValid($data));
  71. $this->assertCount(0, $form->errors());
  72. }
  73. /**
  74. * Test the errors methods.
  75. *
  76. * @return void
  77. */
  78. public function testErrors() {
  79. $form = new Form();
  80. $form->validator()
  81. ->add('email', 'format', [
  82. 'message' => 'Must be a valid email',
  83. 'rule' => 'email'
  84. ])
  85. ->add('body', 'length', [
  86. 'message' => 'Must be so long',
  87. 'rule' => ['minLength', 12],
  88. ]);
  89. $data = [
  90. 'email' => 'rong',
  91. 'body' => 'too short'
  92. ];
  93. $form->isValid($data);
  94. $errors = $form->errors();
  95. $this->assertCount(2, $errors);
  96. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  97. $this->assertEquals('Must be so long', $errors['body']['length']);
  98. }
  99. /**
  100. * Test _execute is skipped on validation failure.
  101. *
  102. * @return void
  103. */
  104. public function testExecuteInvalid() {
  105. $form = $this->getMock('Cake\Form\Form', ['_execute']);
  106. $form->validator()
  107. ->add('email', 'format', ['rule' => 'email']);
  108. $data = [
  109. 'email' => 'rong'
  110. ];
  111. $form->expects($this->never())
  112. ->method('_execute');
  113. $this->assertFalse($form->execute($data));
  114. }
  115. /**
  116. * test execute() when data is valid.
  117. *
  118. * @return void
  119. */
  120. public function testExecuteValid() {
  121. $form = $this->getMock('Cake\Form\Form', ['_execute']);
  122. $form->validator()
  123. ->add('email', 'format', ['rule' => 'email']);
  124. $data = [
  125. 'email' => 'test@example.com'
  126. ];
  127. $form->expects($this->once())
  128. ->method('_execute')
  129. ->with($data)
  130. ->will($this->returnValue(true));
  131. $this->assertTrue($form->execute($data));
  132. }
  133. }