An example of a BelongsToMany association would be Article belongs to many Tags.
Inheritance: extends Cake\ORM\Association, use trait Cake\ORM\Association\ExternalAssociationTrait
 /**
  * Test that plugin names are omitted from property()
  *
  * @return void
  */
 public function testPropertyNoPlugin()
 {
     $mock = $this->getMock('Cake\\ORM\\Table', [], [], '', false);
     $config = ['sourceTable' => $this->article, 'targetTable' => $mock];
     $association = new BelongsToMany('Contacts.Tags', $config);
     $this->assertEquals('tags', $association->property());
 }
Esempio n. 2
0
 /**
  * Test that plugin names are omitted from property()
  *
  * @return void
  */
 public function testPropertyNoPlugin()
 {
     $mock = $this->getMockBuilder('Cake\\ORM\\Table')->disableOriginalConstructor()->getMock();
     $config = ['sourceTable' => $this->article, 'targetTable' => $mock];
     $association = new BelongsToMany('Contacts.Tags', $config);
     $this->assertEquals('tags', $association->property());
 }
 /**
  * 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');
     $association = new BelongsToMany('Tags', $config);
     $listener = $this->getMock('stdClass', ['__invoke']);
     $this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
     $opts = ['something' => 'more'];
     $listener->expects($this->once())->method('__invoke')->with($this->isInstanceOf('\\Cake\\Event\\Event'), $this->isInstanceOf('\\Cake\\ORM\\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'), $this->isInstanceOf('\\Cake\\ORM\\Query'), [], false);
     $association->attachTo($query, ['queryBuilder' => function ($q) {
         return $q->applyOptions(['something' => 'more']);
     }]);
 }