Skip to content

Commit

Permalink
Added unit test for missing primary key in replaceLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 15, 2013
1 parent 53e6d87 commit 5ce7482
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 0 additions & 1 deletion Cake/Database/Type.php
Expand Up @@ -17,7 +17,6 @@
namespace Cake\Database;

use Cake\Database\Driver;
use Exception;
use PDO;

/**
Expand Down
5 changes: 3 additions & 2 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -484,8 +484,9 @@ public function replaceLinks(Entity $sourceEntity, array $targetEntities, array
$primaryKey = (array)$this->source()->primaryKey();
$primaryValue = $sourceEntity->extract($primaryKey);

if (empty($primaryValue)) {
throw new \InvalidArgumentException;
if (count(array_filter($primaryValue, 'strlen')) !== count($primaryKey)) {
$message = __d('cake_dev', 'Could not find primary key value for source entity');
throw new \InvalidArgumentException($message);
}

return $this->junction()->connection()->transactional(
Expand Down
19 changes: 19 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -899,4 +899,23 @@ public function testUnlinkWithoutPropertyClean() {
$this->assertEquals($tags, $entity->get('test'));
}

/**
* Test liking entities having a non persited source entity
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Could not find primary key value for source entity
* @return void
*/
public function testRplaceWithMissingPrimaryKey() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$entity = new Entity(['foo' => 1], ['markNew' => false]);
$tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
$assoc->replaceLinks($entity, $tags);
}

}

0 comments on commit 5ce7482

Please sign in to comment.