Skip to content

Commit

Permalink
Unit test to prove HasMany.link is using multiple transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Thompson committed Jan 23, 2017
1 parent 3ec192f commit d735338
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -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.