/** * Test getting associations by property. * * @return void */ public function testGetByProperty() { $belongsTo = new BelongsTo('Users', []); $this->assertEquals('user', $belongsTo->property()); $this->associations->add('Users', $belongsTo); $this->assertNull($this->associations->get('user')); $this->assertSame($belongsTo, $this->associations->getByProperty('user')); }
/** * 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 = ['foreignKey' => 'company_id', 'sourceTable' => $this->client, 'targetTable' => $this->company]; $listener = $this->getMock('stdClass', ['__invoke']); $this->company->eventManager()->attach($listener, 'Model.beforeFind'); $association = new BelongsTo('Companies', $config); $options = new \ArrayObject(['something' => 'more']); $listener->expects($this->once())->method('__invoke')->with($this->isInstanceOf('\\Cake\\Event\\Event'), $this->isInstanceOf('\\Cake\\ORM\\Query'), $options, false); $association->attachTo($query, ['queryBuilder' => function ($q) { return $q->applyOptions(['something' => 'more']); }]); }
/** * Test getting associations by property. * * @return void */ public function testGetByProperty() { $table = $this->getMockBuilder('Cake\\ORM\\Table')->setMethods(['table'])->getMock(); $table->schema([]); $belongsTo = new BelongsTo('Users', ['sourceTable' => $table]); $this->assertEquals('user', $belongsTo->property()); $this->associations->add('Users', $belongsTo); $this->assertNull($this->associations->get('user')); $this->assertSame($belongsTo, $this->associations->getByProperty('user')); }