Skip to content

Commit

Permalink
Add a test proving that formaters on a belongsToMany join cause an error
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed Jan 5, 2015
1 parent ae19757 commit c4b2862
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1204,6 +1204,57 @@ public function testHydrateBelongsToMany()
$this->assertEquals($expected, $first->tags[1]->toArray());
}

/**
* Tests that belongsToMany associations are also correctly hydrated
*
* @return void
*/
public function testFormatResultsBelongsToMany()
{
$table = TableRegistry::get('Articles');
TableRegistry::get('Tags');
$articlesTags = TableRegistry::get('ArticlesTags', [
'table' => 'articles_tags'
]);
$table->belongsToMany('Tags');

$articlesTags
->eventManager()
->attach(function ($event, $query) {
$query->formatResults(function ($results) {
return $results;
});
}, 'Model.beforeFind');


$query = new Query($this->connection, $table);

$results = $query
->select()
->contain('Tags')
->toArray();

$first = $results[0];
foreach ($first->tags as $r) {
$this->assertInstanceOf('Cake\ORM\Entity', $r);
}

$this->assertCount(2, $first->tags);
$expected = [
'id' => 1,
'name' => 'tag1',
'_joinData' => ['article_id' => 1, 'tag_id' => 1]
];
$this->assertEquals($expected, $first->tags[0]->toArray());

$expected = [
'id' => 2,
'name' => 'tag2',
'_joinData' => ['article_id' => 1, 'tag_id' => 2]
];
$this->assertEquals($expected, $first->tags[1]->toArray());
}

/**
* Tests that belongsTo relations are correctly hydrated
*
Expand Down

0 comments on commit c4b2862

Please sign in to comment.