Skip to content

Commit

Permalink
Completing unit tests fir unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 14, 2013
1 parent 8dce1e2 commit ac00e21
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 7 deletions.
177 changes: 177 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -717,4 +717,181 @@ public function testLinkSuccess() {
$this->assertSame($entity->test, $tags);
}

/**
* Test liking entities having a non persited source entity
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Source entity needs to be persisted before proceeding
* @return void
*/
public function testUnlinkWithNotPersistedSource() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$entity = new Entity(['id' => 1]);
$tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
$assoc->unlink($entity, $tags);
}

/**
* Test liking entities having a non persited target entity
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot link not persisted entities
* @return void
*/
public function testUnlinkWithNotPersistedTarget() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$entity = new Entity(['id' => 1], ['markNew' => false]);
$tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
$assoc->unlink($entity, $tags);
}

/**
* Tests that unlinking calls the right methods
*
* @return void
*/
public function testUnlinkSuccess() {
$connection = \Cake\Database\ConnectionManager::get('test');
$joint = $this->getMock(
'\Cake\ORM\Table',
['delete', 'find'],
[['alias' => 'ArticlesTags', 'connection' => $connection]]
);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'through' => $joint,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$assoc
->junction()
->association('tags')
->conditions(['foo' => 1]);

$query1 = $this->getMock('\Cake\ORM\Query', [], [$connection, $joint]);
$query2 = $this->getMock('\Cake\ORM\Query', [], [$connection, $joint]);

$joint->expects($this->at(0))->method('find')
->with('all')
->will($this->returnValue($query1));

$joint->expects($this->at(1))->method('find')
->with('all')
->will($this->returnValue($query2));

$query1->expects($this->once())
->method('where')
->with(['article_id' => 1])
->will($this->returnSelf());
$query1->expects($this->at(1))
->method('andWhere')
->with(['tag_id' => 2])
->will($this->returnSelf());
$query1->expects($this->at(2))
->method('andWhere')
->with(['foo' => 1])
->will($this->returnSelf());
$query1->expects($this->once())
->method('union')
->with($query2)
->will($this->returnSelf());

$query2->expects($this->once())
->method('where')
->with(['article_id' => 1])
->will($this->returnSelf());
$query2->expects($this->at(1))
->method('andWhere')
->with(['tag_id' => 3])
->will($this->returnSelf());
$query2->expects($this->at(2))
->method('andWhere')
->with(['foo' => 1])
->will($this->returnSelf());

$jointEntities = [
new Entity(['article_id' => 1, 'tag_id' => 2]),
new Entity(['article_id' => 1, 'tag_id' => 3])
];

$query1->expects($this->once())
->method('toArray')
->will($this->returnValue($jointEntities));

$opts = ['markNew' => false];
$tags = [new Entity(['id' => 2], $opts), new Entity(['id' => 3], $opts)];
$entity = new Entity(['id' => 1, 'test' => $tags], $opts);

$joint->expects($this->at(2))
->method('delete')
->with($jointEntities[0]);

$joint->expects($this->at(3))
->method('delete')
->with($jointEntities[1]);

$assoc->unlink($entity, $tags);
$this->assertEmpty($entity->get('test'));
}

/**
* Tests that unlinking with last parameter set to false
* will not remove entities from the association property
*
* @return void
*/
public function testUnlinkWithoutPropertyClean() {
$connection = \Cake\Database\ConnectionManager::get('test');
$joint = $this->getMock(
'\Cake\ORM\Table',
['delete', 'find'],
[['alias' => 'ArticlesTags', 'connection' => $connection]]
);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'through' => $joint,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$assoc
->junction()
->association('tags')
->conditions(['foo' => 1]);

$joint->expects($this->never())->method('find');
$opts = ['markNew' => false];
$jointEntities = [
new Entity(['article_id' => 1, 'tag_id' => 2]),
new Entity(['article_id' => 1, 'tag_id' => 3])
];
$tags = [
new Entity(['id' => 2, 'articles_tags' => $jointEntities[0]], $opts),
new Entity(['id' => 3, 'articles_tags' => $jointEntities[1]], $opts)
];
$entity = new Entity(['id' => 1, 'test' => $tags], $opts);

$joint->expects($this->at(0))
->method('delete')
->with($jointEntities[0]);

$joint->expects($this->at(1))
->method('delete')
->with($jointEntities[1]);

$assoc->unlink($entity, $tags, false);
$this->assertEquals($tags, $entity->get('test'));
}

}
15 changes: 8 additions & 7 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2809,13 +2809,14 @@ public function testUnlinkBelongsToMany() {
$tagsTable = TableRegistry::get('tags');
$options = ['markNew' => false];

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

$table->association('tags')->unlink($article, $tags);
$left = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
$this->assertCount(1, $left->tags);
$this->assertEquals(2, $left->tags[0]->get('id'));
$article = $table->find('all')
->where(['id' => 1])
->contain(['tags'])->first();

$table->association('tags')->unlink($article, [$article->tags[0]]);
$this->assertCount(1, $article->tags);
$this->assertEquals(2, $article->tags[0]->get('id'));
$this->assertFalse($article->dirty('tags'));
}

/**
Expand Down

0 comments on commit ac00e21

Please sign in to comment.