Skip to content
Permalink
Browse files

Unit test to prove HasMany.link is using multiple transactions

  • Loading branch information...
coryjthompson committed Jan 23, 2017
1 parent 3ec192f commit d7353382562f5cda6869c27ceb6e9b83a20c03cc
Showing with 32 additions and 0 deletions.
  1. +32 −0 tests/TestCase/ORM/Association/HasManyTest.php
@@ -693,4 +693,36 @@ public function testUnlinkWithEmptyArray()
$this->assertSame(4, $this->author->find()->count(), 'Authors should still exist');
$this->assertSame(3, $articles->find()->count(), 'Articles should still exist');
}
/**
* Tests that link only uses a single database transaction
*
* @return void
*/
public function testLinkUsesSingleTransaction()
{
$articles = TableRegistry::get('Articles');
$assoc = $this->author->hasMany('Articles', [
'sourceTable' => $this->author,
'targetTable' => $articles
]);
// Ensure author in fixture has zero associated articles
$entity = $this->author->get(2, ['contain' => 'Articles']);
$initial = $entity->articles;
$this->assertCount(0, $initial);
// Ensure that after each model is saved, we are still within a transaction.
$listenerAfterSave = function ($e, $entity, $options) use ($articles) {
$debugInfo = $articles->connection()->__debugInfo();
$transactionLevel = $debugInfo['transactionLevel'];
$this->assertGreaterThan(0, $transactionLevel);
};
$articles->eventManager()->on('Model.afterSave', $listenerAfterSave);
$assoc->link($entity, $articles->find('all')->toArray());
// Ensure that link was successful.
$new = $this->author->get(2, ['contain' => 'Articles']);
$this->assertCount(3, $new->articles);
}
}

0 comments on commit d735338

Please sign in to comment.
You can’t perform that action at this time.