Skip to content

Commit

Permalink
Add tests for callback related features.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 2, 2013
1 parent 4449dfa commit 9291f2b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cake/ORM/Table.php
Expand Up @@ -938,6 +938,7 @@ public function delete(Entity $entity, array $options = []) {
$process = function () use ($entity, $options) {
return $this->_processDelete($entity, $options);
};

if ($options['atomic']) {
$success = $this->connection()->transactional($process);
} else {
Expand Down
75 changes: 70 additions & 5 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1560,7 +1560,7 @@ public function testUpdateNoPrimaryButOtherKeys() {
* @return void
*/
public function testDelete() {
$table = TableRegistry::get('user');
$table = TableRegistry::get('users');
$conditions = [
'limit' => 1,
'conditions' => [
Expand Down Expand Up @@ -1601,7 +1601,33 @@ public function testDeleteBelongsToMany() {
* @return void
*/
public function testDeleteCallbacks() {
$this->markTestIncomplete('not done');
$entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);
$options = new \ArrayObject(['atomic' => true, 'cascade' => true]);

$mock = $this->getMock('Cake\Event\EventManager');
$mock->expects($this->at(0))
->method('dispatch')
->with($this->logicalAnd(
$this->attributeEqualTo('_name', 'Model.beforeDelete'),
$this->attributeEqualTo(
'data',
['entity' => $entity, 'options' => $options]
)
));

$mock->expects($this->at(1))
->method('dispatch')
->with($this->logicalAnd(
$this->attributeEqualTo('_name', 'Model.afterDelete'),
$this->attributeEqualTo(
'data',
['entity' => $entity, 'options' => $options]
)
));

$table = TableRegistry::get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$table->delete($entity);
}

/**
Expand All @@ -1610,7 +1636,20 @@ public function testDeleteCallbacks() {
* @return void
*/
public function testDeleteBeforeDeleteAbort() {
$this->markTestIncomplete('not done');
$entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);
$options = new \ArrayObject(['atomic' => true, 'cascade' => true]);

$mock = $this->getMock('Cake\Event\EventManager');
$mock->expects($this->once())
->method('dispatch')
->will($this->returnCallback(function ($event) {
$event->stopPropagation();
}));

$table = TableRegistry::get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$result = $table->delete($entity);
$this->assertNull($result);
}

/**
Expand All @@ -1619,7 +1658,21 @@ public function testDeleteBeforeDeleteAbort() {
* @return void
*/
public function testDeleteBeforeDeleteReturnResult() {
$this->markTestIncomplete('not done');
$entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);
$options = new \ArrayObject(['atomic' => true, 'cascade' => true]);

$mock = $this->getMock('Cake\Event\EventManager');
$mock->expects($this->once())
->method('dispatch')
->will($this->returnCallback(function ($event) {
$event->stopPropagation();
$event->result = 'got stopped';
}));

$table = TableRegistry::get('users', ['eventManager' => $mock]);
$entity->isNew(false);
$result = $table->delete($entity);
$this->assertEquals('got stopped', $result);
}

/**
Expand All @@ -1628,7 +1681,19 @@ public function testDeleteBeforeDeleteReturnResult() {
* @return void
*/
public function testDeleteIsNew() {
$this->markTestIncomplete('not done');
$entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);

$table = $this->getMock(
'Cake\ORM\Table',
['_buildQuery'],
[['connection' => $this->connection]]
);
$table->expects($this->never())
->method('_buildQuery');

$entity->isNew(true);
$result = $table->delete($entity);
$this->assertFalse($result);
}

}

0 comments on commit 9291f2b

Please sign in to comment.