Skip to content
Permalink
Browse files

Adding more tests for beforeFind on attachable associations

  • Loading branch information...
lorenzo committed Feb 2, 2014
1 parent 148033f commit a8c6f99169ac11b331f6bf9ed93d6882515f8d0b
@@ -1523,4 +1523,80 @@ public function testPropertyNoPlugin() {
$this->assertEquals('tags', $association->property());
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFind() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
];
$table = TableRegistry::get('ArticlesTags', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
'tag_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]
]
]
]);
$association = new BelongsToMany('Tags', $config);
$listener = $this->getMock('stdClass', ['__invoke']);
$this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, [], false);
$listener2 = $this->getMock('stdClass', ['__invoke']);
$table->getEventManager()->attach($listener2, 'Model.beforeFind');
$listener2->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, [], false);
$association->attachTo($query);
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFindExtraOptions() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
];
$table = TableRegistry::get('ArticlesTags', [
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
'tag_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]
]
]
]);
$association = new BelongsToMany('Tags', $config);
$listener = $this->getMock('stdClass', ['__invoke']);
$this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
$opts = ['somthing' => 'more'];
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, $opts, false);
$listener2 = $this->getMock('stdClass', ['__invoke']);
$table->getEventManager()->attach($listener2, 'Model.beforeFind');
$listener2->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, [], false);
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['somthing' => 'more']);
}]);
}
}
@@ -373,5 +373,4 @@ public function testAttachToBeforeFindExtraOptions() {
}]);
}
}
@@ -772,4 +772,49 @@ public function testPropertyNoPlugin() {
$this->assertEquals('addresses', $association->property());
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFind() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
];
$listener = $this->getMock('stdClass', ['__invoke']);
$association = new HasMany('Articles', $config);
$this->article->getEventManager()->attach($listener, 'Model.beforeFind');
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, [], false);
$association->attachTo($query);
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFindExtraOptions() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'sourceTable' => $this->author,
'targetTable' => $this->article,
];
$listener = $this->getMock('stdClass', ['__invoke']);
$association = new HasMany('Articles', $config);
$this->article->getEventManager()->attach($listener, 'Model.beforeFind');
$opts = ['something' => 'more'];
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, $opts, false);
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
}]);
}
}
@@ -305,4 +305,49 @@ public function testPropertyNoPlugin() {
$this->assertEquals('profile', $association->property());
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFind() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
'targetTable' => $this->profile,
];
$listener = $this->getMock('stdClass', ['__invoke']);
$this->profile->getEventManager()->attach($listener, 'Model.beforeFind');
$association = new HasOne('Profiles', $config);
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, [], false);
$association->attachTo($query);
}
/**
* Tests that attaching an association to a query will trigger beforeFind
* for the target table
*
* @return void
*/
public function testAttachToBeforeFindExtraOptions() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null, null]);
$config = [
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
'targetTable' => $this->profile,
];
$listener = $this->getMock('stdClass', ['__invoke']);
$this->profile->getEventManager()->attach($listener, 'Model.beforeFind');
$association = new HasOne('Profiles', $config);
$opts = ['something' => 'more'];
$listener->expects($this->once())->method('__invoke')
->with($this->isInstanceOf('\Cake\Event\Event'), $query, $opts, false);
$association->attachTo($query, ['queryBuilder' => function($q) {
return $q->applyOptions(['something' => 'more']);
}]);
}
}

0 comments on commit a8c6f99

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