Skip to content

Commit

Permalink
Renaming property and adding unit test for BelongsToMany::link()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 10, 2013
1 parent 744ae5c commit 427844a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Cake/ORM/Entity.php
Expand Up @@ -73,12 +73,12 @@ class Entity implements \ArrayAccess, \JsonSerializable {
protected static $_accessors = [];

/**
* Indicates whether or not this entity has already been persisted.
* Indicates whether or not this entity is yet to be persisted.
* A null value indicates an unknown persistence status
*
* @var boolean
*/
protected $_persisted = null;
protected $_new = null;

/**
* List of errors per field as stored in this object
Expand Down Expand Up @@ -512,9 +512,9 @@ public function clean() {
*/
public function isNew($new = null) {
if ($new === null) {
return $this->_persisted;
return $this->_new;
}
return $this->_persisted = (bool)$new;
return $this->_new = (bool)$new;
}

/**
Expand Down
77 changes: 77 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -639,4 +639,81 @@ public function testCascadeDeleteWithCallbacks() {
$association->cascadeDelete($entity);
}

/**
* Test liking entities having a non persited source entity
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Source entity needs to be persisted before linking
* @return void
*/
public function testLinkWithNotPersistedSource() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$entity = new Entity(['id' => 1]);
$tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
$assoc->link($entity, $tags);
}

/**
* Test liking entities having a non persited target entity
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot link not persisted entities
* @return void
*/
public function testLinkWithNotPersistedTarget() {
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
];
$assoc = new BelongsToMany('Test', $config);
$entity = new Entity(['id' => 1], ['markNew' => false]);
$tags = [new Entity(['id' => 2]), new Entity(['id' => 3])];
$assoc->link($entity, $tags);
}

/**
* Tests that liking entities will validate data and pass on to _saveLinks
*
* @return void
*/
public function testLinkSuccess() {
$joint = $this->getMock('\Cake\ORM\Table', ['save'], [['alias' => 'ArticlesTags']]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'through' => $joint,
'joinTable' => 'tags_articles'
];

$assoc = new BelongsToMany('Test', $config);
$opts = ['markNew' => false];
$entity = new Entity(['id' => 1], $opts);
$tags = [new Entity(['id' => 2], $opts), new Entity(['id' => 3], $opts)];
$saveOptions = ['foo' => 'bar'];

$joint->expects($this->at(0))
->method('save')
->with(
new Entity(['article_id' => 1, 'tag_id' => 2], ['markNew' => true]),
$saveOptions
)
->will($this->returnValue($entity));

$joint->expects($this->at(1))
->method('save')
->with(
new Entity(['article_id' => 1, 'tag_id' => 3], ['markNew' => true]),
$saveOptions
)
->will($this->returnValue($entity));

$this->assertTrue($assoc->link($entity, $tags, $saveOptions));
}

}

0 comments on commit 427844a

Please sign in to comment.