hasMany('comments'); $articles->belongsTo('authors'); $this->assertTrue(isset($articles->authors)); $this->assertTrue(isset($articles->comments)); $this->assertFalse(isset($articles->posts)); $this->assertSame($articles->association('authors'), $articles->authors); $this->assertSame($articles->association('comments'), $articles->comments); } /** * Tests that getting a bad property throws exception * * @expectedException \RuntimeException * @expectedExceptionMessage Table "Cake\ORM\Table" is not associated with "posts" * @return void */ public function testGetBadAssociation() { $articles = TableRegistry::get('articles'); $articles->posts; } /** * Tests that the proxied updateAll will preserve conditions set for the association * * @return void */ public function testUpdateAllFromAssociation() { $articles = TableRegistry::get('articles'); $comments = TableRegistry::get('comments'); $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]); $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]); $changed = $comments->find()->where(['comment' => 'changed'])->count(); $this->assertEquals(3, $changed); } /** * Tests that the proxied deleteAll preserves conditions set for the association * * @return void */ public function testDeleteAllFromAssociation() { $articles = TableRegistry::get('articles'); $comments = TableRegistry::get('comments'); $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]); $articles->comments->deleteAll(['article_id' => 1]); $remaining = $comments->find()->where(['article_id' => 1])->count(); $this->assertEquals(1, $remaining); } /** * Tests that it is possible to get associations as a property * * @return void */ public function testAssociationAsPropertyProxy() { $articles = TableRegistry::get('articles'); $authors = TableRegistry::get('authors'); $articles->belongsTo('authors'); $authors->hasMany('comments'); $this->assertTrue(isset($articles->authors->comments)); $this->assertSame($authors->association('comments'), $articles->authors->comments); } /** * Tests that methods are proxied from the Association to the target table * * @return void */ public function testAssociationMethodProxy() { $articles = TableRegistry::get('articles'); $mock = $this->getMock('Cake\ORM\Table', ['crazy']); $articles->belongsTo('authors', [ 'targetTable' => $mock ]); $mock->expects($this->once())->method('crazy') ->with('a', 'b') ->will($this->returnValue('thing')); $this->assertEquals('thing', $articles->authors->crazy('a', 'b')); } }