Skip to content

Commit

Permalink
Start implementing Form class.
Browse files Browse the repository at this point in the history
Implement some of the basic features for the form class.
  • Loading branch information
markstory committed Dec 14, 2014
1 parent ae69a80 commit 875a464
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Form/Form.php
@@ -0,0 +1,73 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Form;

use Cake\Form\Schema;
use Cake\Validation\Validator;

/**
* Form abstraction used to create forms not tied to ORM backed models,
* or to other permanent datastores. Ideal for implement forms on top of
* API services, or contact forms.
*
*/
class Form {

protected $_schema;
protected $_errors = [];
protected $_validator;

public function schema(Schema $schema = null) {
if ($schema === null && empty($this->_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) {
}

}
91 changes: 91 additions & 0 deletions tests/TestCase/Form/FormTest.php
@@ -0,0 +1,91 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Form;

use Cake\Form\Form;
use Cake\TestSuite\TestCase;

/**
* Form test case.
*/
class FormTest extends TestCase {

/**
* Test schema()
*
* @return void
*/
public function testSchema() {
$form = new Form();
$schema = $form->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() {
}

}

0 comments on commit 875a464

Please sign in to comment.