/** * @group DataModel * @group DataModelEagerLoad * @covers FOF30\Model\DataModel::eagerLoad * @dataProvider DataModelRelationDataprovider::getTestEagerLoad */ public function testEagerLoad($test, $check) { $globRelation = null; $items = array(); $msg = 'DataModel::eagerLoad %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); // The collection should contain items? if ($test['items']) { $fakeRelationManager = new ClosureHelper(array('setDataFromCollection' => function () { })); $mockedItem = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('getRelations'), array(static::$container, $config)); $mockedItem->expects($this->any())->method('getRelations')->willReturn($fakeRelationManager); $item = clone $mockedItem; $items[] = $item; } $collection = Collection::make($items); $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('getRelations'), array(static::$container, $config)); $relation = $this->getMock('\\FOF30\\Model\\DataModel\\RelationManager', array('getData', 'getForeignKeyMap'), array($model)); // Let's check if the logic of swapping the callback function when it's not callable works $relation->expects($check['getData'] ? $this->atLeastOnce() : $this->never())->method('getData')->with($this->equalTo(isset($check['getData']['relation']) ? $check['getData']['relation'] : null), $this->callback(function ($callback = '') use(&$check) { if ($check['getData']['callback'] == 'function') { $checkCallback = is_callable($callback); } else { $checkCallback = $callback == $check['getData']['callback']; } return $checkCallback; })); $model->expects($this->any())->method('getRelations')->willReturn($relation); ReflectionHelper::setValue($model, 'eagerRelations', $test['mock']['eager']); $result = $model->eagerLoad($collection, $test['relations']); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of itself')); }
/** * Returns a DataCollection iterator based on your currently set Model state * * @param boolean $overrideLimits Should I ignore limits set in the Model? * @param integer $limitstart How many items to skip from the start, only when $overrideLimits = true * @param integer $limit How many items to return, only when $overrideLimits = true * * @return DataCollection The data collection */ public function get($overrideLimits = false, $limitstart = 0, $limit = 0) { if (!$overrideLimits) { $limitstart = $this->getState('limitstart', 0); $limit = $this->getState('limit', 0); } $dataCollection = DataCollection::make($this->getItemsArray($limitstart, $limit, $overrideLimits)); $this->eagerLoad($dataCollection, null); return $dataCollection; }