Browse Source

Add a test proving that formaters on a belongsToMany join cause an error

Walther Lalk 11 years ago
parent
commit
c4b286205a
1 changed files with 51 additions and 0 deletions
  1. 51 0
      tests/TestCase/ORM/QueryTest.php

+ 51 - 0
tests/TestCase/ORM/QueryTest.php

@@ -1205,6 +1205,57 @@ class QueryTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @dataProvider internalStategiesProvider