Skip to content

Commit

Permalink
Add unit tests for association classes & cascadeDelete.
Browse files Browse the repository at this point in the history
In addition to the Table integration tests, these are helpful
to have as they test conditions in the associations.
  • Loading branch information
markstory committed Nov 3, 2013
1 parent 6e82645 commit ff9f7c2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -234,7 +234,7 @@ public function cascadeDelete(Entity $entity) {
$conditions = array_merge($conditions, $this->conditions());

$table = $this->pivot();
$table->deleteAll($conditions);
return $table->deleteAll($conditions);
}

/**
Expand Down
32 changes: 30 additions & 2 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
Expand All @@ -36,14 +37,14 @@ class BelongsToManyTest extends TestCase {
public function setUp() {
parent::setUp();
$this->tag = $this->getMock(
'Cake\ORM\Table', ['find'], [['alias' => 'Tag', 'table' => 'tags']]
'Cake\ORM\Table', ['find', 'delete'], [['alias' => 'Tag', 'table' => 'tags']]
);
$this->tag->schema([
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
]);
$this->article = $this->getMock(
'Cake\ORM\Table', ['find'], [['alias' => 'Article', 'table' => 'articles']]
'Cake\ORM\Table', ['find', 'delete'], [['alias' => 'Article', 'table' => 'articles']]
);
$this->article->schema([
'id' => ['type' => 'integer'],
Expand Down Expand Up @@ -542,4 +543,31 @@ public function testEagerLoaderSubquery() {
$this->assertEquals($row, $result);
}

/**
* Test cascading deletes.
*
* @return void
*/
public function testCascadeDelete() {
$articleTag = $this->getMock('Cake\ORM\Table', ['deleteAll'], [], '', false);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'conditions' => ['Tag.name' => 'foo'],
'sort' => ['id' => 'ASC'],
];
$association = new BelongsToMany('Tag', $config);
$association->pivot($articleTag);

$articleTag->expects($this->once())
->method('deleteAll')
->with([
'Tag.name' => 'foo',
'article_id' => 1
]);

$entity = new Entity(['id' => 1, 'name' => 'PHP']);
$association->cascadeDelete($entity);
}

}
22 changes: 22 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
Expand Down Expand Up @@ -162,4 +163,25 @@ public function testAttachToNoFields() {
$association->attachTo($query, ['includeFields' => false]);
}

/**
* Test the cascading delete of BelongsTo.
*
* @return void
*/
public function testCascadeDelete() {
$mock = $this->getMock('Cake\ORM\Table', [], [], '', false);
$config = [
'sourceTable' => $this->client,
'targetTable' => $mock,
];
$mock->expects($this->never())
->method('find');
$mock->expects($this->never())
->method('delete');

$association = new BelongsTo('Company', $config);
$entity = new Entity(['company_name' => 'CakePHP', 'id' => 1]);
$this->assertTrue($association->cascadeDelete($entity));
}

}
49 changes: 48 additions & 1 deletion Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Test\TestCase\ORM\Association;

use Cake\ORM\Association\HasMany;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
Expand All @@ -41,7 +42,7 @@ public function setUp() {
]
]);
$this->article = $this->getMock(
'Cake\ORM\Table', ['find'], [['alias' => 'Article', 'table' => 'articles']]
'Cake\ORM\Table', ['find', 'delete'], [['alias' => 'Article', 'table' => 'articles']]
);
$this->article->schema([
'id' => ['type' => 'integer'],
Expand Down Expand Up @@ -431,4 +432,50 @@ public function testAttachToNoFields() {
$query->expects($this->never())->method('select');
$association->attachTo($query, ['includeFields' => false]);
}

/**
* Test cascading delete with has many.
*
* @return void
*/
public function testCascadeDelete() {
$config = [
'dependent' => true,
'sourceTable' => $this->author,
'targetTable' => $this->article,
'conditions' => ['Article.is_active' => true]
];
$association = new HasMany('Article', $config);

$articleOne = new Entity(['id' => 2, 'title' => 'test']);
$articleTwo = new Entity(['id' => 3, 'title' => 'testing']);
$iterator = new \ArrayIterator([
$articleOne,
$articleTwo
]);

$query = $this->getMock('\Cake\ORM\Query', [], [], '', false);
$query->expects($this->once())
->method('where')
->with(['Article.is_active' => true, 'author_id' => 1])
->will($this->returnSelf());
$query->expects($this->any())
->method('getIterator')
->will($this->returnValue($iterator));

$this->article->expects($this->once())
->method('find')
->will($this->returnValue($query));

$this->article->expects($this->at(1))
->method('delete')
->with($articleOne, ['atomic' => false]);
$this->article->expects($this->at(2))
->method('delete')
->with($articleTwo, ['atomic' => false]);

$entity = new Entity(['id' => 1, 'name' => 'mark']);
$this->assertTrue($association->cascadeDelete($entity));
}

}

0 comments on commit ff9f7c2

Please sign in to comment.