Skip to content

Commit

Permalink
Adding more integration tests for replaceLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 15, 2013
1 parent 005d87f commit d663488
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2888,4 +2888,54 @@ public function testReplacelinksBelongsToMany() {
$this->assertEquals(3, $article->tags[1]->id);
}

/**
* Integration test to show how remove all links from a belongsToMany
*
* @return void
*/
public function testReplacelinksBelongsToManyWithEmpty() {
$table = TableRegistry::get('articles');
$table->belongsToMany('tags');
$tagsTable = TableRegistry::get('tags');
$options = ['markNew' => false];

$article = new \Cake\ORM\Entity(['id' => 1,], $options);
$tags = [];

$table->association('tags')->replaceLinks($article, $tags);
$this->assertSame($tags, $article->tags);
$article = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
$this->assertNull($article->tags);
}

/**
* Integration test to show how to replace records from a belongsToMany
* passing the joint property along in the target entity
*
* @return void
*/
public function testReplacelinksBelongsToManyWithJoint() {
$table = TableRegistry::get('articles');
$table->belongsToMany('tags');
$tagsTable = TableRegistry::get('tags');
$options = ['markNew' => false];

$article = new \Cake\ORM\Entity(['id' => 1,], $options);
$tags[] = new \TestApp\Model\Entity\Tag([
'id' => 2,
'extraInfo' => new \Cake\ORM\Entity([
'article_id' => 1,
'tag_id' => 2,
])
], $options);
$tags[] = new \TestApp\Model\Entity\Tag(['id' => 3], $options);

$table->association('tags')->replaceLinks($article, $tags);
$this->assertSame($tags, $article->tags);
$article = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
$this->assertCount(2, $article->tags);
$this->assertEquals(2, $article->tags[0]->id);
$this->assertEquals(3, $article->tags[1]->id);
}

}

0 comments on commit d663488

Please sign in to comment.