Browse Source

Implement remaining basic Form interface.

Mark Story 11 years ago
parent
commit
f8323cb477
2 changed files with 61 additions and 0 deletions
  1. 7 0
      src/Form/Form.php
  2. 54 0
      tests/TestCase/Form/FormTest.php

+ 7 - 0
src/Form/Form.php

@@ -68,6 +68,13 @@ class Form {
 	}
 
 	public function execute($data) {
+		if (!$this->isValid($data)) {
+			return false;
+		}
+		return $this->_execute($data);
+	}
+
+	protected function _execute($data) {
 	}
 
 }

+ 54 - 0
tests/TestCase/Form/FormTest.php

@@ -82,10 +82,64 @@ class FormTest extends TestCase {
 		$this->assertCount(0, $form->errors());
 	}
 
+/**
+ * Test the errors methods.
+ *
+ * @return void
+ */
+	public function testErrors() {
+		$form = new Form();
+		$form->validator()
+			->add('email', 'format', [
+				'message' => 'Must be a valid email',
+				'rule' => 'email'
+			])
+			->add('body', 'length', [
+				'message' => 'Must be so long',
+				'rule' => ['minLength', 12],
+			]);
+
+		$data = [
+			'email' => 'rong',
+			'body' => 'too short'
+		];
+		$form->isValid($data);
+		$errors = $form->errors();
+		$this->assertCount(2, $errors);
+		$this->assertEquals('Must be a valid email', $errors['email']['format']);
+		$this->assertEquals('Must be so long', $errors['body']['length']);
+	}
+
+/**
+ * Test _execute is skipped on validation failure.
+ *
+ * @return void
+ */
 	public function testExecuteInvalid() {
+		$form = $this->getMock('Cake\Form\Form', ['_execute']);
+		$form->validator()
+			->add('email', 'format', ['rule' => 'email']);
+		$data = [
+			'email' => 'rong'
+		];
+		$form->expects($this->never())
+			->method('_execute');
+
+		$this->assertFalse($form->execute($data));
 	}
 
 	public function testExecuteValid() {
+		$form = $this->getMock('Cake\Form\Form', ['_execute']);
+		$form->validator()
+			->add('email', 'format', ['rule' => 'email']);
+		$data = [
+			'email' => 'test@example.com'
+		];
+		$form->expects($this->once())
+			->method('_execute')
+			->with($data);
+
+		$this->assertNull($form->execute($data));
 	}
 
 }