Skip to content

Commit

Permalink
Add tests for type() and refactor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 2, 2014
1 parent 9f5555a commit bfb6b9d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/View/Form/EntityContext.php
Expand Up @@ -17,7 +17,6 @@
use Cake\Network\Request;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Validation\Validator;
use Traversable;

Expand Down
150 changes: 77 additions & 73 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -130,20 +130,12 @@ public function testValAssociated() {
* @return void
*/
public function testIsRequiredStringValidator() {
$articles = TableRegistry::get('Articles');

$validator = $articles->validator();
$validator->add('title', 'minlength', [
'rule' => ['minlength', 10]
])
->add('body', 'maxlength', [
'rule' => ['maxlength', 1000]
])->allowEmpty('body');
$this->_setupTables();

$context = new EntityContext($this->request, [
'entity' => new Entity(),
'table' => 'Articles',
'validator' => 'default',
'validator' => 'create',
]);

$this->assertTrue($context->isRequired('Articles.title'));
Expand All @@ -161,18 +153,12 @@ public function testIsRequiredStringValidator() {
* @return void
*/
public function testIsRequiredAssociatedHasMany() {
$articles = TableRegistry::get('Articles');
$articles->hasMany('Comments');
$comments = TableRegistry::get('Comments');

$validator = $articles->validator();
$validator->add('title', 'minlength', [
'rule' => ['minlength', 10]
]);
$this->_setupTables();

$comments = TableRegistry::get('Comments');
$validator = $comments->validator();
$validator->add('comment', 'length', [
'rule' => ['minlength', 10]
$validator->add('user_id', 'number', [
'rule' => 'numeric',
]);

$row = new Entity([
Expand All @@ -188,11 +174,8 @@ public function testIsRequiredAssociatedHasMany() {
'validator' => 'default',
]);

// $this->assertTrue($context->isRequired('Articles.title'));
// $this->assertFalse($context->isRequired('Articles.body'));

$this->assertTrue($context->isRequired('comments.0.comment'));
$this->assertTrue($context->isRequired('Articles.comments.0.comment'));
$this->assertTrue($context->isRequired('comments.0.user_id'));
$this->assertTrue($context->isRequired('Articles.comments.0.user_id'));

$this->assertFalse($context->isRequired('comments.0.other'));
$this->assertFalse($context->isRequired('Articles.comments.0.other'));
Expand All @@ -204,21 +187,7 @@ public function testIsRequiredAssociatedHasMany() {
* @return void
*/
public function testIsRequiredAssociatedValidator() {
$articles = TableRegistry::get('Articles');
$articles->hasMany('Comments');
$comments = TableRegistry::get('Comments');

$validator = new Validator();
$validator->add('title', 'minlength', [
'rule' => ['minlength', 10]
]);
$articles->validator('create', $validator);

$validator = new Validator();
$validator->add('comment', 'length', [
'rule' => ['minlength', 10]
]);
$comments->validator('custom', $validator);
$this->_setupTables();

$row = new Entity([
'title' => 'My title',
Expand Down Expand Up @@ -248,21 +217,7 @@ public function testIsRequiredAssociatedValidator() {
* @return void
*/
public function testIsRequiredAssociatedBelongsTo() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$users = TableRegistry::get('Users');

$validator = new Validator();
$validator->add('title', 'minlength', [
'rule' => ['minlength', 10]
]);
$articles->validator('create', $validator);

$validator = new Validator();
$validator->add('username', 'length', [
'rule' => ['minlength', 10]
]);
$users->validator('custom', $validator);
$this->_setupTables();

$row = new Entity([
'title' => 'My title',
Expand All @@ -287,12 +242,7 @@ public function testIsRequiredAssociatedBelongsTo() {
* @return void
*/
public function testType() {
$articles = TableRegistry::get('Articles');
$articles->schema([
'title' => ['type' => 'string'],
'body' => ['type' => 'text'],
'user_id' => ['type' => 'integer']
]);
$this->_setupTables();

$row = new Entity([
'title' => 'My title',
Expand All @@ -315,18 +265,7 @@ public function testType() {
* @return void
*/
public function testTypeAssociated() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$users = TableRegistry::get('Users');

$articles->schema([
'title' => ['type' => 'string'],
'body' => ['type' => 'text']
]);
$users->schema([
'username' => ['type' => 'string'],
'bio' => ['type' => 'text']
]);
$this->_setupTables();

$row = new Entity([
'title' => 'My title',
Expand All @@ -342,4 +281,69 @@ public function testTypeAssociated() {
$this->assertNull($context->type('user.nope'));
}

/**
* Test attributes for fields.
*
* @return void
*/
public function testAttributes() {
$this->_setupTables();

$row = new Entity([
'title' => 'My title',
'user' => new Entity(['username' => 'Mark']),
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
]);
}

/**
* Setup tables for tests.
*
* @return void
*/
protected function _setupTables() {
$articles = TableRegistry::get('Articles');
$articles->belongsTo('Users');
$articles->hasMany('Comments');

$comments = TableRegistry::get('Comments');
$users = TableRegistry::get('Users');

$articles->schema([
'id' => ['type' => 'integer', 'length' => 11, 'null' => false],
'title' => ['type' => 'string', 'length' => 255],
'user_id' => ['type' => 'integer', 'length' => 11, 'null' => false],
'body' => ['type' => 'text']
]);
$users->schema([
'id' => ['type' => 'integer', 'length' => 11],
'username' => ['type' => 'string', 'length' => 255],
'bio' => ['type' => 'text']
]);

$validator = new Validator();
$validator->add('title', 'minlength', [
'rule' => ['minlength', 10]
])
->add('body', 'maxlength', [
'rule' => ['maxlength', 1000]
])->allowEmpty('body');
$articles->validator('create', $validator);

$validator = new Validator();
$validator->add('username', 'length', [
'rule' => ['minlength', 10]
]);
$users->validator('custom', $validator);

$validator = new Validator();
$validator->add('comment', 'length', [
'rule' => ['minlength', 10]
]);
$comments->validator('custom', $validator);
}

}

0 comments on commit bfb6b9d

Please sign in to comment.