getTableLocator()->clear(); } /** * Tests that it is possible to get associations as a property * * @return void */ public function testAssociationAsProperty() { $articles = $this->getTableLocator()->get('articles'); $articles->hasMany('comments'); $articles->belongsTo('authors'); $this->assertTrue(isset($articles->authors)); $this->assertTrue(isset($articles->comments)); $this->assertFalse(isset($articles->posts)); $this->assertSame($articles->getAssociation('authors'), $articles->authors); $this->assertSame($articles->getAssociation('comments'), $articles->comments); } /** * Tests that getting a bad property throws exception * * @return void */ public function testGetBadAssociation() { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('You have not defined'); $articles = $this->getTableLocator()->get('articles'); $articles->posts; } /** * Test that find() with empty conditions generates valid SQL * * @return void */ public function testFindEmptyConditions() { $table = $this->getTableLocator()->get('Users'); $table->hasMany('Articles', [ 'foreignKey' => 'author_id', 'conditions' => '', ]); $query = $table->Articles->find('list', ['limit' => 2]); $this->assertCount(2, $query->all()); } /** * Tests that the proxied updateAll will preserve conditions set for the association * * @return void */ public function testUpdateAllFromAssociation() { $articles = $this->getTableLocator()->get('articles'); $comments = $this->getTableLocator()->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 = $this->getTableLocator()->get('articles'); $comments = $this->getTableLocator()->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 = $this->getTableLocator()->get('articles'); $authors = $this->getTableLocator()->get('authors'); $articles->belongsTo('authors'); $authors->hasMany('comments'); $this->assertTrue(isset($articles->authors->comments)); $this->assertSame($authors->getAssociation('comments'), $articles->authors->comments); } /** * Tests that methods are proxied from the Association to the target table * * @return void */ public function testAssociationMethodProxy() { $articles = $this->getTableLocator()->get('articles'); $mock = $this->getMockBuilder('Cake\ORM\Table') ->setMethods(['crazy']) ->getMock(); $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')); } }