Skip to content

Commit

Permalink
Adding more tests for beforeFind on attachable associations
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 9, 2014
1 parent 148033f commit a8c6f99
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
76 changes: 76 additions & 0 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -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']);
}]);
}


}
1 change: 0 additions & 1 deletion tests/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -373,5 +373,4 @@ public function testAttachToBeforeFindExtraOptions() {
}]);
}


}
45 changes: 45 additions & 0 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -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']);
}]);
}

}
45 changes: 45 additions & 0 deletions tests/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -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.