/** * @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')); }
/** * Figures out how many items exist per visual group and category type * * @param Collection $categories * * @return array */ public static function getCategoriesPerVisualGroup(Collection $categories) { $container = Container::getInstance('com_ars'); // Load visual group definitions /** @var VisualGroups $vGroupModel */ $vGroupModel = $container->factory->model('VisualGroups')->tmpInstance(); $allVisualGroups = $vGroupModel->published(1)->get(true); $visualGroups = array(); $defaultVisualGroup = (object) ['id' => 0, 'title' => '', 'description' => '', 'numitems' => ['all' => 0, 'bleedingedge' => 0, 'normal' => 0]]; if ($allVisualGroups->count()) { /** @var VisualGroups $vGroup */ foreach ($allVisualGroups as $vGroup) { // Get the number of items per visual group and render section $noOfItems = ['all' => 0, 'bleedingedge' => 0, 'normal' => 0]; if ($categories->count()) { /** @var Categories $item */ foreach ($categories as $item) { $renderSection = $item->type; if (empty($item->vgroup_id)) { $defaultVisualGroup->numitems['all']++; $defaultVisualGroup->numitems[$renderSection]++; continue; } if ($item->vgroup_id != $vGroup->id) { continue; } $noOfItems['all']++; $noOfItems[$renderSection]++; } } $visualGroups[$vGroup->id] = (object) ['id' => $vGroup->id, 'title' => $vGroup->title, 'description' => $vGroup->description, 'numitems' => $noOfItems]; } } else { /** @var Categories $item */ foreach ($categories as $item) { $renderSection = $item->type; $defaultVisualGroup->numitems['all']++; $defaultVisualGroup->numitems[$renderSection]++; } } return array_merge(array($defaultVisualGroup), $visualGroups); }
/** * Eager loads the provided relations and assigns their data to a data collection * * @param DataCollection $dataCollection The data collection on which the eager loaded relations will be applied * @param array|null $relations The relations to eager load. Leave empty to use the already defined relations * * @return $this for chaining */ public function eagerLoad(DataCollection &$dataCollection, array $relations = null) { if (empty($relations)) { $relations = $this->eagerRelations; } // Apply eager loaded relations if ($dataCollection->count() && !empty($relations)) { $relationManager = $this->getRelations(); foreach ($relations as $relation => $callback) { // Did they give us a relation name without a callback? if (!is_callable($callback) && is_string($callback) && !empty($callback)) { $relation = $callback; $callback = null; } $relationData = $relationManager->getData($relation, $callback, $dataCollection); $foreignKeyMap = $relationManager->getForeignKeyMap($relation); /** @var DataModel $item */ foreach ($dataCollection as $item) { $item->getRelations()->setDataFromCollection($relation, $relationData, $foreignKeyMap); } } } return $this; }
/** * @group DataModel * @group CollectionCall * @covers FOF30\Model\DataModel\Collection::__call * @dataProvider CollectionDataprovider::getTest__call */ public function test__call($test, $check) { $checkCall = null; $items = array(); $msg = 'Collection::__call %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); if ($test['load']) { $model = $this->getMock('\\FOF30\\Tests\\Stubs\\Model\\DataModelStub', array('getState'), array(static::$container, $config)); $items = $model->getItemsArray(0, 1); } $collection = new Collection($items); switch ($test['arguments']) { case 0: $collection->dynamicCall(); break; case 1: $collection->dynamicCall(1); break; case 2: $collection->dynamicCall(1, 1); break; case 3: $collection->dynamicCall(1, 1, 1); break; case 4: $collection->dynamicCall(1, 1, 1, 1); break; case 5: $collection->dynamicCall(1, 1, 1, 1, 1); break; case 6: $collection->dynamicCall(1, 1, 1, 1, 1, 1); break; case 7: $collection->dynamicCall(1, 1, 1, 1, 1, 1, 1); break; } if ($item = $collection->first()) { $checkCall = $item->dynamicCall; } $this->assertEquals($check['call'], $checkCall, sprintf($msg, 'Failed to correctly invoke DataModel methods')); }