Skip to content
Permalink
Browse files

Renaming property and adding unit test for BelongsToMany::link()

  • Loading branch information...
lorenzo committed Dec 10, 2013
1 parent 744ae5c commit 427844a1408d4ea4ffd97ddf6459f10aff4c53d9
Showing with 81 additions and 4 deletions.
  1. +4 −4 Cake/ORM/Entity.php
  2. +77 −0 Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
@@ -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
@@ -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;
}
/**
@@ -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.
You can’t perform that action at this time.