diff --git a/src/Form/Form.php b/src/Form/Form.php new file mode 100644 index 00000000000..dd3d1c107e6 --- /dev/null +++ b/src/Form/Form.php @@ -0,0 +1,73 @@ +_schema)) { + $schema = $this->_buildSchema(new Schema()); + } + if ($schema) { + $this->_schema = $schema; + } + return $this->_schema; + } + + protected function _buildSchema($schema) { + return $schema; + } + + public function validator(Validator $validator = null) { + if ($validator === null && empty($this->_validator)) { + $validator = $this->_buildValidator(new Validator()); + } + if ($validator) { + $this->_validator = $validator; + } + return $this->_validator; + } + + protected function _buildValidator($validator) { + return $validator; + } + + public function isValid($data) { + $validator = $this->validator(); + $this->_errors = $validator->errors($data); + return count($this->_errors) === 0; + } + + public function errors() { + return $this->_errors; + } + + public function execute($data) { + } + +} diff --git a/tests/TestCase/Form/FormTest.php b/tests/TestCase/Form/FormTest.php new file mode 100644 index 00000000000..9832d4ffaee --- /dev/null +++ b/tests/TestCase/Form/FormTest.php @@ -0,0 +1,91 @@ +schema(); + + $this->assertInstanceOf('Cake\Form\Schema', $schema); + $this->assertSame($schema, $form->schema(), 'Same instance each time'); + + $schema = $this->getMock('Cake\Form\Schema'); + $this->assertSame($schema, $form->schema($schema)); + $this->assertSame($schema, $form->schema()); + } + +/** + * Test validator() + * + * @return void + */ + public function testValidator() { + $form = new Form(); + $validator = $form->validator(); + + $this->assertInstanceOf('Cake\Validation\Validator', $validator); + $this->assertSame($validator, $form->validator(), 'Same instance each time'); + + $schema = $this->getMock('Cake\Validation\Validator'); + $this->assertSame($validator, $form->validator($validator)); + $this->assertSame($validator, $form->validator()); + } + +/** + * Test isValid method. + * + * @return void + */ + public function testIsValid() { + $form = new Form(); + $form->validator() + ->add('email', 'format', ['rule' => 'email']) + ->add('body', 'length', ['rule' => ['minLength', 12]]); + + $data = [ + 'email' => 'rong', + 'body' => 'too short' + ]; + $this->assertFalse($form->isValid($data)); + $this->assertCount(2, $form->errors()); + + $data = [ + 'email' => 'test@example.com', + 'body' => 'Some content goes here' + ]; + $this->assertTrue($form->isValid($data)); + $this->assertCount(0, $form->errors()); + } + + public function testExecuteInvalid() { + } + + public function testExecuteValid() { + } + +}